diff --git a/Userland/Games/Solitaire/Game.cpp b/Userland/Games/Solitaire/Game.cpp index f11fefdffdc..fd394659cdb 100644 --- a/Userland/Games/Solitaire/Game.cpp +++ b/Userland/Games/Solitaire/Game.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2020, Till Mayer - * Copyright (c) 2021-2022, Sam Atkins + * Copyright (c) 2021-2023, Sam Atkins * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause @@ -19,24 +19,30 @@ namespace Solitaire { static constexpr uint8_t new_game_animation_delay = 2; static constexpr int s_timer_interval_ms = 1000 / 60; -Game::Game() +ErrorOr> Game::try_create() { - MUST(add_stack(Gfx::IntPoint { 10, 10 }, CardStack::Type::Stock)); - MUST(add_stack(Gfx::IntPoint { 10 + Card::width + 10, 10 }, CardStack::Type::Waste)); - MUST(add_stack(Gfx::IntPoint { 10 + Card::width + 10, 10 }, CardStack::Type::Play, stack_at_location(Waste))); - MUST(add_stack(Gfx::IntPoint { Game::width - 4 * Card::width - 40, 10 }, CardStack::Type::Foundation)); - MUST(add_stack(Gfx::IntPoint { Game::width - 3 * Card::width - 30, 10 }, CardStack::Type::Foundation)); - MUST(add_stack(Gfx::IntPoint { Game::width - 2 * Card::width - 20, 10 }, CardStack::Type::Foundation)); - MUST(add_stack(Gfx::IntPoint { Game::width - Card::width - 10, 10 }, CardStack::Type::Foundation)); - MUST(add_stack(Gfx::IntPoint { 10, 10 + Card::height + 10 }, CardStack::Type::Normal)); - MUST(add_stack(Gfx::IntPoint { 10 + Card::width + 10, 10 + Card::height + 10 }, CardStack::Type::Normal)); - MUST(add_stack(Gfx::IntPoint { 10 + 2 * Card::width + 20, 10 + Card::height + 10 }, CardStack::Type::Normal)); - MUST(add_stack(Gfx::IntPoint { 10 + 3 * Card::width + 30, 10 + Card::height + 10 }, CardStack::Type::Normal)); - MUST(add_stack(Gfx::IntPoint { 10 + 4 * Card::width + 40, 10 + Card::height + 10 }, CardStack::Type::Normal)); - MUST(add_stack(Gfx::IntPoint { 10 + 5 * Card::width + 50, 10 + Card::height + 10 }, CardStack::Type::Normal)); - MUST(add_stack(Gfx::IntPoint { 10 + 6 * Card::width + 60, 10 + Card::height + 10 }, CardStack::Type::Normal)); + auto game = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Game())); + + TRY(game->add_stack(Gfx::IntPoint { 10, 10 }, CardStack::Type::Stock)); + TRY(game->add_stack(Gfx::IntPoint { 10 + Card::width + 10, 10 }, CardStack::Type::Waste)); + TRY(game->add_stack(Gfx::IntPoint { 10 + Card::width + 10, 10 }, CardStack::Type::Play, game->stack_at_location(Waste))); + TRY(game->add_stack(Gfx::IntPoint { Game::width - 4 * Card::width - 40, 10 }, CardStack::Type::Foundation)); + TRY(game->add_stack(Gfx::IntPoint { Game::width - 3 * Card::width - 30, 10 }, CardStack::Type::Foundation)); + TRY(game->add_stack(Gfx::IntPoint { Game::width - 2 * Card::width - 20, 10 }, CardStack::Type::Foundation)); + TRY(game->add_stack(Gfx::IntPoint { Game::width - Card::width - 10, 10 }, CardStack::Type::Foundation)); + TRY(game->add_stack(Gfx::IntPoint { 10, 10 + Card::height + 10 }, CardStack::Type::Normal)); + TRY(game->add_stack(Gfx::IntPoint { 10 + Card::width + 10, 10 + Card::height + 10 }, CardStack::Type::Normal)); + TRY(game->add_stack(Gfx::IntPoint { 10 + 2 * Card::width + 20, 10 + Card::height + 10 }, CardStack::Type::Normal)); + TRY(game->add_stack(Gfx::IntPoint { 10 + 3 * Card::width + 30, 10 + Card::height + 10 }, CardStack::Type::Normal)); + TRY(game->add_stack(Gfx::IntPoint { 10 + 4 * Card::width + 40, 10 + Card::height + 10 }, CardStack::Type::Normal)); + TRY(game->add_stack(Gfx::IntPoint { 10 + 5 * Card::width + 50, 10 + Card::height + 10 }, CardStack::Type::Normal)); + TRY(game->add_stack(Gfx::IntPoint { 10 + 6 * Card::width + 60, 10 + Card::height + 10 }, CardStack::Type::Normal)); + + return game; } +Game::Game() = default; + static float rand_float() { return get_random_uniform(RAND_MAX) / static_cast(RAND_MAX); diff --git a/Userland/Games/Solitaire/Game.h b/Userland/Games/Solitaire/Game.h index 1b8d2a024b7..fc643fde624 100644 --- a/Userland/Games/Solitaire/Game.h +++ b/Userland/Games/Solitaire/Game.h @@ -1,6 +1,6 @@ /* * Copyright (c) 2020, Till Mayer - * Copyright (c) 2021-2022, Sam Atkins + * Copyright (c) 2021-2023, Sam Atkins * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause @@ -29,11 +29,12 @@ enum class GameOverReason { }; class Game final : public Cards::CardGame { - C_OBJECT(Game) + C_OBJECT_ABSTRACT(Game) public: static constexpr int width = 640; static constexpr int height = 480; + static ErrorOr> try_create(); virtual ~Game() override = default; Mode mode() const { return m_mode; } diff --git a/Userland/Games/Solitaire/main.cpp b/Userland/Games/Solitaire/main.cpp index 8f8617ab3f6..9d63096416c 100644 --- a/Userland/Games/Solitaire/main.cpp +++ b/Userland/Games/Solitaire/main.cpp @@ -1,7 +1,7 @@ /* * Copyright (c) 2020, Till Mayer * Copyright (c) 2021, the SerenityOS developers. - * Copyright (c) 2022, Sam Atkins + * Copyright (c) 2022-2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -84,7 +84,7 @@ ErrorOr serenity_main(Main::Arguments arguments) update_mode(Solitaire::Mode::SingleCardDraw); auto widget = TRY(window->try_set_main_widget()); - widget->load_from_gml(solitaire_gml); + TRY(widget->try_load_from_gml(solitaire_gml)); auto& game = *widget->find_descendant_of_type_named("game"); game.set_focus(true);