Moved externs for command-line args into a Settings singleton.

This commit is contained in:
Aloshi 2013-06-17 14:01:03 -05:00
parent 19eb1c412f
commit e8465baaba
8 changed files with 106 additions and 34 deletions

View file

@ -125,6 +125,7 @@ set(ES_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/src/MathExp.h ${CMAKE_CURRENT_SOURCE_DIR}/src/MathExp.h
${CMAKE_CURRENT_SOURCE_DIR}/src/platform.h ${CMAKE_CURRENT_SOURCE_DIR}/src/platform.h
${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer.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/Sound.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SystemData.h ${CMAKE_CURRENT_SOURCE_DIR}/src/SystemData.h
${CMAKE_CURRENT_SOURCE_DIR}/src/Vector2.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/platform.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer_draw_gl.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer_draw_gl.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer_init.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/Sound.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SystemData.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/SystemData.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/VolumeControl.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/VolumeControl.cpp

View file

@ -15,8 +15,7 @@
#include "ImageIO.h" #include "ImageIO.h"
#include "../data/Resources.h" #include "../data/Resources.h"
#include "EmulationStation.h" #include "EmulationStation.h"
#include "Settings.h"
extern bool WINDOWED;
namespace Renderer namespace Renderer
{ {
@ -72,7 +71,7 @@ namespace Renderer
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
//SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); //vsync //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) if(sdlScreen == NULL)
{ {

48
src/Settings.cpp Normal file
View file

@ -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; }

35
src/Settings.h Normal file
View file

@ -0,0 +1,35 @@
#ifndef _SETTINGS_H_
#define _SETTINGS_H_
#include <string>
#include <map>
//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<std::string, bool> mBoolMap;
std::map<std::string, int> mIntMap;
};
#endif

View file

@ -11,14 +11,12 @@
#include "Log.h" #include "Log.h"
#include "InputManager.h" #include "InputManager.h"
#include <iostream> #include <iostream>
#include "Settings.h"
std::vector<SystemData*> SystemData::sSystemVector; std::vector<SystemData*> SystemData::sSystemVector;
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
extern bool PARSEGAMELISTONLY;
extern bool IGNOREGAMELIST;
std::string SystemData::getStartPath() { return mStartPath; } std::string SystemData::getStartPath() { return mStartPath; }
std::string SystemData::getExtension() { return mSearchExtension; } 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"); mRootFolder = new FolderData(this, mStartPath, "Search Root");
if(!PARSEGAMELISTONLY) if(!Settings::getInstance()->getBool("PARSEGAMELISTONLY"))
populateFolder(mRootFolder); populateFolder(mRootFolder);
if(!IGNOREGAMELIST) if(!Settings::getInstance()->getBool("IGNOREGAMELIST"))
parseGamelist(this); parseGamelist(this);
mRootFolder->sort(); mRootFolder->sort();
@ -322,8 +320,8 @@ FolderData* SystemData::getRootFolder()
return mRootFolder; return mRootFolder;
} }
std::string SystemData::getGamelistPath(){ std::string SystemData::getGamelistPath()
{
std::string filePath; std::string filePath;
filePath = mRootFolder->getPath() + "/gamelist.xml"; filePath = mRootFolder->getPath() + "/gamelist.xml";

View file

@ -5,6 +5,7 @@
#include "GuiFastSelect.h" #include "GuiFastSelect.h"
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include "../Log.h" #include "../Log.h"
#include "../Settings.h"
Vector2i GuiGameList::getImagePos() 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) GuiGameList* GuiGameList::create(Window* window)
{ {
bool detailed = false; bool detailed = false;
if(!IGNOREGAMELIST) if(!Settings::getInstance()->getBool("IGNOREGAMELIST"))
{ {
for(unsigned int i = 0; i < SystemData::sSystemVector.size(); i++) for(unsigned int i = 0; i < SystemData::sSystemVector.size(); i++)
{ {

View file

@ -4,9 +4,7 @@
#include "../Log.h" #include "../Log.h"
#include "../SystemData.h" #include "../SystemData.h"
#include "GuiGameList.h" #include "GuiGameList.h"
#include "../Settings.h"
//defined in main.cpp
extern bool DONTSHOWEXIT;
GuiMenu::GuiMenu(Window* window, GuiGameList* parent) : GuiComponent(window) GuiMenu::GuiMenu(Window* window, GuiGameList* parent) : GuiComponent(window)
{ {
@ -76,7 +74,7 @@ void GuiMenu::populateList()
mList->addObject("Reload", "es_reload", 0x0000FFFF); 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() mList->addObject("Exit", "exit", 0xFF0000FF); //a special case; pushes an SDL quit event to the event stack instead of being called by system()
} }

View file

@ -14,6 +14,7 @@
#include "Log.h" #include "Log.h"
#include "Window.h" #include "Window.h"
#include "EmulationStation.h" #include "EmulationStation.h"
#include "Settings.h"
#ifdef _RPI_ #ifdef _RPI_
#include <bcm_host.h> #include <bcm_host.h>
@ -21,15 +22,6 @@
#include <sstream> #include <sstream>
//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; namespace fs = boost::filesystem;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
@ -50,27 +42,27 @@ int main(int argc, char* argv[])
i++; //skip the argument value i++; //skip the argument value
}else if(strcmp(argv[i], "--gamelist-only") == 0) }else if(strcmp(argv[i], "--gamelist-only") == 0)
{ {
PARSEGAMELISTONLY = true; Settings::getInstance()->setBool("PARSEGAMELISTONLY", true);
}else if(strcmp(argv[i], "--ignore-gamelist") == 0) }else if(strcmp(argv[i], "--ignore-gamelist") == 0)
{ {
IGNOREGAMELIST = true; Settings::getInstance()->setBool("IGNOREGAMELIST", true);
}else if(strcmp(argv[i], "--draw-framerate") == 0) }else if(strcmp(argv[i], "--draw-framerate") == 0)
{ {
DRAWFRAMERATE = true; Settings::getInstance()->setBool("DRAWFRAMERATE", true);
}else if(strcmp(argv[i], "--no-exit") == 0) }else if(strcmp(argv[i], "--no-exit") == 0)
{ {
DONTSHOWEXIT = true; Settings::getInstance()->setBool("DONTSHOWEXIT", true);
}else if(strcmp(argv[i], "--debug") == 0) }else if(strcmp(argv[i], "--debug") == 0)
{ {
DEBUG = true; Settings::getInstance()->setBool("DEBUG", true);
Log::setReportingLevel(LogDebug); Log::setReportingLevel(LogDebug);
}else if(strcmp(argv[i], "--dimtime") == 0) }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 i++; //skip the argument value
}else if(strcmp(argv[i], "--windowed") == 0) }else if(strcmp(argv[i], "--windowed") == 0)
{ {
WINDOWED = true; Settings::getInstance()->setBool("WINDOWED", true);
}else if(strcmp(argv[i], "--help") == 0) }else if(strcmp(argv[i], "--help") == 0)
{ {
std::cout << "EmulationStation, a graphical front-end for ROM browsing.\n"; 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.update(deltaTime);
window.render(); window.render();
if(DRAWFRAMERATE) if(Settings::getInstance()->getBool("DRAWFRAMERATE"))
{ {
static int timeElapsed = 0; static int timeElapsed = 0;
static int nrOfFrames = 0; static int nrOfFrames = 0;
@ -224,7 +216,7 @@ int main(int argc, char* argv[])
//sleeping entails setting a flag to start skipping frames //sleeping entails setting a flag to start skipping frames
//and initially drawing a black semi-transparent rect to dim the screen //and initially drawing a black semi-transparent rect to dim the screen
timeSinceLastEvent += deltaTime; timeSinceLastEvent += deltaTime;
if(timeSinceLastEvent >= DIMTIME && DIMTIME != 0) if(timeSinceLastEvent >= (unsigned int)Settings::getInstance()->getInt("DIMTIME") && Settings::getInstance()->getInt("DIMTIME") != 0)
{ {
sleeping = true; sleeping = true;
timeSinceLastEvent = 0; timeSinceLastEvent = 0;