summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorcreator1creeper1 <creator1creeper1@airmail.cc>2021-12-23 22:29:00 +0100
committerBrian Gianforcaro <b.gianfo@gmail.com>2021-12-23 17:42:05 -0800
commit6f592a32cc1383250a91be6189dcb2565851cb2a (patch)
tree1c05698f87c7cff584160c6ec5d207262999cf41 /Userland
parentff359c27e3cf026abee0d772d2e28a4724d070f5 (diff)
downloadserenity-6f592a32cc1383250a91be6189dcb2565851cb2a.zip
FlappyBug: Propagate errors in Cloud class
Move-construct Cloud into the Game class to improve error handling.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Games/FlappyBug/Game.cpp3
-rw-r--r--Userland/Games/FlappyBug/Game.h23
-rw-r--r--Userland/Games/FlappyBug/main.cpp2
3 files changed, 19 insertions, 9 deletions
diff --git a/Userland/Games/FlappyBug/Game.cpp b/Userland/Games/FlappyBug/Game.cpp
index 85041743b3..b494cfd204 100644
--- a/Userland/Games/FlappyBug/Game.cpp
+++ b/Userland/Games/FlappyBug/Game.cpp
@@ -8,8 +8,9 @@
namespace FlappyBug {
-Game::Game(Bug bug)
+Game::Game(Bug bug, Cloud cloud)
: m_bug(move(bug))
+ , m_cloud(move(cloud))
{
set_override_cursor(Gfx::StandardCursor::Hidden);
start_timer(16);
diff --git a/Userland/Games/FlappyBug/Game.h b/Userland/Games/FlappyBug/Game.h
index fe06c32599..8b0a46b103 100644
--- a/Userland/Games/FlappyBug/Game.h
+++ b/Userland/Games/FlappyBug/Game.h
@@ -122,21 +122,30 @@ public:
};
struct Cloud {
- Vector<NonnullRefPtr<Gfx::Bitmap>> const cloud_bitmaps {
- Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/cloud_0.png").release_value_but_fixme_should_propagate_errors(),
- Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/cloud_1.png").release_value_but_fixme_should_propagate_errors(),
- Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/cloud_2.png").release_value_but_fixme_should_propagate_errors(),
- };
+ Vector<NonnullRefPtr<Gfx::Bitmap>> const cloud_bitmaps;
float x {};
float y {};
int bitmap_id {};
- Cloud()
+ private:
+ Cloud(Vector<NonnullRefPtr<Gfx::Bitmap>> const cloud_bitmaps_value)
+ : cloud_bitmaps(move(cloud_bitmaps_value))
{
reset();
x = get_random_uniform(game_width);
}
+ public:
+ static ErrorOr<Cloud> construct()
+ {
+ Vector<NonnullRefPtr<Gfx::Bitmap>> const cloud_bitmaps {
+ TRY(Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/cloud_0.png")),
+ TRY(Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/cloud_1.png")),
+ TRY(Gfx::Bitmap::try_load_from_file("/res/icons/flappybug/cloud_2.png")),
+ };
+ return Cloud(move(cloud_bitmaps));
+ }
+
void reset()
{
bitmap_id = get_random_uniform(cloud_bitmaps.size());
@@ -168,7 +177,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(Bug);
+ Game(Bug, Cloud);
};
}
diff --git a/Userland/Games/FlappyBug/main.cpp b/Userland/Games/FlappyBug/main.cpp
index eab09f91e3..fc6aac43e8 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>(TRY(FlappyBug::Game::Bug::construct())));
+ auto widget = TRY(window->try_set_main_widget<FlappyBug::Game>(TRY(FlappyBug::Game::Bug::construct()), TRY(FlappyBug::Game::Cloud::construct())));
widget->on_game_end = [&](u32 score) {
if (score <= high_score)