From e8465baaba04afaa554980b47cbab99e0bf37c77 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Mon, 17 Jun 2013 14:01:03 -0500 Subject: [PATCH] Moved externs for command-line args into a Settings singleton. --- CMakeLists.txt | 2 ++ src/Renderer_init_sdlgl.cpp | 5 ++-- src/Settings.cpp | 48 ++++++++++++++++++++++++++++++++++ src/Settings.h | 35 +++++++++++++++++++++++++ src/SystemData.cpp | 12 ++++----- src/components/GuiGameList.cpp | 4 +-- src/components/GuiMenu.cpp | 6 ++--- src/main.cpp | 28 +++++++------------- 8 files changed, 106 insertions(+), 34 deletions(-) create mode 100644 src/Settings.cpp create mode 100644 src/Settings.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 64a542eb1..08026db09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,6 +125,7 @@ set(ES_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/src/MathExp.h ${CMAKE_CURRENT_SOURCE_DIR}/src/platform.h ${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/Settings.h ${CMAKE_CURRENT_SOURCE_DIR}/src/Sound.h ${CMAKE_CURRENT_SOURCE_DIR}/src/SystemData.h ${CMAKE_CURRENT_SOURCE_DIR}/src/Vector2.h @@ -161,6 +162,7 @@ set(ES_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/platform.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer_draw_gl.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer_init.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/Settings.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Sound.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/SystemData.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/VolumeControl.cpp diff --git a/src/Renderer_init_sdlgl.cpp b/src/Renderer_init_sdlgl.cpp index d4d28a061..b2aa045b7 100644 --- a/src/Renderer_init_sdlgl.cpp +++ b/src/Renderer_init_sdlgl.cpp @@ -15,8 +15,7 @@ #include "ImageIO.h" #include "../data/Resources.h" #include "EmulationStation.h" - -extern bool WINDOWED; +#include "Settings.h" namespace Renderer { @@ -72,7 +71,7 @@ namespace Renderer SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); //vsync - sdlScreen = SDL_SetVideoMode(display_width, display_height, 16, SDL_OPENGL | (WINDOWED ? 0 : SDL_FULLSCREEN)); + sdlScreen = SDL_SetVideoMode(display_width, display_height, 16, SDL_OPENGL | (Settings::getInstance()->getBool("WINDOWED") ? 0 : SDL_FULLSCREEN)); if(sdlScreen == NULL) { diff --git a/src/Settings.cpp b/src/Settings.cpp new file mode 100644 index 000000000..37cf61c30 --- /dev/null +++ b/src/Settings.cpp @@ -0,0 +1,48 @@ +#include "Settings.h" +#include "Log.h" + +Settings* Settings::sInstance = NULL; + +Settings::Settings() +{ + setDefaults(); +} + +Settings* Settings::getInstance() +{ + if(sInstance == NULL) + sInstance = new Settings(); + + return sInstance; +} + +void Settings::setDefaults() +{ + mBoolMap.clear(); + mIntMap.clear(); + + mBoolMap["PARSEGAMELISTONLY"] = false; + mBoolMap["IGNOREGAMELIST"] = false; + mBoolMap["DRAWFRAMERATE"] = false; + mBoolMap["DONTSHOWEXIT"] = false; + mBoolMap["DEBUG"] = false; + mBoolMap["WINDOWED"] = false; + + mIntMap["DIMTIME"] = 30*1000; +} + +//Print a warning message if the setting we're trying to get doesn't already exist in the map, then return the value in the map. +#define SETTINGS_GET(type, mapName, methodName) type Settings::##methodName##(const std::string& name) \ +{ \ + if(mapName.find(name) == mapName.end()) \ + { \ + LOG(LogError) << "Tried to use unset setting " << name << "!"; \ + } \ + return mapName[name]; \ +} + +SETTINGS_GET(bool, mBoolMap, getBool); +SETTINGS_GET(int, mIntMap, getInt); + +void Settings::setBool(const std::string& name, bool value) { mBoolMap[name] = value; } +void Settings::setInt(const std::string& name, int value) { mIntMap[name] = value; } diff --git a/src/Settings.h b/src/Settings.h new file mode 100644 index 000000000..5babf70c7 --- /dev/null +++ b/src/Settings.h @@ -0,0 +1,35 @@ +#ifndef _SETTINGS_H_ +#define _SETTINGS_H_ + +#include +#include + +//This is a singleton for storing settings. +class Settings +{ +public: + static Settings* getInstance(); + + void loadFile(const std::string& path); + void saveFile(const std::string& path); + + //You will get a warning if you try a get on a key that is not already present. + bool getBool(const std::string& name); + int getInt(const std::string& name); + + void setBool(const std::string& name, bool value); + void setInt(const std::string& name, int value); + +private: + static Settings* sInstance; + + Settings(); + + //Clear everything and load default values. + void setDefaults(); + + std::map mBoolMap; + std::map mIntMap; +}; + +#endif diff --git a/src/SystemData.cpp b/src/SystemData.cpp index e673041db..2e983e5e4 100644 --- a/src/SystemData.cpp +++ b/src/SystemData.cpp @@ -11,14 +11,12 @@ #include "Log.h" #include "InputManager.h" #include +#include "Settings.h" std::vector SystemData::sSystemVector; namespace fs = boost::filesystem; -extern bool PARSEGAMELISTONLY; -extern bool IGNOREGAMELIST; - std::string SystemData::getStartPath() { return mStartPath; } std::string SystemData::getExtension() { return mSearchExtension; } @@ -41,10 +39,10 @@ SystemData::SystemData(std::string name, std::string descName, std::string start mRootFolder = new FolderData(this, mStartPath, "Search Root"); - if(!PARSEGAMELISTONLY) + if(!Settings::getInstance()->getBool("PARSEGAMELISTONLY")) populateFolder(mRootFolder); - if(!IGNOREGAMELIST) + if(!Settings::getInstance()->getBool("IGNOREGAMELIST")) parseGamelist(this); mRootFolder->sort(); @@ -322,8 +320,8 @@ FolderData* SystemData::getRootFolder() return mRootFolder; } -std::string SystemData::getGamelistPath(){ - +std::string SystemData::getGamelistPath() +{ std::string filePath; filePath = mRootFolder->getPath() + "/gamelist.xml"; diff --git a/src/components/GuiGameList.cpp b/src/components/GuiGameList.cpp index aad66bc10..6d6986017 100644 --- a/src/components/GuiGameList.cpp +++ b/src/components/GuiGameList.cpp @@ -5,6 +5,7 @@ #include "GuiFastSelect.h" #include #include "../Log.h" +#include "../Settings.h" Vector2i GuiGameList::getImagePos() { @@ -344,12 +345,11 @@ void GuiGameList::init() } } -extern bool IGNOREGAMELIST; //defined in main.cpp (as a command line argument) GuiGameList* GuiGameList::create(Window* window) { bool detailed = false; - if(!IGNOREGAMELIST) + if(!Settings::getInstance()->getBool("IGNOREGAMELIST")) { for(unsigned int i = 0; i < SystemData::sSystemVector.size(); i++) { diff --git a/src/components/GuiMenu.cpp b/src/components/GuiMenu.cpp index c3f1708ff..a0bd43fa6 100644 --- a/src/components/GuiMenu.cpp +++ b/src/components/GuiMenu.cpp @@ -4,9 +4,7 @@ #include "../Log.h" #include "../SystemData.h" #include "GuiGameList.h" - -//defined in main.cpp -extern bool DONTSHOWEXIT; +#include "../Settings.h" GuiMenu::GuiMenu(Window* window, GuiGameList* parent) : GuiComponent(window) { @@ -76,7 +74,7 @@ void GuiMenu::populateList() mList->addObject("Reload", "es_reload", 0x0000FFFF); - if(!DONTSHOWEXIT) + if(!Settings::getInstance()->getBool("DONTSHOWEXIT")) mList->addObject("Exit", "exit", 0xFF0000FF); //a special case; pushes an SDL quit event to the event stack instead of being called by system() } diff --git a/src/main.cpp b/src/main.cpp index 092ce19b9..d633b960b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,7 @@ #include "Log.h" #include "Window.h" #include "EmulationStation.h" +#include "Settings.h" #ifdef _RPI_ #include @@ -21,15 +22,6 @@ #include -//these can be set by command-line arguments -bool PARSEGAMELISTONLY = false; -bool IGNOREGAMELIST = false; -bool DRAWFRAMERATE = false; -bool DONTSHOWEXIT = false; -bool DEBUG = false; -bool WINDOWED = false; -unsigned int DIMTIME = 30*1000; - namespace fs = boost::filesystem; int main(int argc, char* argv[]) @@ -50,27 +42,27 @@ int main(int argc, char* argv[]) i++; //skip the argument value }else if(strcmp(argv[i], "--gamelist-only") == 0) { - PARSEGAMELISTONLY = true; + Settings::getInstance()->setBool("PARSEGAMELISTONLY", true); }else if(strcmp(argv[i], "--ignore-gamelist") == 0) { - IGNOREGAMELIST = true; + Settings::getInstance()->setBool("IGNOREGAMELIST", true); }else if(strcmp(argv[i], "--draw-framerate") == 0) { - DRAWFRAMERATE = true; + Settings::getInstance()->setBool("DRAWFRAMERATE", true); }else if(strcmp(argv[i], "--no-exit") == 0) { - DONTSHOWEXIT = true; + Settings::getInstance()->setBool("DONTSHOWEXIT", true); }else if(strcmp(argv[i], "--debug") == 0) { - DEBUG = true; + Settings::getInstance()->setBool("DEBUG", true); Log::setReportingLevel(LogDebug); }else if(strcmp(argv[i], "--dimtime") == 0) { - DIMTIME = atoi(argv[i + 1]) * 1000; + Settings::getInstance()->setInt("DIMTIME", atoi(argv[i + 1]) * 1000); i++; //skip the argument value }else if(strcmp(argv[i], "--windowed") == 0) { - WINDOWED = true; + Settings::getInstance()->setBool("WINDOWED", true); }else if(strcmp(argv[i], "--help") == 0) { std::cout << "EmulationStation, a graphical front-end for ROM browsing.\n"; @@ -200,7 +192,7 @@ int main(int argc, char* argv[]) window.update(deltaTime); window.render(); - if(DRAWFRAMERATE) + if(Settings::getInstance()->getBool("DRAWFRAMERATE")) { static int timeElapsed = 0; static int nrOfFrames = 0; @@ -224,7 +216,7 @@ int main(int argc, char* argv[]) //sleeping entails setting a flag to start skipping frames //and initially drawing a black semi-transparent rect to dim the screen timeSinceLastEvent += deltaTime; - if(timeSinceLastEvent >= DIMTIME && DIMTIME != 0) + if(timeSinceLastEvent >= (unsigned int)Settings::getInstance()->getInt("DIMTIME") && Settings::getInstance()->getInt("DIMTIME") != 0) { sleeping = true; timeSinceLastEvent = 0;