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 committed Oct 19, 2015
1 parent dcd2cc2 commit d834fa8
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 36 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
16 changes: 14 additions & 2 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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 @@ -147,7 +151,7 @@ void Graphics::UpdateTitle() {
std::stringstream title;
title << Player::game_title;

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

Expand Down Expand Up @@ -201,7 +205,7 @@ void Graphics::DrawFrame() {
}

void Graphics::DrawOverlay() {
if (DisplayUi->IsFullscreen() && Player::fps_flag) {
if (DisplayUi->IsFullscreen() && 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 @@ -476,3 +480,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 @@ -137,6 +137,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 @@ -143,7 +143,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
67 changes: 50 additions & 17 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@
#endif

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 @@ -96,6 +93,12 @@ namespace Player {
#endif
}

namespace {
bool exit_flag;
bool hide_title_flag;
std::string game_title_native;
}

void Player::Init(int argc, char *argv[]) {
static bool init = false;
frames = 0;
Expand Down Expand Up @@ -136,6 +139,11 @@ void Player::Init(int argc, char *argv[]) {

srand(time(NULL));

// setup global flags
exit_flag = false;
hide_title_flag = false;
no_audio_flag = false;

ParseCommandLine(argc, argv);

if (Main_Data::project_path.empty()) {
Expand All @@ -145,6 +153,8 @@ void Player::Init(int argc, char *argv[]) {

FileFinder::Init();

ParseIni();

DisplayUi.reset();

if(! DisplayUi) {
Expand Down Expand Up @@ -228,7 +238,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 @@ -275,6 +285,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 @@ -304,10 +318,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 @@ -317,7 +328,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 @@ -339,7 +349,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 @@ -474,6 +484,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 @@ -489,12 +520,8 @@ void Player::CreateGameObjects() {

LoadDatabase();

INIReader ini(FileFinder::FindDefault(INI_NAME));
if (ini.ParseError() != -1) {
std::string title = ini.Get("RPG_RT", "GameTitle", GAME_TITLE);
game_title = ReaderUtil::Recode(title, encoding);
no_rtp_flag = ini.Get("RPG_RT", "FullPackageFlag", "0") == "1"? true : no_rtp_flag;
}
// setup correct game title
game_title = ReaderUtil::Recode(game_title_native, encoding);

Output::Debug("Loading game %s", Player::game_title.c_str());

Expand Down Expand Up @@ -766,6 +793,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 @@ -884,6 +919,4 @@ static void InitMiniDumpWriter()
}
}
}


#endif
24 changes: 19 additions & 5 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 @@ -93,6 +98,11 @@ namespace Player {
*/
void ParseCommandLine(int argc, char *argv[]);

/**
* Parses settings from ini file.
*/
void ParseIni();

/**
* (Re)Initializes all game objects
*/
Expand Down Expand Up @@ -148,18 +158,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 @@ -109,7 +109,7 @@ void Scene_Battle::TransitionIn() {
}

void Scene_Battle::TransitionOut() {
if (Player::exit_flag) {
if (Player::ExitRequested()) {
Scene::TransitionOut();
}
else {
Expand Down
8 changes: 4 additions & 4 deletions src/scene_title.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Scene_Title::Scene_Title() {
}

void Scene_Title::Start() {
if (!Player::battle_test_flag && !Player::hide_title_flag) {
if (!Player::battle_test_flag && !Player::IsTitleHidden()) {
CreateTitleGraphic();
PlayTitleMusic();
}
Expand All @@ -75,7 +75,7 @@ void Scene_Title::Continue() {
void Scene_Title::TransitionIn() {
if (!Player::battle_test_flag) {
Graphics::Transition(Graphics::TransitionErase, 1, true);
if (!Player::hide_title_flag) {
if (!Player::IsTitleHidden()) {
Graphics::Transition(Graphics::TransitionFadeIn, 32);
} else {
Graphics::Transition(Graphics::TransitionFadeIn, 6);
Expand Down Expand Up @@ -158,7 +158,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 @@ -174,7 +174,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
8 changes: 4 additions & 4 deletions src/sdl_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ void SdlUi::ProcessEvents() {
while (SDL_PollEvent(&evnt)) {
ProcessEvent(evnt);

if (Player::exit_flag)
if (Player::ExitRequested())
break;
}
}
Expand Down Expand Up @@ -594,7 +594,7 @@ void SdlUi::ProcessEvent(SDL_Event &evnt) {
return;

case SDL_QUIT:
Player::exit_flag = true;
Player::RequestExit();
return;

case SDL_KEYDOWN:
Expand Down Expand Up @@ -709,7 +709,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;
}

Expand Down Expand Up @@ -1176,7 +1176,7 @@ int FilterUntilFocus(const SDL_Event* evnt) {

switch (evnt->type) {
case SDL_QUIT:
Player::exit_flag = true;
Player::RequestExit();
return 1;

case SDL_WINDOWEVENT:
Expand Down

1 comment on commit d834fa8

@Ghabry
Copy link

@Ghabry Ghabry commented on d834fa8 Mar 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting branch 👍

Please sign in to comment.