diff options
author | creator1creeper1 <creator1creeper1@airmail.cc> | 2021-12-23 22:21:33 +0100 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2021-12-23 17:42:05 -0800 |
commit | ff359c27e3cf026abee0d772d2e28a4724d070f5 (patch) | |
tree | 951498e39816ffdf5f4188122029cf631a1dd55e | |
parent | a67afa735cfb8d95f62cc961fbb4f5f61866309b (diff) | |
download | serenity-ff359c27e3cf026abee0d772d2e28a4724d070f5.zip |
FlappyBug: Propagate errors in Bug class
Move-construct Bug into the Game class to improve
error handling.
-rw-r--r-- | Userland/Games/FlappyBug/Game.cpp | 3 | ||||
-rw-r--r-- | Userland/Games/FlappyBug/Game.h | 21 | ||||
-rw-r--r-- | Userland/Games/FlappyBug/main.cpp | 2 |
3 files changed, 21 insertions, 5 deletions
diff --git a/Userland/Games/FlappyBug/Game.cpp b/Userland/Games/FlappyBug/Game.cpp index 02344d7f85..85041743b3 100644 --- a/Userland/Games/FlappyBug/Game.cpp +++ b/Userland/Games/FlappyBug/Game.cpp @@ -8,7 +8,8 @@ namespace FlappyBug { -Game::Game() +Game::Game(Bug bug) + : m_bug(move(bug)) { set_override_cursor(Gfx::StandardCursor::Hidden); start_timer(16); diff --git a/Userland/Games/FlappyBug/Game.h b/Userland/Games/FlappyBug/Game.h index fb9d4e890c..fe06c32599 100644 --- a/Userland/Games/FlappyBug/Game.h +++ b/Userland/Games/FlappyBug/Game.h @@ -44,11 +44,26 @@ public: const float x { 50 }; const float radius { 16 }; const float starting_y { 200 }; - NonnullRefPtr<Gfx::Bitmap> falling_bitmap { Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/falling.png").release_value_but_fixme_should_propagate_errors() }; - NonnullRefPtr<Gfx::Bitmap> flapping_bitmap { Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/flapping.png").release_value_but_fixme_should_propagate_errors() }; + NonnullRefPtr<Gfx::Bitmap> falling_bitmap; + NonnullRefPtr<Gfx::Bitmap> flapping_bitmap; float y {}; float velocity {}; + private: + Bug(NonnullRefPtr<Gfx::Bitmap> falling_bitmap_value, NonnullRefPtr<Gfx::Bitmap> flapping_bitmap_value) + : falling_bitmap(move(falling_bitmap_value)) + , flapping_bitmap(move(flapping_bitmap_value)) + { + } + + public: + static ErrorOr<Bug> construct() + { + NonnullRefPtr<Gfx::Bitmap> falling_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/falling.png")); + NonnullRefPtr<Gfx::Bitmap> flapping_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/flapping.png")); + return Bug(move(falling_bitmap), move(flapping_bitmap)); + } + void reset() { y = starting_y; @@ -153,7 +168,7 @@ private: 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 }; - Game(); + Game(Bug); }; } diff --git a/Userland/Games/FlappyBug/main.cpp b/Userland/Games/FlappyBug/main.cpp index 008a9f4fbc..eab09f91e3 100644 --- a/Userland/Games/FlappyBug/main.cpp +++ b/Userland/Games/FlappyBug/main.cpp @@ -37,7 +37,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) window->set_title("Flappy Bug"); window->set_double_buffering_enabled(false); window->set_resizable(false); - auto widget = TRY(window->try_set_main_widget<FlappyBug::Game>()); + auto widget = TRY(window->try_set_main_widget<FlappyBug::Game>(TRY(FlappyBug::Game::Bug::construct()))); widget->on_game_end = [&](u32 score) { if (score <= high_score) |