diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2017-10-12 13:20:06 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2017-10-14 12:42:09 +0200 |
commit | e1da92454d096c28292699f64d891a3af292a204 (patch) | |
tree | db84ed861a55e6c4f61c7090c19d6f0e41b6cec5 /src/render.cpp | |
parent | 753f71536339b78c49468ba6452c96d6b3c345b2 (diff) |
Initial work on animations.
Diffstat (limited to 'src/render.cpp')
-rw-r--r-- | src/render.cpp | 67 |
1 files changed, 59 insertions, 8 deletions
diff --git a/src/render.cpp b/src/render.cpp index 1baf0e0..08a4524 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -52,7 +52,29 @@ static void draw_sector(sf::RenderWindow *window, world::sector_t *sector) } } -void game::state_t::render(sf::RenderWindow *window) +void interface::state_t::render() +{ +} + +namespace render { + +state_t::state_t(sf::RenderWindow *window_) +{ + window = window_; +} + +void state_t::begin_frame(double time_) +{ + now = time_; + window->clear(); +} + +void state_t::end_frame(void) +{ + window->display(); +} + +void state_t::render(game::state_t *game) { sf::Vector2u size = window->getSize(); sf::Vector2f A, B, C, D; @@ -68,22 +90,51 @@ void game::state_t::render(sf::RenderWindow *window) bbox.width = std::max({A.x, B.x, C.x, D.x}) - bbox.left; bbox.height = std::max({A.y, B.y, C.y, D.y}) - bbox.top; - for (world::sector_t *sector : world.get_sectors(bbox)) + for (world::sector_t *sector : game->world.get_sectors(bbox)) draw_sector(window, sector); - for (world::entity_t *ent : world.get_entities(bbox)) - ent->render(window); + for (world::entity_t *ent : game->world.get_entities(bbox)) + ent->render(this); } -void game::human_t::render(sf::RenderWindow *window) +void state_t::render(animated_texture_t *anim, sf::FloatRect bounds) { + size_t frame; + + frame = floor(fmod(now * 20.0, anim->frame_count)); + + wot_rect.setTexture(anim->frames + frame, true); + wot_rect.setFillColor(sf::Color::White); wot_rect.setPosition(bounds.left, bounds.top); wot_rect.setSize(sf::Vector2f(bounds.width, bounds.height)); - wot_rect.setFillColor(sf::Color::Red); - wot_rect.setOutlineColor(sf::Color::Transparent); window->draw(wot_rect); + wot_rect.setTexture(NULL); } -void interface::state_t::render() +animated_texture_t::~animated_texture_t(void) { + //delete[] frames; } + +bool animated_texture_t::load(std::string prefix, size_t frame_count_) +{ + frame_count = frame_count_; + frames = new sf::Texture[frame_count]; + + for (size_t i = 0; i < frame_count; i++) { + std::string path; + + path = prefix + std::to_string(i) + ".png"; + std::cout << "load " << path << "\n"; + + if (!frames[i].loadFromFile(path)) { + // FIXME + //delete[] frames; + return false; + } + } + + return true; +} + +} // namespace render |