mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:07: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:
parent
e2299b52de
commit
dd65f52331
2 changed files with 17 additions and 4 deletions
|
@ -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());
|
painter.draw_scaled_bitmap(enclosing_int_rect(m_bug.rect()), *m_bug.current_bitmap(), m_bug.flapping_bitmap->rect());
|
||||||
|
|
||||||
if (m_active) {
|
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()) {
|
} 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" : " ");
|
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 {
|
} 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()
|
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) {
|
if (m_active) {
|
||||||
|
queue_update();
|
||||||
|
|
||||||
m_difficulty += 1.0f / 16.0f;
|
m_difficulty += 1.0f / 16.0f;
|
||||||
|
|
||||||
m_bug.fall();
|
m_bug.fall();
|
||||||
|
@ -124,7 +135,7 @@ void Game::tick()
|
||||||
|
|
||||||
m_restart_cooldown -= 1.0f / 16.0f;
|
m_restart_cooldown -= 1.0f / 16.0f;
|
||||||
|
|
||||||
update();
|
queue_update();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,6 +148,8 @@ private:
|
||||||
float m_difficulty {};
|
float m_difficulty {};
|
||||||
float m_restart_cooldown {};
|
float m_restart_cooldown {};
|
||||||
const RefPtr<Gfx::Bitmap> m_background_bitmap { Gfx::Bitmap::load_from_file("/res/icons/flappybug/background.png") };
|
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 };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue