mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-04 01:05:58 +03:00
Solitaire: Make Game creation fallible
This commit is contained in:
parent
6e0ad5536f
commit
306676792c
Notes:
sideshowbarker
2024-07-17 18:08:55 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/306676792c Pull-request: https://github.com/SerenityOS/serenity/pull/16851 Reviewed-by: https://github.com/linusg Reviewed-by: https://github.com/trflynn89 ✅
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
|
||||
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
|
||||
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
* 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<NonnullRefPtr<Game>> 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<float>(RAND_MAX);
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
|
||||
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
|
||||
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
* 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<NonnullRefPtr<Game>> try_create();
|
||||
virtual ~Game() override = default;
|
||||
|
||||
Mode mode() const { return m_mode; }
|
||||
|
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
|
||||
* Copyright (c) 2021, the SerenityOS developers.
|
||||
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
||||
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
@ -84,7 +84,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
update_mode(Solitaire::Mode::SingleCardDraw);
|
||||
|
||||
auto widget = TRY(window->try_set_main_widget<GUI::Widget>());
|
||||
widget->load_from_gml(solitaire_gml);
|
||||
TRY(widget->try_load_from_gml(solitaire_gml));
|
||||
|
||||
auto& game = *widget->find_descendant_of_type_named<Solitaire::Game>("game");
|
||||
game.set_focus(true);
|
||||
|
Loading…
Reference in New Issue
Block a user