From d9513bdee3769811d7d2bf4f5c9d6c4329e3b7b2 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Fri, 11 Mar 2022 16:54:18 -0800 Subject: [PATCH] Add pausing --- include/Config.hpp | 8 +++- res/textures/paused.png | Bin 0 -> 540 bytes src/Main.cpp | 86 ++++++++++++++++++++++++++++++++++------ 3 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 res/textures/paused.png diff --git a/include/Config.hpp b/include/Config.hpp index 43442b0..1c581a6 100644 --- a/include/Config.hpp +++ b/include/Config.hpp @@ -9,10 +9,16 @@ #define PLAYFIELD_X 20 #define PLAYFIELD_Y 20 +#define NEXT_X 370 +#define NEXT_Y 70 +#define NEXT_WIDTH 5 +#define NEXT_HEIGHT 5 + #define LINES_PER_LEVEL 5 #define POINTS_1_LINE 40 #define POINTS_2_LINES 100 #define POINTS_3_LINES 300 #define POINTS_4_LINES 1200 -#define MOVE_FRAME_INTERVAL 125 \ No newline at end of file +#define MOVE_FRAME_INTERVAL 125 +#define MAX_FAST_FORWARD_INTERVAL 125 \ No newline at end of file diff --git a/res/textures/paused.png b/res/textures/paused.png new file mode 100644 index 0000000000000000000000000000000000000000..444becdee04839c9831b09ea86f3fb163f196562 GIT binary patch literal 540 zcmeAS@N?(olHy`uVBq!ia0y~yV0gg5z@Wmx#=yWZA;ip?fq{XsILO_JVcj{ImkbPy z*F0SuLn`LHy}RG*aDd41k4L7dT~qRKSC`d(v0(NO2D^>?3VBu{~Y6Z~mkx$^YVw72Yr*|MrhhcDzwF`Y-M==D0F2 zy0QoaOv!(7(aE^#!1L|l>#g@&*xue{yS3`m*KOywuT5W_{q@$j`CGrV>@#F$>f%sX z(80je^(*p6spazXSGL(sT7Bbd((Q%puR?4q|2?T_f6P6LKh42GkdZ}1(7_=v-cBHK z?oqEda%-|}=coMszH^@c?b5T4Ki-h2Ka=M7wf6H|B-IzA_$8|EF7wTL?Xhxm?f#_o ztG9WseLlG+zW+=^gAx;mhEhYrqOEclsvG0ZOZ+$SeKF~O*Lyjgt@BL3mbz #include +#include #include +#include +#include +#include +#include +#include #include #include #include @@ -85,8 +92,24 @@ int main() bool move_left_immediate = false; bool move_right_immediate = false; bool snap = false; - sf::Clock update_clock; - sf::Clock move_clock; + bool paused = false; + bool paused_from_lost_focus = false; + sf::Clock update_clock, move_clock, pause_clock; + uint pause_offset = 0; + + sf::RectangleShape paused_clear; + paused_clear.setFillColor(sf::Color(81, 62, 69)); + + sf::Texture paused_texture; + paused_texture.loadFromMemory(PAUSED_TEXTURE_DATA, sizeof(PAUSED_TEXTURE_DATA)); + auto paused_texture_size = paused_texture.getSize(); + + sf::Sprite paused_text; + paused_text.setTexture(paused_texture); + paused_text.setPosition( + PLAYFIELD_X + ((float)GRID_WIDTH * TILE_SIZE / 2) - (float)paused_texture_size.x / 2, + PLAYFIELD_Y + ((float)GRID_HEIGHT * TILE_SIZE / 2) - (float)paused_texture_size.y / 2 + ); // https://stackoverflow.com/a/478088 const char *homedir; @@ -161,6 +184,24 @@ int main() sf::Sound new_highscore_sound; new_highscore_sound.setBuffer(new_highscore_buffer); + auto toggle_pause = [&] () { + paused = !paused; + if (paused) { + pause_clock.restart(); + paused_clear.setPosition(sf::Vector2f(PLAYFIELD_X, PLAYFIELD_Y)); + paused_clear.setSize(sf::Vector2f(GRID_WIDTH * TILE_SIZE, GRID_HEIGHT * TILE_SIZE)); + window.draw(paused_clear); + auto size = sf::Vector2f(NEXT_WIDTH * TILE_SIZE, NEXT_HEIGHT * TILE_SIZE); + paused_clear.setPosition(sf::Vector2f(NEXT_X, NEXT_Y) - size / 2.0f); + paused_clear.setSize(size); + window.draw(paused_clear); + window.draw(paused_text); + window.display(); + } else { + pause_offset = pause_clock.getElapsedTime().asMilliseconds(); + } + }; + while (window.isOpen()) { sf::Event event; @@ -170,22 +211,37 @@ int main() case sf::Event::Closed: window.close(); break; + case sf::Event::GainedFocus: + if (paused && paused_from_lost_focus) { + toggle_pause(); + paused_from_lost_focus = false; + } + break; + case sf::Event::LostFocus: + if (!paused) { + toggle_pause(); + paused_from_lost_focus = true; + } + break; case sf::Event::KeyPressed: switch (event.key.code) { + case sf::Keyboard::Escape: + toggle_pause(); + break; case sf::Keyboard::Space: - snap = true; + snap = !paused; break; case sf::Keyboard::Up: - rotate = true; + rotate = !paused; break; case sf::Keyboard::Left: - move_left = true; - move_left_immediate = true; + move_left = !paused; + move_left_immediate = !paused; move_clock.restart(); break; case sf::Keyboard::Right: - move_right = true; - move_right_immediate = true; + move_right = !paused; + move_right_immediate = !paused; move_clock.restart(); break; default: @@ -208,16 +264,22 @@ int main() } } - bool is_update_frame = update_clock.getElapsedTime().asMilliseconds() > (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) ? std::min({update_interval, 125u}) : update_interval); + if (paused) { + continue; + } + + bool is_update_frame = update_clock.getElapsedTime().asMilliseconds() - pause_offset > (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) ? std::min({update_interval, (uint)MAX_FAST_FORWARD_INTERVAL}) : update_interval); if (is_update_frame) { update_clock.restart(); } - bool is_move_frame = move_clock.getElapsedTime().asMilliseconds() > MOVE_FRAME_INTERVAL; + bool is_move_frame = move_clock.getElapsedTime().asMilliseconds() - pause_offset > MOVE_FRAME_INTERVAL; if (is_move_frame) { move_clock.restart(); } + pause_offset = 0; + // Rotation if (rotate) { block.rotation_state++; @@ -308,8 +370,8 @@ int main() for (auto tile : next_block_tiles) { sprite.setTextureRect(next_block.type->tile_type->texture_rect); sprite.setPosition( - 370 + (tile.x - next_block.position.x) * TILE_SIZE - x_offset, - 70 + (tile.y - next_block.position.y) * TILE_SIZE - y_offset + NEXT_X + (tile.x - next_block.position.x) * TILE_SIZE - x_offset, + NEXT_Y + (tile.y - next_block.position.y) * TILE_SIZE - y_offset ); window.draw(sprite); }