2021-06-01 13:16:19 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Mim Hufford <mim@hotmail.co.uk>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Game.h"
|
2022-01-09 16:25:38 -03:00
|
|
|
#include <AK/URL.h>
|
2021-08-26 12:15:57 -03:00
|
|
|
#include <LibConfig/Client.h>
|
2021-11-23 06:59:50 -03:00
|
|
|
#include <LibCore/System.h>
|
2022-01-09 16:25:38 -03:00
|
|
|
#include <LibDesktop/Launcher.h>
|
2021-06-01 13:16:19 -03:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
|
#include <LibGUI/Icon.h>
|
|
|
|
|
#include <LibGUI/Menu.h>
|
|
|
|
|
#include <LibGUI/Menubar.h>
|
2021-06-22 11:50:54 -03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2021-06-01 13:16:19 -03:00
|
|
|
#include <LibGUI/Window.h>
|
2021-11-22 20:46:18 -03:00
|
|
|
#include <LibMain/Main.h>
|
2021-06-01 13:16:19 -03:00
|
|
|
|
2021-11-22 20:46:18 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-06-01 13:16:19 -03:00
|
|
|
{
|
2021-11-27 19:26:34 -03:00
|
|
|
TRY(Core::System::pledge("stdio rpath recvfd sendfd unix"));
|
2021-06-01 13:16:19 -03:00
|
|
|
|
2021-11-23 21:38:27 -03:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2021-06-01 13:16:19 -03:00
|
|
|
|
2022-02-11 13:33:09 -03:00
|
|
|
Config::pledge_domain("FlappyBug");
|
2021-08-26 12:15:57 -03:00
|
|
|
|
2022-01-09 16:25:38 -03:00
|
|
|
TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man6/FlappyBug.md") }));
|
|
|
|
|
TRY(Desktop::Launcher::seal_allowlist());
|
|
|
|
|
|
2021-11-27 19:26:34 -03:00
|
|
|
TRY(Core::System::pledge("stdio rpath recvfd sendfd"));
|
2021-06-01 13:16:19 -03:00
|
|
|
|
2021-11-23 06:59:50 -03:00
|
|
|
TRY(Core::System::unveil("/res", "r"));
|
2022-08-07 13:19:42 -03:00
|
|
|
TRY(Core::System::unveil("/tmp/user/%uid/portal/launch", "rw"));
|
2021-11-23 06:59:50 -03:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-06-01 13:16:19 -03:00
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
u32 high_score = Config::read_i32("FlappyBug"sv, "Game"sv, "HighScore"sv, 0);
|
2021-06-22 11:50:54 -03:00
|
|
|
|
2021-11-23 21:38:27 -03:00
|
|
|
auto window = TRY(GUI::Window::try_create());
|
2021-06-01 13:16:19 -03:00
|
|
|
window->resize(FlappyBug::Game::game_width, FlappyBug::Game::game_height);
|
2022-07-11 14:32:29 -03:00
|
|
|
auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-flappybug"sv));
|
2021-06-01 13:16:19 -03:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
|
|
|
|
window->set_title("Flappy Bug");
|
|
|
|
|
window->set_double_buffering_enabled(false);
|
|
|
|
|
window->set_resizable(false);
|
2021-12-23 18:29:00 -03:00
|
|
|
auto widget = TRY(window->try_set_main_widget<FlappyBug::Game>(TRY(FlappyBug::Game::Bug::construct()), TRY(FlappyBug::Game::Cloud::construct())));
|
2021-06-22 11:50:54 -03:00
|
|
|
|
2021-11-26 22:02:52 -03:00
|
|
|
widget->on_game_end = [&](u32 score) {
|
2021-06-22 11:50:54 -03:00
|
|
|
if (score <= high_score)
|
|
|
|
|
return high_score;
|
|
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
Config::write_i32("FlappyBug"sv, "Game"sv, "HighScore"sv, score);
|
2021-06-22 11:50:54 -03:00
|
|
|
high_score = score;
|
|
|
|
|
|
|
|
|
|
return high_score;
|
|
|
|
|
};
|
2021-06-01 13:16:19 -03:00
|
|
|
|
2021-11-26 22:02:52 -03:00
|
|
|
auto game_menu = TRY(window->try_add_menu("&Game"));
|
|
|
|
|
TRY(game_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
2021-06-01 13:16:19 -03:00
|
|
|
GUI::Application::the()->quit();
|
2021-11-26 22:02:52 -03:00
|
|
|
})));
|
2021-06-01 13:16:19 -03:00
|
|
|
|
2021-11-26 22:02:52 -03:00
|
|
|
auto help_menu = TRY(window->try_add_menu("&Help"));
|
2022-01-09 16:25:38 -03:00
|
|
|
TRY(help_menu->try_add_action(GUI::CommonActions::make_help_action([](auto&) {
|
|
|
|
|
Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man6/FlappyBug.md"), "/bin/Help");
|
|
|
|
|
})));
|
2021-11-26 22:02:52 -03:00
|
|
|
TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Flappy Bug", app_icon, window)));
|
2021-06-01 13:16:19 -03:00
|
|
|
|
2021-06-22 11:50:54 -03:00
|
|
|
window->show();
|
2021-06-01 13:16:19 -03:00
|
|
|
|
|
|
|
|
return app->exec();
|
|
|
|
|
}
|