Skip to content

Commit

Permalink
Start implementing ini flag reading (EasyRPG#627)
Browse files Browse the repository at this point in the history
Reduce use of global variables for flags.
  • Loading branch information
carstene1ns authored and Ghabry committed May 28, 2016
1 parent ab903ef commit 4d2cf95
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/game_interpreter_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 14 additions & 2 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -141,7 +145,7 @@ void Graphics::UpdateTitle() {
}
title << GAME_TITLE;

if (Player::fps_flag) {
if (show_fps) {
title << " - FPS " << real_fps;
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}
11 changes: 11 additions & 0 deletions src/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
65 changes: 52 additions & 13 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -358,6 +367,10 @@ int Player::GetFrames() {
return frames;
}

void Player::RequestExit() {
exit_flag = true;
}

void Player::Exit() {
#ifdef EMSCRIPTEN
emscripten_cancel_main_loop();
Expand Down Expand Up @@ -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;
Expand All @@ -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<std::string> args;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1028,6 +1069,4 @@ static void InitMiniDumpWriter()
}
}
}


#endif
26 changes: 20 additions & 6 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ namespace Player {
*/
void FrameReset();

/**
* Initiates a clean exit
*/
void RequestExit();

/**
* Exits EasyRPG Player.
*/
Expand All @@ -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();

Expand Down Expand Up @@ -154,18 +164,22 @@ 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;

/** 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;

Expand Down
2 changes: 1 addition & 1 deletion src/scene_battle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void Scene_Battle::TransitionIn() {
}

void Scene_Battle::TransitionOut() {
if (Player::exit_flag) {
if (Player::ExitRequested()) {
Scene::TransitionOut();
}
else {
Expand Down
23 changes: 11 additions & 12 deletions src/scene_title.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
}
}
}

Expand All @@ -91,7 +90,7 @@ void Scene_Title::Update() {
if (!Data::system.show_title || Player::new_game_flag) {
Player::SetupPlayerSpawn();
Scene::Push(std::make_shared<Scene_Map>());
if (Player::debug_flag && Player::hide_title_flag) {
if (Player::debug_flag && Player::IsTitleHidden()) {
Scene::Push(std::make_shared<Scene_Load>());
}
return;
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
Loading

0 comments on commit 4d2cf95

Please sign in to comment.