Refactor some of the startup code.

This commit is contained in:
Aloshi 2013-07-09 05:37:37 -05:00
parent 40dc9abdf8
commit 77e9f2d150
6 changed files with 110 additions and 75 deletions

View file

@ -182,14 +182,21 @@ std::string SystemData::getDescName()
} }
//creates systems from information located in a config file //creates systems from information located in a config file
void SystemData::loadConfig() bool SystemData::loadConfig(const std::string& path, bool writeExample)
{ {
deleteSystems(); deleteSystems();
std::string path = getConfigPath();
LOG(LogInfo) << "Loading system config file..."; LOG(LogInfo) << "Loading system config file...";
if(!fs::exists(path))
{
LOG(LogInfo) << "System config file \"" << path << "\" doesn't exist!";
if(writeExample)
writeExampleConfig(path);
return false;
}
std::ifstream file(path.c_str()); std::ifstream file(path.c_str());
if(file.is_open()) if(file.is_open())
{ {
@ -262,21 +269,21 @@ void SystemData::loadConfig()
} }
}else{ }else{
LOG(LogError) << "Error reading config file \"" << path << "\" - no equals sign found on line " << lineNr << ": \"" << line << "\"!"; LOG(LogError) << "Error reading config file \"" << path << "\" - no equals sign found on line " << lineNr << ": \"" << line << "\"!";
return; return false;
} }
} }
}else{ }else{
LOG(LogError) << "Error - could not load config file \"" << path << "\"!"; LOG(LogError) << "Error - could not load config file \"" << path << "\"!";
return; return false;
} }
LOG(LogInfo) << "Finished loading config file - created " << sSystemVector.size() << " systems."; LOG(LogInfo) << "Finished loading config file - created " << sSystemVector.size() << " systems.";
return; return true;
} }
void SystemData::writeExampleConfig() void SystemData::writeExampleConfig(const std::string& path)
{ {
std::string path = getConfigPath(); std::cerr << "Writing example config to \"" << path << "\"...";
std::ofstream file(path.c_str()); std::ofstream file(path.c_str());
@ -303,6 +310,8 @@ void SystemData::writeExampleConfig()
file << "COMMAND=" << std::endl; file << "COMMAND=" << std::endl;
file.close(); file.close();
std::cerr << "done. Go read it!\n";
} }
void SystemData::deleteSystems() void SystemData::deleteSystems()

View file

@ -25,8 +25,8 @@ public:
void launchGame(Window* window, GameData* game); void launchGame(Window* window, GameData* game);
static void deleteSystems(); static void deleteSystems();
static void loadConfig(); static bool loadConfig(const std::string& path, bool writeExampleIfNonexistant = true); //Load the system config file at getConfigPath(). Returns true if no errors were encountered. An example can be written if the file doesn't exist.
static void writeExampleConfig(); static void writeExampleConfig(const std::string& path);
static std::string getConfigPath(); static std::string getConfigPath();
static std::vector<SystemData*> sSystemVector; static std::vector<SystemData*> sSystemVector;

View file

@ -3,18 +3,20 @@
#include "Renderer.h" #include "Renderer.h"
#include "AudioManager.h" #include "AudioManager.h"
#include "VolumeControl.h" #include "VolumeControl.h"
#include "Log.h"
#include "Settings.h"
Window::Window() Window::Window()
{ {
mInputManager = new InputManager(this); mInputManager = new InputManager(this);
mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_SMALL));
mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_MEDIUM));
mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_LARGE));
} }
Window::~Window() Window::~Window()
{ {
//delete all our GUIs
while(peekGui())
delete peekGui();
delete mInputManager; delete mInputManager;
} }
@ -55,25 +57,31 @@ void Window::render()
} }
} }
void Window::init() bool Window::init(unsigned int width, unsigned int height)
{ {
mInputManager->init(); //shouldn't this go AFTER renderer initialization? if(!Renderer::init(width, height))
Renderer::init(0, 0); {
LOG(LogError) << "Renderer failed to initialize!";
return false;
}
mInputManager->init();
mResourceManager.reloadAll(); mResourceManager.reloadAll();
for(unsigned int i = 0; i < mGuiStack.size(); i++) //keep a reference to the default fonts, so they don't keep getting destroyed/recreated
if(mDefaultFonts.empty())
{ {
mGuiStack.at(i)->init(); mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_SMALL));
mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_MEDIUM));
mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_LARGE));
} }
return true;
} }
void Window::deinit() void Window::deinit()
{ {
for(unsigned int i = 0; i < mGuiStack.size(); i++)
{
mGuiStack.at(i)->deinit();
}
mInputManager->deinit(); mInputManager->deinit();
mResourceManager.unloadAll(); mResourceManager.unloadAll();
Renderer::deinit(); Renderer::deinit();

View file

@ -21,7 +21,7 @@ public:
void update(int deltaTime); void update(int deltaTime);
void render(); void render();
void init(); bool init(unsigned int width = 0, unsigned int height = 0);
void deinit(); void deinit();
InputManager* getInputManager(); InputManager* getInputManager();

View file

@ -52,7 +52,7 @@ void GuiMenu::executeCommand(std::string command)
}else if(command == "es_reload") }else if(command == "es_reload")
{ {
//reload the game list //reload the game list
SystemData::loadConfig(); SystemData::loadConfig(SystemData::getConfigPath(), false);
mParent->setSystemId(0); mParent->setSystemId(0);
}else if(command == "es_settings") }else if(command == "es_settings")
{ {

View file

@ -20,26 +20,23 @@
#include <bcm_host.h> #include <bcm_host.h>
#endif #endif
#include <sstream> #include <sstream>
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
int main(int argc, char* argv[]) bool parseArgs(int argc, char* argv[], unsigned int* width, unsigned int* height)
{ {
unsigned int width = 0;
unsigned int height = 0;
if(argc > 1) if(argc > 1)
{ {
for(int i = 1; i < argc; i++) for(int i = 1; i < argc; i++)
{ {
if(strcmp(argv[i], "-w") == 0) if(strcmp(argv[i], "-w") == 0)
{ {
width = atoi(argv[i + 1]); *width = atoi(argv[i + 1]);
i++; //skip the argument value i++; //skip the argument value
}else if(strcmp(argv[i], "-h") == 0) }else if(strcmp(argv[i], "-h") == 0)
{ {
height = atoi(argv[i + 1]); *height = atoi(argv[i + 1]);
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)
{ {
@ -83,17 +80,16 @@ int main(int argc, char* argv[])
std::cout << "--help summon a sentient, angry tuba\n\n"; std::cout << "--help summon a sentient, angry tuba\n\n";
std::cout << "More information available in README.md.\n"; std::cout << "More information available in README.md.\n";
return 0; return false; //exit after printing help
} }
} }
} }
#ifdef _RPI_ return true;
bcm_host_init(); }
#endif
bool running = true;
bool verifyHomeFolderExists()
{
//make sure the config directory exists //make sure the config directory exists
std::string home = getHomePath(); std::string home = getHomePath();
std::string configDir = home + "/.emulationstation"; std::string configDir = home + "/.emulationstation";
@ -101,55 +97,83 @@ int main(int argc, char* argv[])
{ {
std::cout << "Creating config directory \"" << configDir << "\"\n"; std::cout << "Creating config directory \"" << configDir << "\"\n";
fs::create_directory(configDir); fs::create_directory(configDir);
if(!fs::exists(configDir))
{
std::cerr << "Config directory could not be created!\n";
return false;
} }
}
return true;
}
//called on exit, assuming we get far enough to have the log initialized
void onExit()
{
Log::close();
#ifdef _RPI_
bcm_host_deinit();
#endif
}
int main(int argc, char* argv[])
{
unsigned int width = 0;
unsigned int height = 0;
if(!parseArgs(argc, argv, &width, &height))
return 0;
//if ~/.emulationstation doesn't exist and cannot be created, bail
if(!verifyHomeFolderExists())
return 1;
#ifdef _RPI_
bcm_host_init();
#endif
//start the logger //start the logger
Log::open(); Log::open();
LOG(LogInfo) << "EmulationStation - " << PROGRAM_VERSION_STRING; LOG(LogInfo) << "EmulationStation - " << PROGRAM_VERSION_STRING;
//the renderer also takes care of setting up SDL for input and sound //always close the log and deinit the BCM library on exit
bool renderInit = Renderer::init(width, height); atexit(&onExit);
if(!renderInit)
Window window;
if(!window.init(width, height))
{ {
std::cerr << "Error initializing renderer!\n"; LOG(LogError) << "Window failed to initialize!";
Log::close();
return 1; return 1;
} }
Window window; //don't call Window.init() because we manually pass the resolution to Renderer::init
window.getInputManager()->init();
//try loading the system config file //try loading the system config file
if(!fs::exists(SystemData::getConfigPath())) //if it doesn't exist, create the example and quit if(!SystemData::loadConfig(SystemData::getConfigPath(), true))
{ {
std::cerr << "A system config file in " << SystemData::getConfigPath() << " was not found. An example will be created.\n"; LOG(LogError) << "Error parsing system config file!";
SystemData::writeExampleConfig(); return 1;
std::cerr << "Set it up, then re-run EmulationStation.\n"; }
running = false;
}else{
SystemData::loadConfig();
if(SystemData::sSystemVector.size() == 0) //if it exists but was empty, notify the user and quit //make sure it wasn't empty
if(SystemData::sSystemVector.size() == 0)
{ {
std::cerr << "A system config file in " << SystemData::getConfigPath() << " was found, but contained no systems.\n"; LOG(LogError) << "No systems found! Does at least one system have a game present? (check that extensions match!)";
std::cerr << "Does at least one system have a game present?\n"; return 1;
running = false; }
}else{
//choose which GUI to open depending on Input configuration //choose which GUI to open depending on if an input configuration already exists
if(fs::exists(InputManager::getConfigPath())) if(fs::exists(InputManager::getConfigPath()))
{ {
//an input config already exists - we have input, proceed to the gamelist as usual.
GuiGameList::create(&window); GuiGameList::create(&window);
}else{ }else{
window.pushGui(new GuiDetectDevice(&window)); window.pushGui(new GuiDetectDevice(&window));
} }
}
}
bool sleeping = false; bool sleeping = false;
unsigned int timeSinceLastEvent = 0; unsigned int timeSinceLastEvent = 0;
int lastTime = 0; int lastTime = 0;
bool running = true;
while(running) while(running)
{ {
SDL_Event event; SDL_Event event;
@ -234,11 +258,5 @@ int main(int argc, char* argv[])
std::cout << "EmulationStation cleanly shutting down...\n"; std::cout << "EmulationStation cleanly shutting down...\n";
Log::close();
#ifdef _RPI_
bcm_host_deinit();
#endif
return 0; return 0;
} }