introduced game data dir and user data dir
- the game data dir contains system wide data such as levels, sounds, etc. - user data dir stores higscore, configurations
This commit is contained in:
+40
-2
@@ -1,4 +1,5 @@
|
||||
#include "Commands.h"
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace Engine {
|
||||
|
||||
@@ -190,6 +191,40 @@ std::string CommandGetErrorString (){
|
||||
return CommandsInstance->GetErrorString();
|
||||
}
|
||||
|
||||
/** \brief Searches for possible candidates at reasonable places
|
||||
*
|
||||
* In that order:
|
||||
* 1. current directory
|
||||
* 2. user data directory
|
||||
* 3. game data directory
|
||||
*
|
||||
* \returns full path to the file, otherwise only the filename (which will
|
||||
* \returns then cause an error because the file cannot be opened)
|
||||
*/
|
||||
std::string find_exec_file_full_path (const std::string &exec_file) {
|
||||
std::string full_path = exec_file;
|
||||
|
||||
boost::filesystem::path exec_file_path (full_path);
|
||||
if(boost::filesystem::is_regular_file(exec_file_path)) {
|
||||
return full_path;
|
||||
}
|
||||
|
||||
full_path = GetUserDirFullPath(std::string("/") + exec_file);
|
||||
exec_file_path = full_path;
|
||||
if(boost::filesystem::is_regular_file(exec_file_path)) {
|
||||
return full_path;
|
||||
}
|
||||
|
||||
full_path = GetResourceFullPath(std::string("/") + exec_file);
|
||||
exec_file_path = full_path;
|
||||
if(boost::filesystem::is_regular_file(exec_file_path)) {
|
||||
return full_path;
|
||||
}
|
||||
|
||||
// otherwise just return the normal path which will fail anyway
|
||||
return exec_file;
|
||||
}
|
||||
|
||||
/*
|
||||
* Commands of the Command system
|
||||
*/
|
||||
@@ -202,13 +237,16 @@ bool Cmd_Exec (const std::vector<std::string> args) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string full_path = find_exec_file_full_path(args[0]);
|
||||
LogDebug ("Trying to exec file %s", full_path.c_str());
|
||||
|
||||
std::ifstream exec_file;
|
||||
exec_file.open(args[0].c_str(), std::ios_base::in);
|
||||
exec_file.open(full_path.c_str(), std::ios_base::in);
|
||||
|
||||
if (!exec_file) {
|
||||
std::ostringstream error_msg;
|
||||
error_msg << "exec failed: could not open file '"
|
||||
<< args[0] << "'";
|
||||
<< full_path.c_str() << "'";
|
||||
CommandsInstance->SetErrorString(error_msg.str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -363,5 +363,23 @@ ControllerBase* EngineGetController () {
|
||||
return EngineInstance->GetController();
|
||||
}
|
||||
|
||||
std::string GetResourceFullPath (const std::string &resource) {
|
||||
if (EngineInstance == NULL) {
|
||||
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
||||
assert (0);
|
||||
}
|
||||
|
||||
return EngineInstance->GetResourceFullPath(resource);
|
||||
}
|
||||
|
||||
std::string GetUserDirFullPath (const std::string &path) {
|
||||
if (EngineInstance == NULL) {
|
||||
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
||||
assert (0);
|
||||
}
|
||||
|
||||
return EngineInstance->GetUserDirFullPath(path);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -99,6 +99,29 @@ class Engine : public Module {
|
||||
void SetStatus (const EngineStatus new_status);
|
||||
EngineStatus GetStatus ();
|
||||
|
||||
void SetUserDataPath (const std::string &path) {
|
||||
mUserDataPath = path;
|
||||
};
|
||||
std::string GetUserDataPath () { return mUserDataPath; };
|
||||
|
||||
void SetGameDataPath (const std::string &path) {
|
||||
mGameDataPath = path;
|
||||
};
|
||||
std::string GetGameDataPath () { return mGameDataPath; };
|
||||
|
||||
/** \brief Returns the path to a resource by prepending the game data path to
|
||||
* \brief it
|
||||
*/
|
||||
std::string GetResourceFullPath (const std::string &resource) {
|
||||
return mGameDataPath + resource;
|
||||
};
|
||||
|
||||
/** \brief Returns the path to a file by prepending the user data path to it
|
||||
*/
|
||||
std::string GetUserDirFullPath (const std::string &path) {
|
||||
return mUserDataPath + path;
|
||||
};
|
||||
|
||||
private:
|
||||
// Engine must not be created with the standard constructor!
|
||||
// It must be ensured, that the correct EntityFactory is used!
|
||||
@@ -121,6 +144,9 @@ class Engine : public Module {
|
||||
Commands *mCommands;
|
||||
Variables *mVariables;
|
||||
|
||||
std::string mUserDataPath;
|
||||
std::string mGameDataPath;
|
||||
|
||||
std::string mErrorString;
|
||||
EngineStatus mStatus;
|
||||
};
|
||||
@@ -152,6 +178,14 @@ ViewBase* EngineGetView ();
|
||||
/** \brief Global access functions for the Controller */
|
||||
ControllerBase* EngineGetController ();
|
||||
|
||||
/** \brief Returns the path to a resource by prepending the game data path to
|
||||
* \brief it
|
||||
*/
|
||||
std::string GetResourceFullPath (const std::string &resource);
|
||||
|
||||
/** \brief Returns the path to a file by prepending the user data path to it
|
||||
*/
|
||||
std::string GetUserDirFullPath (const std::string &path);
|
||||
}
|
||||
|
||||
/* Include the globally visible declarations of the other modules */
|
||||
|
||||
+9
-9
@@ -13,16 +13,16 @@ namespace Engine {
|
||||
* Code is taken from http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures on
|
||||
* Sunday, March 14 2010.
|
||||
*/
|
||||
bool Sprite::LoadFromPNG (const char *filename) {
|
||||
LogDebug ("Loading png from %s", filename);
|
||||
bool Sprite::LoadFromPNG (const std::string &filename) {
|
||||
LogDebug ("Loading png from %s", filename.c_str());
|
||||
|
||||
//header for testing if it is a png
|
||||
png_byte header[8];
|
||||
|
||||
//open file as binary
|
||||
FILE *fp = fopen(filename, "rb");
|
||||
FILE *fp = fopen(filename.c_str(), "rb");
|
||||
if (!fp) {
|
||||
LogError ("Could not open file: %s", filename);
|
||||
LogError ("Could not open file: %s", filename.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ bool Sprite::LoadFromPNG (const char *filename) {
|
||||
//test if png
|
||||
int is_png = !png_sig_cmp(header, 0, 8);
|
||||
if (!is_png) {
|
||||
LogError ("Error opening png file %s: file is not a png file!", filename);
|
||||
LogError ("Error opening png file %s: file is not a png file!", filename.c_str());
|
||||
fclose(fp);
|
||||
return false;
|
||||
}
|
||||
@@ -41,7 +41,7 @@ bool Sprite::LoadFromPNG (const char *filename) {
|
||||
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
|
||||
NULL, NULL);
|
||||
if (!png_ptr) {
|
||||
LogError ("Error opening png file %s: unable to read png header", filename);
|
||||
LogError ("Error opening png file %s: unable to read png header", filename.c_str());
|
||||
fclose(fp);
|
||||
return (false);
|
||||
}
|
||||
@@ -49,7 +49,7 @@ bool Sprite::LoadFromPNG (const char *filename) {
|
||||
//create png info struct
|
||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||
if (!info_ptr) {
|
||||
LogError ("Error opening png file %s: unable to read png header", filename);
|
||||
LogError ("Error opening png file %s: unable to read png header", filename.c_str());
|
||||
png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
|
||||
fclose(fp);
|
||||
return (false);
|
||||
@@ -58,7 +58,7 @@ bool Sprite::LoadFromPNG (const char *filename) {
|
||||
//create png info struct
|
||||
png_infop end_info = png_create_info_struct(png_ptr);
|
||||
if (!end_info) {
|
||||
LogError ("Error opening png file %s: unable to read png header", filename);
|
||||
LogError ("Error opening png file %s: unable to read png header", filename.c_str());
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
|
||||
fclose(fp);
|
||||
return (false);
|
||||
@@ -66,7 +66,7 @@ bool Sprite::LoadFromPNG (const char *filename) {
|
||||
|
||||
//png error stuff, not sure libpng man suggests this.
|
||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||
LogError ("Error opening png file %s: unable to read png header", filename);
|
||||
LogError ("Error opening png file %s: unable to read png header", filename.c_str());
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
||||
fclose(fp);
|
||||
return (false);
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ class Sprite {
|
||||
mSubSpriteCount = 1;
|
||||
}
|
||||
|
||||
bool LoadFromPNG (const char *filename);
|
||||
bool LoadFromPNG (const std::string &filename);
|
||||
void DrawAt (float xpos, float ypos, float zpos);
|
||||
void DrawAt2D (float xpos, float ypos);
|
||||
unsigned int GetWidth() { return mWidth; };
|
||||
|
||||
+1
-1
@@ -275,7 +275,7 @@ bool ViewBase::LoadFont (const std::string &font_spec_string) {
|
||||
|
||||
parse_font_spec_string(font_spec_string, font_name, font_color, &font_size);
|
||||
|
||||
std::string font_path ("./data/fonts/");
|
||||
std::string font_path (GetResourceFullPath("/data/fonts/"));
|
||||
font_path += font_name;
|
||||
|
||||
LogDebug ("Loading font %s color (%1.2f, %1.2f, %1.2f) size %f from %s", font_name.c_str(), font_color[0], font_color[1], font_color[2], font_size, font_path.c_str());
|
||||
|
||||
Reference in New Issue
Block a user