Spider: Make Game creation fallible

This commit is contained in:
Sam Atkins 2023-01-06 12:21:12 +00:00 committed by Linus Groh
parent f9f6bf3cd4
commit e193679352
Notes: sideshowbarker 2024-07-17 05:03:11 +09:00
3 changed files with 13 additions and 6 deletions

View File

@ -20,16 +20,22 @@ static constexpr uint8_t new_game_animation_delay = 2;
static constexpr uint8_t draw_animation_delay = 2;
static constexpr int s_timer_interval_ms = 1000 / 60;
Game::Game()
ErrorOr<NonnullRefPtr<Game>> Game::try_create()
{
MUST(add_stack(Gfx::IntPoint { 10, Game::height - Card::height - 10 }, CardStack::Type::Waste));
MUST(add_stack(Gfx::IntPoint { Game::width - Card::width - 10, Game::height - Card::height - 10 }, CardStack::Type::Stock));
auto game = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Game()));
TRY(game->add_stack(Gfx::IntPoint { 10, Game::height - Card::height - 10 }, CardStack::Type::Waste));
TRY(game->add_stack(Gfx::IntPoint { Game::width - Card::width - 10, Game::height - Card::height - 10 }, CardStack::Type::Stock));
for (int i = 0; i < 10; i++) {
MUST(add_stack(Gfx::IntPoint { 10 + i * (Card::width + 10), 10 }, CardStack::Type::Normal));
TRY(game->add_stack(Gfx::IntPoint { 10 + i * (Card::width + 10), 10 }, CardStack::Type::Normal));
}
return game;
}
Game::Game() = default;
void Game::setup(Mode mode)
{
if (m_new_game_animation)

View File

@ -30,11 +30,12 @@ enum class GameOverReason {
};
class Game final : public Cards::CardGame {
C_OBJECT(Game)
C_OBJECT_ABSTRACT(Game)
public:
static constexpr int width = 10 + 10 * Card::width + 90 + 10;
static constexpr int height = 480;
static ErrorOr<NonnullRefPtr<Game>> try_create();
~Game() override = default;
Mode mode() const { return m_mode; }

View File

@ -118,7 +118,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
update_statistic_display(StatisticDisplay::HighScore);
auto widget = TRY(window->try_set_main_widget<GUI::Widget>());
widget->load_from_gml(spider_gml);
TRY(widget->try_load_from_gml(spider_gml));
auto& game = *widget->find_descendant_of_type_named<Spider::Game>("game");
game.set_focus(true);