diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-05-04 23:00:53 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-05 21:38:45 +0200 |
commit | 15f0ee1727c964aed73363b65b0b4270640ac370 (patch) | |
tree | eb652591e100b30e1d9950db2bbafc02a06d28df /Userland/Games | |
parent | cfc3a2ebac3702c5517847a22a75e682aa219e9a (diff) | |
download | serenity-15f0ee1727c964aed73363b65b0b4270640ac370.zip |
Solitaire: Replace self-owned timer with Core::Object's timer
This is just a bit nicer than owning a separate timer in the Solitaire
application because LibCore will prevent timer events from firing when
e.g. the window is not visible. Therefore SolitaireWidget doesn't need
need to check for such conditions.
Diffstat (limited to 'Userland/Games')
-rw-r--r-- | Userland/Games/Solitaire/SolitaireWidget.cpp | 22 | ||||
-rw-r--r-- | Userland/Games/Solitaire/SolitaireWidget.h | 5 | ||||
-rw-r--r-- | Userland/Games/Solitaire/main.cpp | 2 |
3 files changed, 8 insertions, 21 deletions
diff --git a/Userland/Games/Solitaire/SolitaireWidget.cpp b/Userland/Games/Solitaire/SolitaireWidget.cpp index f42cab19a1..19df773abf 100644 --- a/Userland/Games/Solitaire/SolitaireWidget.cpp +++ b/Userland/Games/Solitaire/SolitaireWidget.cpp @@ -5,15 +5,14 @@ */ #include "SolitaireWidget.h" -#include <LibCore/Timer.h> #include <LibGUI/Painter.h> -#include <LibGUI/Window.h> #include <time.h> static const Color s_background_color { Color::from_rgb(0x008000) }; static constexpr uint8_t new_game_animation_delay = 5; +static constexpr int s_timer_interval_ms = 1000 / 60; -SolitaireWidget::SolitaireWidget(GUI::Window& window, Function<void(uint32_t)>&& on_score_update) +SolitaireWidget::SolitaireWidget(Function<void(uint32_t)>&& on_score_update) : m_on_score_update(move(on_score_update)) { set_fill_with_background_color(false); @@ -31,9 +30,6 @@ SolitaireWidget::SolitaireWidget(GUI::Window& window, Function<void(uint32_t)>&& m_stacks[Pile5] = CardStack({ 10 + 4 * Card::width + 40, 10 + Card::height + 10 }, CardStack::Type::Normal); m_stacks[Pile6] = CardStack({ 10 + 5 * Card::width + 50, 10 + Card::height + 10 }, CardStack::Type::Normal); m_stacks[Pile7] = CardStack({ 10 + 6 * Card::width + 60, 10 + Card::height + 10 }, CardStack::Type::Normal); - - m_timer = Core::Timer::construct(1000 / 60, [&]() { tick(window); }); - m_timer->stop(); } SolitaireWidget::~SolitaireWidget() @@ -45,11 +41,8 @@ static float rand_float() return rand() / static_cast<float>(RAND_MAX); } -void SolitaireWidget::tick(GUI::Window& window) +void SolitaireWidget::timer_event(Core::TimerEvent&) { - if (!is_visible() || !updates_enabled() || !window.is_visible_for_timer_purposes()) - return; - if (m_game_over_animation) { VERIFY(!m_animation.card().is_null()); if (m_animation.card()->position().x() > SolitaireWidget::width || m_animation.card()->rect().right() < 0) @@ -96,7 +89,7 @@ void SolitaireWidget::stop_game_over_animation() void SolitaireWidget::setup() { stop_game_over_animation(); - m_timer->stop(); + stop_timer(); for (auto& stack : m_stacks) stack.clear(); @@ -118,7 +111,7 @@ void SolitaireWidget::setup() m_new_deck.append(m_new_deck.take(rand() % m_new_deck.size())); m_new_game_animation = true; - m_timer->start(); + start_timer(s_timer_interval_ms); update(); } @@ -353,11 +346,6 @@ void SolitaireWidget::paint_event(GUI::PaintEvent& event) GUI::Painter painter(*this); if (m_repaint_all) { - /* Only start the timer when update() got called from the - window manager, or else we might end up with a blank screen */ - if (!m_timer->is_active()) - m_timer->start(); - painter.fill_rect(event.rect(), s_background_color); for (auto& stack : m_stacks) diff --git a/Userland/Games/Solitaire/SolitaireWidget.h b/Userland/Games/Solitaire/SolitaireWidget.h index a7d0b95553..b7efe3cc31 100644 --- a/Userland/Games/Solitaire/SolitaireWidget.h +++ b/Userland/Games/Solitaire/SolitaireWidget.h @@ -20,7 +20,7 @@ public: void setup(); private: - SolitaireWidget(GUI::Window&, Function<void(uint32_t)>&& on_score_update); + SolitaireWidget(Function<void(uint32_t)>&& on_score_update); class Animation { public: @@ -85,7 +85,6 @@ private: void stop_game_over_animation(); void create_new_animation_card(); void check_for_game_over(); - void tick(GUI::Window&); ALWAYS_INLINE CardStack& stack(StackLocation location) { @@ -98,8 +97,8 @@ private: virtual void mousemove_event(GUI::MouseEvent&) override; virtual void doubleclick_event(GUI::MouseEvent&) override; virtual void keydown_event(GUI::KeyEvent&) override; + virtual void timer_event(Core::TimerEvent&) override; - RefPtr<Core::Timer> m_timer; NonnullRefPtrVector<Card> m_focused_cards; NonnullRefPtrVector<Card> m_new_deck; CardStack m_stacks[StackLocation::__Count]; diff --git a/Userland/Games/Solitaire/main.cpp b/Userland/Games/Solitaire/main.cpp index d345a64e18..3fcdf5152e 100644 --- a/Userland/Games/Solitaire/main.cpp +++ b/Userland/Games/Solitaire/main.cpp @@ -39,7 +39,7 @@ int main(int argc, char** argv) window->set_resizable(false); window->resize(SolitaireWidget::width, SolitaireWidget::height); - auto widget = SolitaireWidget::construct(window, [&](uint32_t score) { + auto widget = SolitaireWidget::construct([&](uint32_t score) { window->set_title(String::formatted("Score: {} - Solitaire", score)); }); |