diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-07-10 17:15:38 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-10 23:28:20 +0200 |
commit | dd65f52331653a445bce9831736d53a246430631 (patch) | |
tree | 74ab927987e50adf1846d8f486927e8d3c09cd1a | |
parent | e2299b52deec1b189e9d692b2c7d7e3ffa77466e (diff) | |
download | serenity-dd65f52331653a445bce9831736d53a246430631.zip |
FlappyBug: Only paint areas which need to be painted
Drawing the entire PNG background is rather expensive. Instead, only
draw the rects that are updating.
-rw-r--r-- | Userland/Games/FlappyBug/Game.cpp | 19 | ||||
-rw-r--r-- | Userland/Games/FlappyBug/Game.h | 2 |
2 files changed, 17 insertions, 4 deletions
diff --git a/Userland/Games/FlappyBug/Game.cpp b/Userland/Games/FlappyBug/Game.cpp index f236ada2d9..f174bd9193 100644 --- a/Userland/Games/FlappyBug/Game.cpp +++ b/Userland/Games/FlappyBug/Game.cpp @@ -69,12 +69,12 @@ void Game::paint_event(GUI::PaintEvent& event) painter.draw_scaled_bitmap(enclosing_int_rect(m_bug.rect()), *m_bug.current_bitmap(), m_bug.flapping_bitmap->rect()); if (m_active) { - painter.draw_text({ 10, 10, 100, 100 }, String::formatted("{:.0}", m_difficulty), Gfx::TextAlignment::TopLeft, Color::White); + painter.draw_text(m_score_rect, String::formatted("{:.0}", m_difficulty), Gfx::TextAlignment::TopLeft, Color::White); } else if (m_high_score.has_value()) { auto message = String::formatted("Your score: {:.0}\nHigh score: {:.0}\n\n{}", m_last_score, m_high_score.value(), m_restart_cooldown < 0 ? "Press any key to play again" : " "); - painter.draw_text(rect(), message, Gfx::TextAlignment::Center, Color::White); + painter.draw_text(m_text_rect, message, Gfx::TextAlignment::Center, Color::White); } else { - painter.draw_text(rect(), "Press any key to start", Gfx::TextAlignment::Center, Color::White); + painter.draw_text(m_text_rect, "Press any key to start", Gfx::TextAlignment::Center, Color::White); } } @@ -97,7 +97,18 @@ void Game::keydown_event(GUI::KeyEvent& event) void Game::tick() { + auto queue_update = [&]() { + update(m_score_rect); + update(m_text_rect); + update(enclosing_int_rect(m_bug.rect())); + update(enclosing_int_rect(m_obstacle.top_rect())); + update(enclosing_int_rect(m_obstacle.bottom_rect())); + update(m_cloud.rect()); + }; + if (m_active) { + queue_update(); + m_difficulty += 1.0f / 16.0f; m_bug.fall(); @@ -124,7 +135,7 @@ void Game::tick() m_restart_cooldown -= 1.0f / 16.0f; - update(); + queue_update(); } } diff --git a/Userland/Games/FlappyBug/Game.h b/Userland/Games/FlappyBug/Game.h index 90061b0176..0b285462f1 100644 --- a/Userland/Games/FlappyBug/Game.h +++ b/Userland/Games/FlappyBug/Game.h @@ -148,6 +148,8 @@ private: float m_difficulty {}; float m_restart_cooldown {}; const RefPtr<Gfx::Bitmap> m_background_bitmap { Gfx::Bitmap::load_from_file("/res/icons/flappybug/background.png") }; + const Gfx::IntRect m_score_rect { 10, 10, 20, 20 }; + const Gfx::IntRect m_text_rect { game_width / 2 - 80, game_height / 2 - 40, 160, 80 }; }; } |