From 7ab8b17bf68a82b573849632a6b1e325e0165296 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sun, 20 Mar 2011 16:10:40 -0300 Subject: [PATCH] move some codes, fix bugs --- src/framework/engine.cpp | 20 +---------------- src/framework/platform.h | 2 +- src/framework/prerequisites.h | 5 ----- src/framework/resourcemanager.cpp | 8 +++---- src/framework/win32platform.cpp | 2 +- src/framework/x11platform.cpp | 4 ++-- src/main.cpp | 37 +++++++++++++++++++++++-------- 7 files changed, 37 insertions(+), 41 deletions(-) diff --git a/src/framework/engine.cpp b/src/framework/engine.cpp index 59dadd18..f068e98b 100644 --- a/src/framework/engine.cpp +++ b/src/framework/engine.cpp @@ -44,35 +44,17 @@ Engine::~Engine() void Engine::init() { - Platform::init(); - - int width = g_config.getInteger("width"); - int height = g_config.getInteger("height"); - - // create the window - Platform::createWindow(0, 0, width, height, 550, 450, false); - Platform::setWindowTitle(APP_NAME); - Platform::setVsync(); - // initialize graphics stuff g_graphics.init(); // finally show the window - onResize(width, height); - Platform::showWindow(); - //Platform::hideMouseCursor(); + onResize(Platform::getWindowWidth(), Platform::getWindowHeight()); } void Engine::terminate() { changeState(NULL); - // save configs - g_config.setValue("width", Platform::getWindowWidth()); - g_config.setValue("height", Platform::getWindowHeight()); - - Platform::showMouseCursor(); - Platform::terminate(); g_graphics.terminate(); } diff --git a/src/framework/platform.h b/src/framework/platform.h index 38a1ef0c..60bf7727 100644 --- a/src/framework/platform.h +++ b/src/framework/platform.h @@ -66,7 +66,7 @@ namespace Platform void swapBuffers(); /// Get the app user directory, the place to save files configurations files - const char *getAppUserDir(); + const char *getAppUserDir(const char *appName); } #endif // PLATFORM_H diff --git a/src/framework/prerequisites.h b/src/framework/prerequisites.h index 3e546ef2..f20377dd 100644 --- a/src/framework/prerequisites.h +++ b/src/framework/prerequisites.h @@ -25,11 +25,6 @@ #ifndef PREREQUISITES_H #define PREREQUISITES_H -// app name and version -#define APP_NAME "OTClient" -#define APP_LONGNAME APP_NAME " " APP_VERSION -#define APP_VERSION "0.1.0" - // easy typing #include diff --git a/src/framework/resourcemanager.cpp b/src/framework/resourcemanager.cpp index b6ba46b4..574c9b1a 100644 --- a/src/framework/resourcemanager.cpp +++ b/src/framework/resourcemanager.cpp @@ -53,14 +53,14 @@ bool ResourceManager::setWriteDir(const std::string& path) bool ret = (bool)PHYSFS_setWriteDir(path.c_str()); if(!ret) - error("Could not set the path %s as write directory, file write will not work."); + error("Could not set the path \"%s\" as write directory, file write will not work.", path.c_str()); return ret; } bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFront) { if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1)) { - error("Error while adding %s to resources search path: %s", PHYSFS_getLastError()); + error("Error while adding \"%s\" to resources search path: %s", path.c_str(), PHYSFS_getLastError()); return false; } return true; @@ -75,7 +75,7 @@ unsigned char *ResourceManager::loadFile(const std::string& fileName, unsigned i { PHYSFS_file *file = PHYSFS_openRead(fileName.c_str()); if(!file) { - error("Failed to load file %s: %s", fileName.c_str(), PHYSFS_getLastError()); + error("Failed to load file \"%s\": %s", fileName.c_str(), PHYSFS_getLastError()); *fileSize = 0; return NULL; } @@ -104,7 +104,7 @@ bool ResourceManager::saveFile(const std::string &fileName, const unsigned char { PHYSFS_file *file = PHYSFS_openWrite(fileName.c_str()); if(!file) { - error("Failed to save file %s: %s", fileName.c_str(), PHYSFS_getLastError()); + error("Failed to save file \"%s\": %s", fileName.c_str(), PHYSFS_getLastError()); return false; } diff --git a/src/framework/win32platform.cpp b/src/framework/win32platform.cpp index a9c15a96..799303fb 100644 --- a/src/framework/win32platform.cpp +++ b/src/framework/win32platform.cpp @@ -276,7 +276,7 @@ int Platform::getWindowHeight() return win32.height; } -const char *Platform::getAppUserDir() +const char *Platform::getAppUserDir(const char *appName) { /*std::stringstream sdir; sdir << PHYSFS_getUserDir() << "/." << APP_NAME << "/"; diff --git a/src/framework/x11platform.cpp b/src/framework/x11platform.cpp index a7dac089..5e3172de 100644 --- a/src/framework/x11platform.cpp +++ b/src/framework/x11platform.cpp @@ -717,10 +717,10 @@ int Platform::getWindowHeight() return x11.height; } -const char *Platform::getAppUserDir() +const char *Platform::getAppUserDir(const char *appName) { std::stringstream sdir; - sdir << PHYSFS_getUserDir() << "/." << APP_NAME << "/"; + sdir << PHYSFS_getUserDir() << "/." << appName << "/"; if((mkdir(sdir.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) && (errno != EEXIST)) error("Couldn't create directory for saving configuration file. (%s)", sdir.str().c_str()); return sdir.str().c_str(); diff --git a/src/main.cpp b/src/main.cpp index 4d6def4e..c9cf2a6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,16 +52,23 @@ void setDefaultConfigs() g_config.setValue("height", 480); } +void saveConfigs() +{ + g_config.setValue("width", Platform::getWindowWidth()); + g_config.setValue("height", Platform::getWindowHeight()); + g_config.save(); +} + int main(int argc, const char *argv[]) { // install our signal handler signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); - // setup resources + // init resources g_resources.init(argv[0]); - if(g_resources.setWriteDir(Platform::getAppUserDir())) - g_resources.addToSearchPath(Platform::getAppUserDir()); + if(g_resources.setWriteDir(Platform::getAppUserDir("OTClient"))) + g_resources.addToSearchPath(Platform::getAppUserDir("OTClient")); g_resources.addToSearchPath("data"); // before loading configurations set the default ones @@ -71,23 +78,35 @@ int main(int argc, const char *argv[]) if(!g_config.load("config.yml")) notice("Could not read configuration file, default configurations will be used."); - notice(APP_LONGNAME); + notice("OTClient 0.1.0"); - // setup the engine - g_engine.init(); + // init platform stuff + Platform::init(); - // create initial state + // create the window + Platform::createWindow(0, 0, g_config.getInteger("width"), g_config.getInteger("height"), 640, 480, false); + Platform::setWindowTitle("OTClient"); + Platform::setVsync(); + + // init engine + g_engine.init(); boost::scoped_ptr menuState(new MenuState); g_engine.changeState(menuState.get()); - // run + Platform::showWindow(); + //Platform::hideMouseCursor(); + + // main loop, run everything g_engine.run(); // terminate stuff g_engine.terminate(); + //Platform::showMouseCursor(); + Platform::terminate(); + // save configurations before exiting - g_config.save(); + saveConfigs(); // unload resources g_resources.terminate();