mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:17:44 +00:00
FlappyBug: Persist high score to disk
Previously, the high score was only in-memory, so only persisted for as long as the FlappyBug window was open.
This commit is contained in:
parent
25c53e35e7
commit
1a2053781f
3 changed files with 30 additions and 5 deletions
|
@ -5,10 +5,12 @@
|
|||
*/
|
||||
|
||||
#include "Game.h"
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/Menubar.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -20,6 +22,7 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
auto app = GUI::Application::construct(argc, argv);
|
||||
auto config = Core::ConfigFile::get_for_app("FlappyBug");
|
||||
|
||||
if (pledge("stdio rpath wpath cpath recvfd sendfd", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
|
@ -31,11 +34,18 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (unveil(config->filename().characters(), "crw") < 0) {
|
||||
perror("unveil");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (unveil(nullptr, nullptr) < 0) {
|
||||
perror("unveil");
|
||||
return 1;
|
||||
}
|
||||
|
||||
u32 high_score = static_cast<u32>(config->read_num_entry("Game", "HighScore", 0));
|
||||
|
||||
auto window = GUI::Window::construct();
|
||||
window->resize(FlappyBug::Game::game_width, FlappyBug::Game::game_height);
|
||||
auto app_icon = GUI::Icon::default_icon("app-flappybug");
|
||||
|
@ -43,8 +53,20 @@ int main(int argc, char** argv)
|
|||
window->set_title("Flappy Bug");
|
||||
window->set_double_buffering_enabled(false);
|
||||
window->set_resizable(false);
|
||||
window->set_main_widget<FlappyBug::Game>();
|
||||
window->show();
|
||||
auto& widget = window->set_main_widget<FlappyBug::Game>();
|
||||
|
||||
widget.on_game_end = [&](u32 score) {
|
||||
if (score <= high_score)
|
||||
return high_score;
|
||||
|
||||
config->write_num_entry("Game", "HighScore", static_cast<int>(score));
|
||||
high_score = score;
|
||||
|
||||
if (!config->sync())
|
||||
GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error);
|
||||
|
||||
return high_score;
|
||||
};
|
||||
|
||||
auto menubar = GUI::Menubar::construct();
|
||||
|
||||
|
@ -57,6 +79,7 @@ int main(int argc, char** argv)
|
|||
help_menu.add_action(GUI::CommonActions::make_about_action("Flappy Bug", app_icon, window));
|
||||
|
||||
window->set_menubar(move(menubar));
|
||||
window->show();
|
||||
|
||||
return app->exec();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue