diff options
Diffstat (limited to 'Userland/Games/Snake/Game.cpp')
-rw-r--r-- | Userland/Games/Snake/Game.cpp | 83 |
1 files changed, 50 insertions, 33 deletions
diff --git a/Userland/Games/Snake/Game.cpp b/Userland/Games/Snake/Game.cpp index 8f49464b29..3afdaa34bb 100644 --- a/Userland/Games/Snake/Game.cpp +++ b/Userland/Games/Snake/Game.cpp @@ -15,45 +15,62 @@ #include <LibGfx/Font/Font.h> #include <LibGfx/Font/FontDatabase.h> +REGISTER_WIDGET(Snake, Game); + namespace Snake { -ErrorOr<NonnullRefPtr<Game>> Game::create() +static NonnullRefPtrVector<Gfx::Bitmap> load_food_bitmaps() { + static constexpr auto food_bitmaps_files = Array { + "/res/emoji/U+1F41F.png"sv, + "/res/emoji/U+1F95A.png"sv, + "/res/emoji/U+1F99C.png"sv, + "/res/emoji/U+1F986.png"sv, + "/res/emoji/U+1FAB2.png"sv, + "/res/emoji/U+1F426.png"sv, + "/res/emoji/U+1F424.png"sv, + "/res/emoji/U+1F40D.png"sv, + "/res/emoji/U+1F989.png"sv, + "/res/emoji/U+1F54A.png"sv, + "/res/emoji/U+1F408.png"sv, + "/res/emoji/U+1F420.png"sv, + "/res/emoji/U+1F415.png"sv, + "/res/emoji/U+1F429.png"sv, + "/res/emoji/U+1F98C.png"sv, + "/res/emoji/U+1F416.png"sv, + "/res/emoji/U+1F401.png"sv, + "/res/emoji/U+1F400.png"sv, + "/res/emoji/U+1F407.png"sv, + "/res/emoji/U+1F43F.png"sv, + "/res/emoji/U+1F9A5.png"sv, + "/res/emoji/U+1F423.png"sv, + "/res/emoji/U+1F425.png"sv, + "/res/emoji/U+1F98E.png"sv, + "/res/emoji/U+1F997.png"sv, + "/res/emoji/U+1FAB3.png"sv, + "/res/emoji/U+1F413.png"sv, + "/res/emoji/U+1FAB0.png"sv, + "/res/emoji/U+1FAB1.png"sv, + }; + NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps; - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F41F.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F95A.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F99C.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F986.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1FAB2.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F426.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F424.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F40D.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F989.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F54A.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F408.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F420.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F415.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F429.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F98C.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F416.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F401.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F400.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F407.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F43F.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F9A5.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F423.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F425.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F98E.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F997.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1FAB3.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1F413.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1FAB0.png"sv))); - food_bitmaps.append(*TRY(Gfx::Bitmap::try_load_from_file("/res/emoji/U+1FAB1.png"sv))); - return adopt_nonnull_ref_or_enomem(new (nothrow) Game(move(food_bitmaps))); + food_bitmaps.ensure_capacity(food_bitmaps_files.size()); + + for (auto file : food_bitmaps_files) { + auto bitmap = Gfx::Bitmap::try_load_from_file(file); + if (bitmap.is_error()) { + dbgln("\033[31;1mCould not load bitmap file\033[0m '{}': {}", file, bitmap.error()); + VERIFY_NOT_REACHED(); + } + + food_bitmaps.unchecked_append(bitmap.release_value()); + } + + return food_bitmaps; } -Game::Game(NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps) - : m_food_bitmaps(move(food_bitmaps)) +Game::Game() + : m_food_bitmaps(load_food_bitmaps()) { set_font(Gfx::FontDatabase::default_fixed_width_font().bold_variant()); reset(); |