diff --git a/ColorConfiguration.cpp b/ColorConfiguration.cpp index 007fab61..ce6caa09 100644 --- a/ColorConfiguration.cpp +++ b/ColorConfiguration.cpp @@ -112,11 +112,6 @@ void parseColorElement( TiXmlElement* elemColor, vector & co } } -void flushColorConfig(vector& config) -{ - config.clear(); -} - bool addSingleColorConfig( TiXmlElement* elemRoot) { string elementType = elemRoot->Value(); diff --git a/Config.cpp b/Config.cpp index 398abc9e..3143be36 100644 --- a/Config.cpp +++ b/Config.cpp @@ -554,21 +554,26 @@ void parseConfigLine( string line ){ } -bool loadConfigFile(){ - string line; - ifstream myfile ("init.txt"); - if (myfile.is_open() == false) - { - cout << "Cannot find init file" << endl; - return false; - } +bool loadConfigFile() +{ + string line; + ALLEGRO_PATH * p =al_create_path("stonesense/init.txt"); + const char * path = al_path_cstr(p,ALLEGRO_NATIVE_PATH_SEP); + ifstream myfile (path); + if (myfile.is_open() == false) + { + cout << "Cannot find init file" << endl; + al_destroy_path(p); + return false; + } - while ( !myfile.eof() ) - { - getline (myfile,line); - cout << line << endl; - parseConfigLine( line ); - } - myfile.close(); - return true; + while ( !myfile.eof() ) + { + getline (myfile,line); + cout << line << endl; + parseConfigLine( line ); + } + myfile.close(); + al_destroy_path(p); + return true; } diff --git a/ContentBuildingReader.cpp b/ContentBuildingReader.cpp index 4b3b8b3e..026c2ec0 100644 --- a/ContentBuildingReader.cpp +++ b/ContentBuildingReader.cpp @@ -206,17 +206,25 @@ inline bool readNode(SpriteNode* node, TiXmlElement* elemNode, TiXmlElement* ele bool includeFile(SpriteNode* node, TiXmlElement* includeNode, SpriteBlock* &oldSibling) { - string filenameStr("buildings/include/"); - filenameStr.append(includeNode->Attribute("file")); + // get path... ugly + char configfilepath[FILENAME_BUFFERSIZE] = {0}; + const char* documentRef = getDocument(includeNode); - TiXmlDocument doc( filenameStr.c_str() ); + if (!getLocalFilename(configfilepath,includeNode->Attribute("file"),documentRef)) + { + return false; + } + ALLEGRO_PATH * incpath = al_create_path(configfilepath); + al_append_path_component(incpath, "include"); + TiXmlDocument doc( al_path_cstr(incpath, ALLEGRO_NATIVE_PATH_SEP) ); + al_destroy_path(incpath); bool loadOkay = doc.LoadFile(); TiXmlHandle hDoc(&doc); TiXmlElement* elemParent; if(!loadOkay) { contentError("Include failed",includeNode); - WriteErr("File load failed: %s\n",filenameStr.c_str()); + WriteErr("File load failed: %s\n",configfilepath); WriteErr("Line %d: %s\n",doc.ErrorRow(),doc.ErrorDesc()); return false; } diff --git a/ContentLoader.cpp b/ContentLoader.cpp index 0c5f7593..fe0a2d0e 100644 --- a/ContentLoader.cpp +++ b/ContentLoader.cpp @@ -21,7 +21,7 @@ ContentLoader::~ContentLoader(void) flushTerrainConfig(terrainFloorConfigs); flushTerrainConfig(terrainBlockConfigs); flushCreatureConfig(); - flushColorConfig(colorConfigs); + colorConfigs.clear(); } void DumpMaterialNamesToDisk(vector material, const char* filename){ @@ -52,7 +52,7 @@ bool ContentLoader::Load( DFHack::Core& DF){ flushBuildingConfig(&customBuildingConfigs); flushTerrainConfig(terrainFloorConfigs); flushTerrainConfig(terrainBlockConfigs); - flushColorConfig(colorConfigs); + colorConfigs.clear(); creatureConfigs.clear(); treeConfigs.clear(); shrubConfigs.clear(); @@ -227,7 +227,9 @@ bool ContentLoader::Load( DFHack::Core& DF){ contentLoader->obsidian = lookupMaterialIndex(INORGANIC, "OBSIDIAN"); loadGraphicsFromDisk(); //these get destroyed when flushImgFiles is called. - bool overallResult = parseContentIndexFile( "index.txt" ); + ALLEGRO_PATH * p = al_create_path("stonesense/index.txt"); + bool overallResult = parseContentIndexFile( al_path_cstr(p, ALLEGRO_NATIVE_PATH_SEP) ); + al_destroy_path(p); translationComplete = false; return true; @@ -239,7 +241,7 @@ bool ContentLoader::reload_configs() flushBuildingConfig(&customBuildingConfigs); flushTerrainConfig(terrainFloorConfigs); flushTerrainConfig(terrainBlockConfigs); - flushColorConfig(colorConfigs); + colorConfigs.clear(); creatureConfigs.clear(); treeConfigs.clear(); shrubConfigs.clear(); @@ -247,7 +249,9 @@ bool ContentLoader::reload_configs() flushImgFiles(); loadGraphicsFromDisk(); //these get destroyed when flushImgFiles is called. - bool overallResult = parseContentIndexFile( "index.txt" ); + ALLEGRO_PATH * p = al_create_path("stonesense/index.txt"); + bool overallResult = parseContentIndexFile( al_path_cstr(p, ALLEGRO_NATIVE_PATH_SEP) ); + al_destroy_path(p); return overallResult; } diff --git a/ContentLoader.h b/ContentLoader.h index 6f0dff71..0b4eca17 100644 --- a/ContentLoader.h +++ b/ContentLoader.h @@ -70,6 +70,7 @@ class ContentLoader{ extern ContentLoader * contentLoader; extern const char* getDocument(TiXmlNode* element); +bool getLocalFilename(char * buffer, const char* filename, const char* relativeto); extern void contentError(const char* message, TiXmlNode* element); extern char getAnimFrames(const char* framestring); extern int loadConfigImgFile(const char* filename, TiXmlElement* referrer); diff --git a/FluidConfiguration.cpp b/FluidConfiguration.cpp index f0ec4543..fe65ef2a 100644 --- a/FluidConfiguration.cpp +++ b/FluidConfiguration.cpp @@ -70,7 +70,7 @@ void parseFluidElement( TiXmlElement* elemFluid, int basefile) bool addSingleFluidConfig( TiXmlElement* elemRoot) { - int basefile = INVALID_INDEX; + int basefile = 0; const char* filename = elemRoot->Attribute("file"); if (filename != NULL && filename[0] != 0) { diff --git a/GUI.cpp b/GUI.cpp index 4134eda9..fcb2756b 100644 --- a/GUI.cpp +++ b/GUI.cpp @@ -810,39 +810,63 @@ void paintboard(){ -void loadGraphicsFromDisk(){ - /*al_clear_to_color(al_map_rgb(0,0,0)); - draw_textf_border(font, - al_get_bitmap_width(al_get_target_bitmap())/2, - al_get_bitmap_height(al_get_target_bitmap())/2, - ALLEGRO_ALIGN_CENTRE, "Loading..."); - al_flip_display();*/ - int index; - index = loadImgFile("objects.png"); - if(index == -1) return; - IMGObjectSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); - index = loadImgFile("creatures.png"); - if(index == -1) return; - IMGCreatureSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); - index = loadImgFile("ramps.png"); - if(index == -1) return; - IMGRampSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); - index = loadImgFile("SSStatusIcons.png"); - if(index == -1) return; - IMGStatusSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); - index = loadImgFile("gibs.png"); - if(index == -1) return; - IMGBloodSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); - index = loadImgFile("engravings_floor.png"); - if(index == -1) return; - IMGEngFloorSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); - index = loadImgFile("engravings_left.png"); - if(index == -1) return; - IMGEngLeftSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); - index = loadImgFile("engravings_right.png"); - if(index == -1) return; - IMGEngRightSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); - createEffectSprites(); +void loadGraphicsFromDisk() +{ + auto globulate = [](const char * filename, ALLEGRO_BITMAP *& imgd ) -> bool + { + int index; + ALLEGRO_PATH * p = al_create_path_for_directory("stonesense"); + al_set_path_filename(p,filename); + index = loadImgFile(al_path_cstr(p,ALLEGRO_NATIVE_PATH_SEP)); + if(index == -1) + { + al_destroy_path(p); + return false; + } + imgd = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); + al_destroy_path(p); + return true; + }; + if(!globulate("objects.png", IMGObjectSheet)) return; + if(!globulate("creatures.png", IMGObjectSheet)) return; + if(!globulate("ramps.png", IMGObjectSheet)) return; + if(!globulate("SSStatusIcons.png", IMGObjectSheet)) return; + if(!globulate("gibs.png", IMGObjectSheet)) return; + if(!globulate("engravings_floor.png", IMGObjectSheet)) return; + if(!globulate("engravings_left.png", IMGObjectSheet)) return; + if(!globulate("engravings_right.png", IMGObjectSheet)) return; +/* + al_set_path_filename(p,"creatures.png"); + index = loadImgFile(al_path_cstr(p,ALLEGRO_NATIVE_PATH_SEP)); + if(index == -1) return; + IMGCreatureSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); + + al_set_path_filename(p,"ramps.png"); + index = loadImgFile(al_path_cstr(p,ALLEGRO_NATIVE_PATH_SEP)); + if(index == -1) return; + IMGRampSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); + + index = loadImgFile("SSStatusIcons.png"); + if(index == -1) return; + IMGStatusSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); + + index = loadImgFile("gibs.png"); + if(index == -1) return; + IMGBloodSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); + + index = loadImgFile("engravings_floor.png"); + if(index == -1) return; + IMGEngFloorSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); + + index = loadImgFile("engravings_left.png"); + if(index == -1) return; + IMGEngLeftSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); + + index = loadImgFile("engravings_right.png"); + if(index == -1) return; + IMGEngRightSheet = al_create_sub_bitmap(IMGFilelist[index], 0, 0, al_get_bitmap_width(IMGFilelist[index]), al_get_bitmap_height(IMGFilelist[index])); + */ + createEffectSprites(); } //delete and clean out the image files diff --git a/common.h b/common.h index 4e03174e..27f237f3 100644 --- a/common.h +++ b/common.h @@ -202,9 +202,9 @@ bool loadConfigFile(); //xmlBuildingReader.cpp class BuildingConfiguration; -#define FILENAME_BUFFERSIZE 50 -// temp buffers are bigger because they deal with absolute paths -#define FILENAME_BUFFERSIZE_LOCAL 200 +// BUG: this is dangerous! +#define FILENAME_BUFFERSIZE 1024 +#define FILENAME_BUFFERSIZE_LOCAL 2048 extern ALLEGRO_FONT *font; extern ALLEGRO_KEYBOARD_STATE keyboard; diff --git a/main.cpp b/main.cpp index 8e4beabe..54d7976f 100644 --- a/main.cpp +++ b/main.cpp @@ -68,16 +68,18 @@ int32_t viewy = 0; int32_t viewz = 0; bool followmode = true;*/ -ALLEGRO_BITMAP* load_bitmap_withWarning(const char* path){ - ALLEGRO_BITMAP* img = 0; - img = al_load_bitmap(path); - if(!img){ - DFConsole->printerr("Cannot load image: %s\n", path); - al_set_thread_should_stop(thread); - return 0; - } - al_convert_mask_to_alpha(img, al_map_rgb(255, 0, 255)); - return img; +ALLEGRO_BITMAP* load_bitmap_withWarning(const char* path) +{ + ALLEGRO_BITMAP* img = 0; + img = al_load_bitmap(path); + if(!img) + { + DFConsole->printerr("Cannot load image: %s\n", path); + al_set_thread_should_stop(thread); + return 0; + } + al_convert_mask_to_alpha(img, al_map_rgb(255, 0, 255)); + return img; } @@ -140,12 +142,21 @@ void correctBlockForSegmetOffset(int32_t& x, int32_t& y, int32_t& z){ bool loadfont() { - font = al_load_font(al_path_cstr(config.font, ALLEGRO_NATIVE_PATH_SEP), config.fontsize, 0); - if (!font) { - DFConsole->printerr("Cannot load font: %s\n", al_path_cstr(config.font, ALLEGRO_NATIVE_PATH_SEP)); - return 0; - } - return 1; + ALLEGRO_PATH * p = al_create_path_for_directory("stonesense"); + if(!al_join_paths(p, config.font)) + { + al_destroy_path(p); + return false; + } + font = al_load_font(al_path_cstr(p, ALLEGRO_NATIVE_PATH_SEP), config.fontsize, 0); + if (!font) + { + DFConsole->printerr("Cannot load font: %s\n", al_path_cstr(p, ALLEGRO_NATIVE_PATH_SEP)); + al_destroy_path(p); + return false; + } + al_destroy_path(p); + return true; } void benchmark(DFHack::Core * c){ @@ -177,10 +188,16 @@ void animUpdateProc() void drawcredits() { + static ALLEGRO_BITMAP* SplashImage = NULL; // BUG: leaks the image al_clear_to_color(al_map_rgb(0,0,0)); //centred splash image { - static ALLEGRO_BITMAP* SplashImage = load_bitmap_withWarning("splash.png"); + if(!SplashImage) + { + ALLEGRO_PATH * p = al_create_path("stonesense/stonesense.png"); + SplashImage = load_bitmap_withWarning(al_path_cstr(p, ALLEGRO_NATIVE_PATH_SEP)); + al_destroy_path(p); + } if(!SplashImage) return; al_draw_bitmap_region(SplashImage, 0, 0, @@ -467,9 +484,12 @@ static void * stonesense_thread(ALLEGRO_THREAD * thred, void * parms) if(config.software) al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP|ALLEGRO_ALPHA_TEST); - IMGIcon = load_bitmap_withWarning("stonesense.png"); + ALLEGRO_PATH * p = al_create_path("stonesense/stonesense.png"); + IMGIcon = load_bitmap_withWarning(al_path_cstr(p, ALLEGRO_NATIVE_PATH_SEP)); + al_destroy_path(p); if(!IMGIcon) { + al_destroy_display(display); stonesense_started = 0; return NULL; } diff --git a/Colors.xml b/resources/Colors.xml similarity index 100% rename from Colors.xml rename to resources/Colors.xml diff --git a/resources/index.txt b/resources/index.txt index b2deb5c2..7f24416e 100644 --- a/resources/index.txt +++ b/resources/index.txt @@ -8,4 +8,4 @@ buildings/index.txt vegetation/index.txt colors/index.txt Colors.xml -Fluids/index.txt \ No newline at end of file +Fluids/index.txt