mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:17:44 +00:00
Breakout: Only paint areas that needs to be updated
This commit is contained in:
parent
2e3df52862
commit
67e3daa679
2 changed files with 42 additions and 11 deletions
|
@ -10,7 +10,6 @@
|
||||||
#include <LibGUI/Application.h>
|
#include <LibGUI/Application.h>
|
||||||
#include <LibGUI/MessageBox.h>
|
#include <LibGUI/MessageBox.h>
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGfx/Font.h>
|
|
||||||
#include <LibGfx/StandardCursor.h>
|
#include <LibGfx/StandardCursor.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
@ -33,14 +32,19 @@ Game::~Game()
|
||||||
|
|
||||||
void Game::reset_paddle()
|
void Game::reset_paddle()
|
||||||
{
|
{
|
||||||
|
update(enclosing_int_rect(m_paddle.rect));
|
||||||
m_paddle.moving_left = false;
|
m_paddle.moving_left = false;
|
||||||
m_paddle.moving_right = false;
|
m_paddle.moving_right = false;
|
||||||
m_paddle.rect = { game_width / 2 - 40, game_height - 20, 80, 16 };
|
m_paddle.rect = { game_width / 2 - 40, game_height - 20, 80, 16 };
|
||||||
|
update(enclosing_int_rect(m_paddle.rect));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::reset()
|
void Game::reset()
|
||||||
{
|
{
|
||||||
|
update(lives_left_rect());
|
||||||
m_lives = 3;
|
m_lives = 3;
|
||||||
|
update(lives_left_rect());
|
||||||
|
|
||||||
m_pause_count = 0;
|
m_pause_count = 0;
|
||||||
m_cheater = false;
|
m_cheater = false;
|
||||||
reset_ball();
|
reset_ball();
|
||||||
|
@ -83,12 +87,16 @@ void Game::generate_bricks()
|
||||||
|
|
||||||
if (m_board != -1) {
|
if (m_board != -1) {
|
||||||
m_bricks = boards[m_board];
|
m_bricks = boards[m_board];
|
||||||
|
|
||||||
|
for (auto& brick : m_bricks)
|
||||||
|
update(enclosing_int_rect(brick.rect));
|
||||||
} else {
|
} else {
|
||||||
// Rainbow
|
// Rainbow
|
||||||
for (int row = 0; row < 7; ++row) {
|
for (int row = 0; row < 7; ++row) {
|
||||||
for (int column = 0; column < 10; ++column) {
|
for (int column = 0; column < 10; ++column) {
|
||||||
Brick brick(row, column, colors[row]);
|
Brick brick(row, column, colors[row]);
|
||||||
m_bricks.append(brick);
|
m_bricks.append(brick);
|
||||||
|
update(enclosing_int_rect(brick.rect));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,7 +113,7 @@ void Game::set_paused(bool paused)
|
||||||
set_override_cursor(Gfx::StandardCursor::Hidden);
|
set_override_cursor(Gfx::StandardCursor::Hidden);
|
||||||
}
|
}
|
||||||
|
|
||||||
update();
|
update(pause_rect());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::timer_event(Core::TimerEvent&)
|
void Game::timer_event(Core::TimerEvent&)
|
||||||
|
@ -131,15 +139,11 @@ void Game::paint_event(GUI::PaintEvent& event)
|
||||||
painter.fill_rect(enclosing_int_rect(brick.rect), brick.color);
|
painter.fill_rect(enclosing_int_rect(brick.rect), brick.color);
|
||||||
}
|
}
|
||||||
|
|
||||||
int msg_width = font().width(String::formatted("Lives: {}", m_lives));
|
painter.draw_text(lives_left_rect(), String::formatted("Lives: {}", m_lives), Gfx::TextAlignment::Center, Color::White);
|
||||||
int msg_height = font().glyph_height();
|
|
||||||
painter.draw_text({ (game_width - msg_width - 2), 2, msg_width, msg_height }, String::formatted("Lives: {}", m_lives), Gfx::TextAlignment::Center, Color::White);
|
|
||||||
|
|
||||||
if (m_paused) {
|
if (m_paused) {
|
||||||
const char* msg = m_cheater ? "C H E A T E R" : "P A U S E D";
|
const char* msg = m_cheater ? "C H E A T E R" : "P A U S E D";
|
||||||
int msg_width = font().width(msg);
|
painter.draw_text(pause_rect(), msg, Gfx::TextAlignment::Center, Color::White);
|
||||||
int msg_height = font().glyph_height();
|
|
||||||
painter.draw_text({ (game_width / 2) - (msg_width / 2), (game_height / 2) - (msg_height / 2), msg_width, msg_height }, msg, Gfx::TextAlignment::Center, Color::White);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,10 +186,12 @@ void Game::mousemove_event(GUI::MouseEvent& event)
|
||||||
{
|
{
|
||||||
if (m_paused)
|
if (m_paused)
|
||||||
return;
|
return;
|
||||||
|
update(enclosing_int_rect(m_paddle.rect));
|
||||||
float new_paddle_x = event.x() - m_paddle.rect.width() / 2;
|
float new_paddle_x = event.x() - m_paddle.rect.width() / 2;
|
||||||
new_paddle_x = max(0.0f, new_paddle_x);
|
new_paddle_x = max(0.0f, new_paddle_x);
|
||||||
new_paddle_x = min(game_width - m_paddle.rect.width(), new_paddle_x);
|
new_paddle_x = min(game_width - m_paddle.rect.width(), new_paddle_x);
|
||||||
m_paddle.rect.set_x(new_paddle_x);
|
m_paddle.rect.set_x(new_paddle_x);
|
||||||
|
update(enclosing_int_rect(m_paddle.rect));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::reset_ball()
|
void Game::reset_ball()
|
||||||
|
@ -199,17 +205,20 @@ void Game::reset_ball()
|
||||||
if (get_random<u32>() % 2)
|
if (get_random<u32>() % 2)
|
||||||
velocity_x = velocity_x * -1;
|
velocity_x = velocity_x * -1;
|
||||||
|
|
||||||
|
update(enclosing_int_rect(m_ball.rect()));
|
||||||
m_ball = {};
|
m_ball = {};
|
||||||
m_ball.position = { position_x, position_y };
|
m_ball.position = { position_x, position_y };
|
||||||
m_ball.velocity = { velocity_x, velocity_y };
|
m_ball.velocity = { velocity_x, velocity_y };
|
||||||
|
update(enclosing_int_rect(m_ball.rect()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::hurt()
|
void Game::hurt()
|
||||||
{
|
{
|
||||||
stop_timer();
|
stop_timer();
|
||||||
|
update(lives_left_rect());
|
||||||
m_lives--;
|
m_lives--;
|
||||||
|
update(lives_left_rect());
|
||||||
if (m_lives <= 0) {
|
if (m_lives <= 0) {
|
||||||
update();
|
|
||||||
GUI::MessageBox::show(window(), "You lose!", "Breakout", GUI::MessageBox::Type::Information, GUI::MessageBox::InputType::OK);
|
GUI::MessageBox::show(window(), "You lose!", "Breakout", GUI::MessageBox::Type::Information, GUI::MessageBox::InputType::OK);
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
@ -237,6 +246,8 @@ void Game::tick()
|
||||||
auto new_ball = m_ball;
|
auto new_ball = m_ball;
|
||||||
new_ball.position += new_ball.velocity;
|
new_ball.position += new_ball.velocity;
|
||||||
|
|
||||||
|
update(enclosing_int_rect(m_ball.rect()));
|
||||||
|
|
||||||
if (new_ball.x() < new_ball.radius || new_ball.x() > game_width - new_ball.radius) {
|
if (new_ball.x() < new_ball.radius || new_ball.x() > game_width - new_ball.radius) {
|
||||||
new_ball.position.set_x(m_ball.x());
|
new_ball.position.set_x(m_ball.x());
|
||||||
new_ball.velocity.set_x(new_ball.velocity.x() * -1);
|
new_ball.velocity.set_x(new_ball.velocity.x() * -1);
|
||||||
|
@ -252,6 +263,8 @@ void Game::tick()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update(enclosing_int_rect(new_ball.rect()));
|
||||||
|
|
||||||
if (new_ball.rect().intersects(m_paddle.rect)) {
|
if (new_ball.rect().intersects(m_paddle.rect)) {
|
||||||
new_ball.position.set_y(m_ball.y());
|
new_ball.position.set_y(m_ball.y());
|
||||||
new_ball.velocity.set_y(new_ball.velocity.y() * -1);
|
new_ball.velocity.set_y(new_ball.velocity.y() * -1);
|
||||||
|
@ -275,6 +288,7 @@ void Game::tick()
|
||||||
new_ball.position.set_y(m_ball.y());
|
new_ball.position.set_y(m_ball.y());
|
||||||
new_ball.velocity.set_y(new_ball.velocity.y() * -1);
|
new_ball.velocity.set_y(new_ball.velocity.y() * -1);
|
||||||
}
|
}
|
||||||
|
update(enclosing_int_rect(brick.rect));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -293,18 +307,20 @@ void Game::tick()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_paddle.moving_left) {
|
if (m_paddle.moving_left) {
|
||||||
|
update(enclosing_int_rect(m_paddle.rect));
|
||||||
m_paddle.rect.set_x(max(0.0f, m_paddle.rect.x() - m_paddle.speed));
|
m_paddle.rect.set_x(max(0.0f, m_paddle.rect.x() - m_paddle.speed));
|
||||||
|
update(enclosing_int_rect(m_paddle.rect));
|
||||||
}
|
}
|
||||||
if (m_paddle.moving_right) {
|
if (m_paddle.moving_right) {
|
||||||
|
update(enclosing_int_rect(m_paddle.rect));
|
||||||
m_paddle.rect.set_x(min(game_width - m_paddle.rect.width(), m_paddle.rect.x() + m_paddle.speed));
|
m_paddle.rect.set_x(min(game_width - m_paddle.rect.width(), m_paddle.rect.x() + m_paddle.speed));
|
||||||
|
update(enclosing_int_rect(m_paddle.rect));
|
||||||
}
|
}
|
||||||
|
|
||||||
m_ball = new_ball;
|
m_ball = new_ball;
|
||||||
|
|
||||||
if (m_pause_count > 50)
|
if (m_pause_count > 50)
|
||||||
m_cheater = true;
|
m_cheater = true;
|
||||||
|
|
||||||
update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <LibGUI/Widget.h>
|
#include <LibGUI/Widget.h>
|
||||||
|
#include <LibGfx/Font.h>
|
||||||
|
|
||||||
namespace Breakout {
|
namespace Breakout {
|
||||||
|
|
||||||
|
@ -76,6 +77,20 @@ private:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Gfx::IntRect lives_left_rect() const
|
||||||
|
{
|
||||||
|
int msg_width = font().width(String::formatted("Lives: {}", m_lives));
|
||||||
|
return { (game_width - msg_width - 2), 2, msg_width, font().glyph_height() };
|
||||||
|
}
|
||||||
|
|
||||||
|
Gfx::IntRect pause_rect() const
|
||||||
|
{
|
||||||
|
const char* msg = m_cheater ? "C H E A T E R" : "P A U S E D";
|
||||||
|
int msg_width = font().width(msg);
|
||||||
|
int msg_height = font().glyph_height();
|
||||||
|
return { (game_width / 2) - (msg_width / 2), (game_height / 2) - (msg_height / 2), msg_width, msg_height };
|
||||||
|
}
|
||||||
|
|
||||||
bool m_paused;
|
bool m_paused;
|
||||||
int m_lives;
|
int m_lives;
|
||||||
int m_board;
|
int m_board;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue