From 4d2cf9514996c589c00827b2f31d21173fc1fa34 Mon Sep 17 00:00:00 2001 From: Carsten Teibes Date: Mon, 19 Oct 2015 03:34:55 +0200 Subject: [PATCH] Start implementing ini flag reading (#627) Reduce use of global variables for flags. --- ...pg_player_player_EasyRpgPlayerActivity.cpp | 4 +- src/game_interpreter_map.cpp | 2 +- src/graphics.cpp | 16 ++++- src/graphics.h | 11 ++++ src/output.cpp | 3 +- src/player.cpp | 65 +++++++++++++++---- src/player.h | 26 ++++++-- src/scene_battle.cpp | 2 +- src/scene_title.cpp | 23 ++++--- src/sdl_ui.cpp | 8 +-- 10 files changed, 118 insertions(+), 42 deletions(-) diff --git a/builds/android/app/src/main/jni/src/org_easyrpg_player_player_EasyRpgPlayerActivity.cpp b/builds/android/app/src/main/jni/src/org_easyrpg_player_player_EasyRpgPlayerActivity.cpp index c97781c01db..1bf7e41635a 100644 --- a/builds/android/app/src/main/jni/src/org_easyrpg_player_player_EasyRpgPlayerActivity.cpp +++ b/builds/android/app/src/main/jni/src/org_easyrpg_player_player_EasyRpgPlayerActivity.cpp @@ -33,13 +33,13 @@ extern "C" { JNIEXPORT void JNICALL Java_org_easyrpg_player_player_EasyRpgPlayerActivity_toggleFps (JNIEnv *, jclass) { - Player::fps_flag = !Player::fps_flag; + Graphics::ToggleFPS(); } JNIEXPORT void JNICALL Java_org_easyrpg_player_player_EasyRpgPlayerActivity_endGame (JNIEnv *, jclass) { - Player::exit_flag = true; + Player::RequestExit(); } #ifdef __cplusplus diff --git a/src/game_interpreter_map.cpp b/src/game_interpreter_map.cpp index ef9f12715a2..7a726d1312f 100644 --- a/src/game_interpreter_map.cpp +++ b/src/game_interpreter_map.cpp @@ -1834,7 +1834,7 @@ bool Game_Interpreter_Map::CommandExitGame(RPG::EventCommand const& com) { if (Scene::Find(Scene::GameBrowser)) { Scene::PopUntil(Scene::GameBrowser); } else { - Player::exit_flag = true; + Player::RequestExit(); } return true; } diff --git a/src/graphics.cpp b/src/graphics.cpp index 0be7caa408c..3baa76e7cfd 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -69,6 +69,10 @@ namespace Graphics { bool SortDrawableList(const Drawable* first, const Drawable* second); } +namespace { + bool show_fps = false; +} + unsigned SecondToFrame(float const second) { return(second * Graphics::framerate); } @@ -141,7 +145,7 @@ void Graphics::UpdateTitle() { } title << GAME_TITLE; - if (Player::fps_flag) { + if (show_fps) { title << " - FPS " << real_fps; } @@ -199,7 +203,7 @@ void Graphics::DrawOverlay() { #ifndef EMSCRIPTEN DisplayUi->IsFullscreen() && #endif - Player::fps_flag) { + show_fps) { std::stringstream text; text << "FPS: " << real_fps; DisplayUi->GetDisplaySurface()->TextDraw(2, 2, Color(255, 255, 255, 255), text.str()); @@ -479,3 +483,11 @@ void Graphics::Pop() { int Graphics::GetDefaultFps() { return DEFAULT_FPS; } + +void Graphics::ToggleFPS() { + show_fps = !show_fps; +} + +void Graphics::ShowFPS(bool visibility) { + show_fps = visibility; +} diff --git a/src/graphics.h b/src/graphics.h index 2cc5cfab7db..84c8afd568c 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -136,6 +136,17 @@ namespace Graphics { * @return target frame rate */ int GetDefaultFps(); + + /** + * Shows or Hides the Frames per Second display + */ + void ToggleFPS(); + + /** + * Sets the Frames per Second display visibility + * @param visibility The visibility to set to + */ + void ShowFPS(bool visibility); } #endif diff --git a/src/output.cpp b/src/output.cpp index eb16f83c19e..1348397069a 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -174,7 +174,8 @@ static void HandleErrorOutput(const std::string& err) { DisplayUi->Sleep(1); DisplayUi->ProcessEvents(); - if (Player::exit_flag) break; + if (Player::ExitRequested()) + break; Input::Update(); } diff --git a/src/player.cpp b/src/player.cpp index b2ff7436b9f..0d08d07c2b0 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -73,12 +73,9 @@ #include "version.h" namespace Player { - bool exit_flag; bool reset_flag; bool debug_flag; - bool hide_title_flag; bool window_flag; - bool fps_flag; bool battle_test_flag; int battle_test_troop_id; bool new_game_flag; @@ -104,6 +101,10 @@ namespace Player { } namespace { + bool exit_flag; + bool hide_title_flag; + std::string game_title_native; + double start_time; double next_frame; @@ -114,7 +115,6 @@ namespace { FileRequestBinding save_request_id; FileRequestBinding map_request_id; } - void Player::Init(int argc, char *argv[]) { static bool init = false; frames = 0; @@ -220,6 +220,15 @@ void Player::Init(int argc, char *argv[]) { Main_Data::Init(); + // setup global flags + exit_flag = false; + hide_title_flag = false; + no_audio_flag = false; + + ParseCommandLine(argc, argv); + + ParseIni(); + DisplayUi.reset(); if(! DisplayUi) { @@ -308,7 +317,7 @@ void Player::Update(bool update_scene) { // Normal logic update if (Input::IsTriggered(Input::TOGGLE_FPS)) { - fps_flag = !fps_flag; + Graphics::ToggleFPS(); } if (Input::IsTriggered(Input::TAKE_SCREENSHOT)) { Output::TakeScreenshot(); @@ -358,6 +367,10 @@ int Player::GetFrames() { return frames; } +void Player::RequestExit() { + exit_flag = true; +} + void Player::Exit() { #ifdef EMSCRIPTEN emscripten_cancel_main_loop(); @@ -404,10 +417,7 @@ void Player::ParseCommandLine(int argc, char *argv[]) { #else window_flag = false; #endif - fps_flag = false; debug_flag = false; - hide_title_flag = false; - exit_flag = false; reset_flag = false; battle_test_flag = false; battle_test_troop_id = 0; @@ -417,7 +427,6 @@ void Player::ParseCommandLine(int argc, char *argv[]) { party_y_position = -1; start_map_id = -1; no_rtp_flag = false; - no_audio_flag = false; std::vector args; @@ -443,7 +452,7 @@ void Player::ParseCommandLine(int argc, char *argv[]) { window_flag = true; } else if (*it == "--show-fps") { - fps_flag = true; + Graphics::ShowFPS(true); } else if (*it == "testplay" || *it == "--test-play") { debug_flag = true; @@ -594,6 +603,27 @@ void Player::ParseCommandLine(int argc, char *argv[]) { } } +static bool IniFlagEnabled(std::string flag) { + return (flag == "1" || flag == "true" || flag == "yes"); +} + +void Player::ParseIni() { + INIReader ini(FileFinder::FindDefault(INI_NAME)); + if (ini.ParseError() != -1) { + game_title_native = ini.Get("RPG_RT", "GameTitle", GAME_TITLE); + no_rtp_flag = ini.Get("RPG_RT", "FullPackageFlag", "0") == "1" ? true : no_rtp_flag; + + if (IniFlagEnabled(ini.Get("EasyRPG", "hide-title", "0"))) + hide_title_flag = true; + + if (IniFlagEnabled(ini.Get("EasyRPG", "show-fps", "0"))) + Graphics::ShowFPS(true); + + if (IniFlagEnabled(ini.Get("EasyRPG", "disable-audio", "0"))) + no_audio_flag = true; + } +} + static void OnSystemFileReady(FileRequestResult* result) { Game_System::SetSystemName(result->file); } @@ -614,8 +644,11 @@ void Player::CreateGameObjects() { Output::Debug("Using %s as Save directory", save_path.c_str()); } - LoadDatabase(); + // setup correct game title + + game_title = ReaderUtil::Recode(game_title_native, encoding); + LoadDatabase(); std::string ini_file = FileFinder::FindDefault(INI_NAME); INIReader ini(ini_file); @@ -910,6 +943,14 @@ bool Player::IsRPG2k3E() { return (engine & EngineRpg2k3E) == EngineRpg2k3E; } +bool Player::IsTitleHidden() { + return hide_title_flag; +} + +bool Player::ExitRequested() { + return exit_flag; +} + #if (defined(_WIN32) && defined(NDEBUG) && defined(WINVER) && WINVER >= 0x0600) // Minidump code for Windows // Original Author: Oleg Starodumov (www.debuginfo.com) @@ -1028,6 +1069,4 @@ static void InitMiniDumpWriter() } } } - - #endif diff --git a/src/player.h b/src/player.h index ca9d13a0af2..b2b9ce9b6b5 100644 --- a/src/player.h +++ b/src/player.h @@ -83,6 +83,11 @@ namespace Player { */ void FrameReset(); + /** + * Initiates a clean exit + */ + void RequestExit(); + /** * Exits EasyRPG Player. */ @@ -94,7 +99,12 @@ namespace Player { void ParseCommandLine(int argc, char *argv[]); /** - * Initializes all game objects + * Parses settings from ini file. + */ + void ParseIni(); + + /** + * (Re)Initializes all game objects */ void CreateGameObjects(); @@ -154,8 +164,15 @@ namespace Player { /** Output program usage information on stdout */ void PrintUsage(); - /** Exit flag, if true will exit application on next Player::Update. */ - extern bool exit_flag; + /** + * @return If title scene will run without image and music. + */ + bool IsTitleHidden(); + + /** + * @return If will exit application on next Player::Update. + */ + bool ExitRequested(); /** Reset flag, if true will restart game on next Player::Update. */ extern bool reset_flag; @@ -163,9 +180,6 @@ namespace Player { /** Debug flag, if true will run game in debug mode. */ extern bool debug_flag; - /** Hide Title flag, if true title scene will run without image and music. */ - extern bool hide_title_flag; - /** Window flag, if true will run in window mode instead of full screen. */ extern bool window_flag; diff --git a/src/scene_battle.cpp b/src/scene_battle.cpp index e386bdfc341..02cd5a99178 100644 --- a/src/scene_battle.cpp +++ b/src/scene_battle.cpp @@ -98,7 +98,7 @@ void Scene_Battle::TransitionIn() { } void Scene_Battle::TransitionOut() { - if (Player::exit_flag) { + if (Player::ExitRequested()) { Scene::TransitionOut(); } else { diff --git a/src/scene_title.cpp b/src/scene_title.cpp index d85898913e2..c525be9bd27 100644 --- a/src/scene_title.cpp +++ b/src/scene_title.cpp @@ -41,7 +41,7 @@ Scene_Title::Scene_Title() { void Scene_Title::Start() { // Skip background image and music if not used if (Data::system.show_title && !Player::new_game_flag && - !Player::battle_test_flag && !Player::hide_title_flag) { + !Player::battle_test_flag && !Player::IsTitleHidden()) { CreateTitleGraphic(); PlayTitleMusic(); } @@ -60,14 +60,13 @@ void Scene_Title::Continue() { } void Scene_Title::TransitionIn() { - if (Player::battle_test_flag || !Data::system.show_title || Player::new_game_flag) - return; - - Graphics::Transition(Graphics::TransitionErase, 1, true); - if (!Player::hide_title_flag) { - Graphics::Transition(Graphics::TransitionFadeIn, 32); - } else { - Graphics::Transition(Graphics::TransitionFadeIn, 6); + if (!Player::battle_test_flag) { + Graphics::Transition(Graphics::TransitionErase, 1, true); + if (!Player::IsTitleHidden()) { + Graphics::Transition(Graphics::TransitionFadeIn, 32); + } else { + Graphics::Transition(Graphics::TransitionFadeIn, 6); + } } } @@ -91,7 +90,7 @@ void Scene_Title::Update() { if (!Data::system.show_title || Player::new_game_flag) { Player::SetupPlayerSpawn(); Scene::Push(std::make_shared()); - if (Player::debug_flag && Player::hide_title_flag) { + if (Player::debug_flag && Player::IsTitleHidden()) { Scene::Push(std::make_shared()); } return; @@ -132,7 +131,7 @@ void Scene_Title::CreateCommandWindow() { options.push_back(Data::terms.exit_game); command_window.reset(new Window_Command(options)); - if (!Player::hide_title_flag) { + if (!Player::IsTitleHidden()) { command_window->SetX(SCREEN_TARGET_WIDTH / 2 - command_window->GetWidth() / 2); command_window->SetY(SCREEN_TARGET_HEIGHT * 53 / 60 - command_window->GetHeight()); } else { @@ -148,7 +147,7 @@ void Scene_Title::CreateCommandWindow() { } // Set the number of frames for the opening animation to last - if (!Player::hide_title_flag) { + if (!Player::IsTitleHidden()) { command_window->SetOpenAnimation(8); } diff --git a/src/sdl_ui.cpp b/src/sdl_ui.cpp index 044fbc89577..a73cc52808a 100644 --- a/src/sdl_ui.cpp +++ b/src/sdl_ui.cpp @@ -557,7 +557,7 @@ void SdlUi::ProcessEvents() { while (SDL_PollEvent(&evnt)) { ProcessEvent(evnt); - if (Player::exit_flag) + if (Player::ExitRequested()) break; } } @@ -625,7 +625,7 @@ void SdlUi::ProcessEvent(SDL_Event &evnt) { return; case SDL_QUIT: - Player::exit_flag = true; + Player::RequestExit(); return; case SDL_KEYDOWN: @@ -731,7 +731,7 @@ void SdlUi::ProcessKeyDownEvent(SDL_Event &evnt) { case SDLK_F4: // Close program on LeftAlt+F4 if (evnt.key.keysym.mod & KMOD_LALT) { - Player::exit_flag = true; + Player::RequestExit(); return; } @@ -1228,7 +1228,7 @@ int FilterUntilFocus(const SDL_Event* evnt) { switch (evnt->type) { case SDL_QUIT: - Player::exit_flag = true; + Player::RequestExit(); return 1; case SDL_WINDOWEVENT: