1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:27:45 +00:00

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.
This commit is contained in:
Timothy Flynn 2021-07-10 17:15:38 -04:00 committed by Andreas Kling
parent e2299b52de
commit dd65f52331
2 changed files with 17 additions and 4 deletions

View file

@ -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();
}
}