diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-20 15:04:02 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-20 15:04:34 +0200 |
commit | 79573ce7e667503addd37ce6d14e353227259e87 (patch) | |
tree | 8059705e2445d97636e37a547a911e5da8512574 /Games | |
parent | 2d736d5591c85f94b49d02b5e8415b98a5577c5b (diff) | |
download | serenity-79573ce7e667503addd37ce6d14e353227259e87.zip |
Snake: Try to only repaint the parts that actually changed between ticks.
Diffstat (limited to 'Games')
-rw-r--r-- | Games/Snake/SnakeGame.cpp | 55 | ||||
-rw-r--r-- | Games/Snake/SnakeGame.h | 3 |
2 files changed, 43 insertions, 15 deletions
diff --git a/Games/Snake/SnakeGame.cpp b/Games/Snake/SnakeGame.cpp index 4aad4e553a..24a539c83a 100644 --- a/Games/Snake/SnakeGame.cpp +++ b/Games/Snake/SnakeGame.cpp @@ -7,6 +7,7 @@ SnakeGame::SnakeGame(GWidget* parent) : GWidget(parent) { + set_font(Font::default_bold_font()); m_fruit_bitmap = GraphicsBitmap::load_from_file("/res/icons/snake/paprika.png"); srand(time(nullptr)); reset(); @@ -22,10 +23,12 @@ void SnakeGame::reset() m_tail.clear_with_capacity(); m_length = 2; m_score = 0; + m_score_text = "Score: 0"; m_velocity_queue.clear(); stop_timer(); start_timer(120); spawn_fruit(); + update(); } bool SnakeGame::is_available(const Coordinate& coord) @@ -53,16 +56,28 @@ void SnakeGame::spawn_fruit() m_fruit = coord; } +Rect SnakeGame::score_rect() const +{ + int score_width = font().width(m_score_text); + return { width() - score_width - 2, height() - font().glyph_height() - 2, score_width, font().glyph_height() }; +} + void SnakeGame::timer_event(CTimerEvent&) { + Vector<Coordinate> dirty_cells; + m_tail.prepend(m_head); - if (m_tail.size() > m_length) + if (m_tail.size() > m_length) { + dirty_cells.append(m_tail.last()); m_tail.take_last(); + } if (!m_velocity_queue.is_empty()) m_velocity = m_velocity_queue.dequeue(); + dirty_cells.append(m_head); + m_head.row += m_velocity.vertical; m_head.column += m_velocity.horizontal; @@ -77,6 +92,8 @@ void SnakeGame::timer_event(CTimerEvent&) if (m_head.column < 0) m_head.column = m_columns - 1; + dirty_cells.append(m_head); + for (int i = 0; i < m_tail.size(); ++i) { if (m_head == m_tail[i]) { game_over(); @@ -87,9 +104,16 @@ void SnakeGame::timer_event(CTimerEvent&) if (m_head == m_fruit) { ++m_length; ++m_score; + m_score_text = String::format("Score: %u", m_score); + update(score_rect()); + dirty_cells.append(m_fruit); spawn_fruit(); + dirty_cells.append(m_fruit); + } + + for (auto& coord : dirty_cells) { + update(cell_rect(coord)); } - update(); } void SnakeGame::keydown_event(GKeyEvent& event) @@ -124,22 +148,23 @@ void SnakeGame::keydown_event(GKeyEvent& event) } } -void SnakeGame::paint_event(GPaintEvent& event) +Rect SnakeGame::cell_rect(const Coordinate& coord) const { - GPainter painter(*this); - painter.fill_rect(event.rect(), Color::Black); - auto game_rect = rect(); auto cell_size = Size(game_rect.width() / m_columns, game_rect.height() / m_rows); - - auto cell_rect = [&] (const Coordinate& coord) -> Rect { - return { - coord.column * cell_size.width(), - coord.row * cell_size.height(), - cell_size.width(), - cell_size.height() - }; + return { + coord.column * cell_size.width(), + coord.row * cell_size.height(), + cell_size.width(), + cell_size.height() }; +} + +void SnakeGame::paint_event(GPaintEvent& event) +{ + GPainter painter(*this); + painter.add_clip_rect(event.rect()); + painter.fill_rect(event.rect(), Color::Black); painter.fill_rect(cell_rect(m_head), Color::Yellow); for (auto& coord : m_tail) @@ -147,7 +172,7 @@ void SnakeGame::paint_event(GPaintEvent& event) painter.draw_scaled_bitmap(cell_rect(m_fruit), *m_fruit_bitmap, m_fruit_bitmap->rect()); - painter.draw_text(rect(), String::format("Score: %u", m_score), TextAlignment::TopLeft, Color::White); + painter.draw_text(score_rect(), m_score_text, TextAlignment::TopLeft, Color::White); } void SnakeGame::game_over() diff --git a/Games/Snake/SnakeGame.h b/Games/Snake/SnakeGame.h index 3102e029e9..192b548521 100644 --- a/Games/Snake/SnakeGame.h +++ b/Games/Snake/SnakeGame.h @@ -35,6 +35,8 @@ private: bool is_available(const Coordinate&); void queue_velocity(int v, int h); const Velocity& last_velocity() const; + Rect cell_rect(const Coordinate&) const; + Rect score_rect() const; int m_rows { 20 }; int m_columns { 20 }; @@ -51,6 +53,7 @@ private: int m_length { 0 }; unsigned m_score { 0 }; + String m_score_text; RetainPtr<GraphicsBitmap> m_fruit_bitmap; }; |