main.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2021, Mim Hufford <mim@hotmail.co.uk>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Game.h"
  7. #include <AK/URL.h>
  8. #include <LibConfig/Client.h>
  9. #include <LibCore/System.h>
  10. #include <LibDesktop/Launcher.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/Icon.h>
  13. #include <LibGUI/Menu.h>
  14. #include <LibGUI/Menubar.h>
  15. #include <LibGUI/MessageBox.h>
  16. #include <LibGUI/Window.h>
  17. #include <LibMain/Main.h>
  18. ErrorOr<int> serenity_main(Main::Arguments arguments)
  19. {
  20. TRY(Core::System::pledge("stdio rpath recvfd sendfd unix"));
  21. auto app = TRY(GUI::Application::create(arguments));
  22. Config::pledge_domain("FlappyBug");
  23. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man6/FlappyBug.md") }));
  24. TRY(Desktop::Launcher::seal_allowlist());
  25. TRY(Core::System::pledge("stdio rpath recvfd sendfd"));
  26. TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw"));
  27. TRY(Core::System::unveil("/res", "r"));
  28. TRY(Core::System::unveil(nullptr, nullptr));
  29. u32 high_score = Config::read_i32("FlappyBug"sv, "Game"sv, "HighScore"sv, 0);
  30. auto window = TRY(GUI::Window::try_create());
  31. window->resize(FlappyBug::Game::game_width, FlappyBug::Game::game_height);
  32. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-flappybug"sv));
  33. window->set_icon(app_icon.bitmap_for_size(16));
  34. window->set_title("Flappy Bug");
  35. window->set_double_buffering_enabled(false);
  36. window->set_resizable(false);
  37. auto widget = TRY(window->set_main_widget<FlappyBug::Game>(TRY(FlappyBug::Game::Bug::construct()), TRY(FlappyBug::Game::Cloud::construct())));
  38. widget->on_game_end = [&](u32 score) {
  39. if (score <= high_score)
  40. return high_score;
  41. Config::write_i32("FlappyBug"sv, "Game"sv, "HighScore"sv, score);
  42. high_score = score;
  43. return high_score;
  44. };
  45. auto game_menu = window->add_menu("&Game"_string);
  46. game_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  47. GUI::Application::the()->quit();
  48. }));
  49. auto help_menu = window->add_menu("&Help"_string);
  50. help_menu->add_action(GUI::CommonActions::make_command_palette_action(window));
  51. help_menu->add_action(GUI::CommonActions::make_help_action([](auto&) {
  52. Desktop::Launcher::open(URL::create_with_file_scheme("/usr/share/man/man6/FlappyBug.md"), "/bin/Help");
  53. }));
  54. help_menu->add_action(GUI::CommonActions::make_about_action("Flappy Bug"_string, app_icon, window));
  55. window->show();
  56. return app->exec();
  57. }