move some codes, fix bugs
This commit is contained in:
parent
abe69d50e7
commit
7ab8b17bf6
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 <stdint.h>
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 << "/";
|
||||
|
|
|
@ -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();
|
||||
|
|
37
src/main.cpp
37
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
|
||||
// init platform stuff
|
||||
Platform::init();
|
||||
|
||||
// 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();
|
||||
|
||||
// create initial state
|
||||
boost::scoped_ptr<MenuState> 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();
|
||||
|
|
Loading…
Reference in New Issue