diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index b3391871..155b2437 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -863,10 +863,11 @@ void printParameterHelp(const char *argv0, bool foundInvalidArgs) { printf("\n \t\tWhere x is an optional property name to filter (default shows all)."); printf("\n \t\texample: %s %s=DebugMode",argv0,GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]); - printf("\n%s=x=textureformat\t\t\tconvert a model file or folder to the current g3d version format.",GAME_ARGS[GAME_ARG_CONVERT_MODELS]); + printf("\n%s=x=textureformat=keepsmallest\t\t\tconvert a model file or folder to the current g3d version format.",GAME_ARGS[GAME_ARG_CONVERT_MODELS]); printf("\n \t\tWhere x is a filename or folder containing the g3d model(s)."); printf("\n \t\tWhere textureformat is an optional supported texture format to convert to (tga,bmp,png)."); - printf("\n \t\texample: %s %s=techs/megapack/factions/tech/units/castle/models/castle.g3d=png",argv0,GAME_ARGS[GAME_ARG_CONVERT_MODELS]); + printf("\n \t\tWhere keepsmallest is an optional flag indicating to keep original texture if its filesize is smaller than the converted format."); + printf("\n \t\texample: %s %s=techs/megapack/factions/tech/units/castle/models/castle.g3d=png=keepsmallest",argv0,GAME_ARGS[GAME_ARG_CONVERT_MODELS]); printf("\n%s=x=textureformat\t\t\tconvert a texture file or folder to the format textureformat.",GAME_ARGS[GAME_ARG_CONVERT_TEXTURES]); printf("\n \t\tWhere x is a filename or folder containing the texture(s)."); @@ -1813,6 +1814,12 @@ int glestMain(int argc, char** argv) { printf("About to convert using texture format [%s]\n",textureFormat.c_str()); } + bool keepsmallest = false; + if(paramPartTokens.size() >= 4 && paramPartTokens[1].length() > 0) { + keepsmallest = (paramPartTokens[3] == "keepsmallest"); + printf("About to convert using keepsmallest = %d\n",keepsmallest); + } + showCursor(true); mainWindow->setUseDefaultCursorOnly(true); @@ -1861,7 +1868,7 @@ int glestMain(int argc, char** argv) { Model *model = renderer.newModel(rsGlobal); try { - printf("About to load model [%s]\n",file.c_str()); + printf("About to load model [%s] [%d of %lu]\n",file.c_str(),i,(long int)models.size()); model->load(file); modelLoadedOk = true; } @@ -1871,7 +1878,7 @@ int glestMain(int argc, char** argv) { if(modelLoadedOk == true) { printf("About to save converted model [%s]\n",file.c_str()); - model->save(file,textureFormat); + model->save(file,textureFormat,keepsmallest); } Renderer::getInstance().endModel(rsGlobal, model); diff --git a/source/shared_lib/include/graphics/model.h b/source/shared_lib/include/graphics/model.h index e644d004..1bbb0e4a 100644 --- a/source/shared_lib/include/graphics/model.h +++ b/source/shared_lib/include/graphics/model.h @@ -140,7 +140,8 @@ public: void loadV3(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad); void load(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad); void save(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager, - string convertTextureToFormat, std::map &textureDeleteList); + string convertTextureToFormat, std::map &textureDeleteList, + bool keepsmallest); void deletePixels(); @@ -194,9 +195,9 @@ public: //io void load(const string &path,bool deletePixMapAfterLoad=false); - void save(const string &path, string convertTextureToFormat=""); + void save(const string &path, string convertTextureToFormat,bool keepsmallest); void loadG3d(const string &path,bool deletePixMapAfterLoad=false); - void saveG3d(const string &path, string convertTextureToFormat=""); + void saveG3d(const string &path, string convertTextureToFormat,bool keepsmallest); void setTextureManager(TextureManager *textureManager) {this->textureManager= textureManager;} void deletePixels(); diff --git a/source/shared_lib/include/platform/common/platform_common.h b/source/shared_lib/include/platform/common/platform_common.h index 76c3c312..b65f8724 100644 --- a/source/shared_lib/include/platform/common/platform_common.h +++ b/source/shared_lib/include/platform/common/platform_common.h @@ -153,6 +153,7 @@ string replaceAll(string& context, const string& from, const string& to); bool removeFile(string file); bool renameFile(string oldFile, string newFile); void removeFolder(const string path); +long getFileSize(string filename); int getScreenW(); int getScreenH(); diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index e8af7b20..a67cb55b 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -490,7 +490,8 @@ void Mesh::load(int meshIndex, const string &dir, FILE *f, TextureManager *textu } void Mesh::save(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager, - string convertTextureToFormat, std::map &textureDeleteList) { + string convertTextureToFormat, std::map &textureDeleteList, + bool keepsmallest) { MeshHeader meshHeader; memset(&meshHeader, 0, sizeof(struct MeshHeader)); @@ -521,7 +522,7 @@ void Mesh::save(int meshIndex, const string &dir, FILE *f, TextureManager *textu //maps uint32 flag= 1; - for(int i=0; i< meshTextureCount; ++i) { + for(int i = 0; i < meshTextureCount; ++i) { if((meshHeader.textures & flag)) { uint8 cMapPath[mapPathSize]; memset(&cMapPath[0],0,mapPathSize); @@ -537,6 +538,9 @@ void Mesh::save(int meshIndex, const string &dir, FILE *f, TextureManager *textu if(toLower(convertTextureToFormat) != "" && EndsWith(file, "." + convertTextureToFormat) == false) { + long originalSize = getFileSize(file); + long newSize = originalSize; + string fileExt = extractExtension(file); replaceAll(file, "." + fileExt, "." + convertTextureToFormat); @@ -544,34 +548,53 @@ void Mesh::save(int meshIndex, const string &dir, FILE *f, TextureManager *textu if(convertTextureToFormat == "tga") { texture->getPixmap()->saveTga(file); - - textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1; + newSize = getFileSize(file); + if(keepsmallest == false || newSize <= originalSize) { + textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1; + } + else { + printf("Texture will not be converted, keeping smallest texture [%s]\n",texture->getPath().c_str()); + textureDeleteList[file] = textureDeleteList[file] + 1; + } } else if(convertTextureToFormat == "bmp") { texture->getPixmap()->saveBmp(file); - - textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1; + newSize = getFileSize(file); + if(keepsmallest == false || newSize <= originalSize) { + textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1; + } + else { + printf("Texture will not be converted, keeping smallest texture [%s]\n",texture->getPath().c_str()); + textureDeleteList[file] = textureDeleteList[file] + 1; + } } //else if(convertTextureToFormat == "jpg") { // texture->getPixmap()->saveJpg(file); //} else if(convertTextureToFormat == "png") { texture->getPixmap()->savePng(file); - - textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1; + newSize = getFileSize(file); + if(keepsmallest == false || newSize <= originalSize) { + textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1; + } + else { + printf("Texture will not be converted, keeping smallest texture [%s]\n",texture->getPath().c_str()); + textureDeleteList[file] = textureDeleteList[file] + 1; + } } else { - throw runtime_error("Unsuppoted texture format: [" + convertTextureToFormat + "]"); + throw runtime_error("Unsupported texture format: [" + convertTextureToFormat + "]"); } //textureManager->endTexture(texture); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Save, load new texture [%s] originalSize [%ld] newSize [%ld]\n",file.c_str(),originalSize,newSize); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Save, load new texture [%s]\n",file.c_str()); - texture = loadMeshTexture(meshIndex, i, textureManager,file, - meshTextureChannelCount[i], - texturesOwned[i], - false); - + if(keepsmallest == false || newSize <= originalSize) { + texture = loadMeshTexture(meshIndex, i, textureManager,file, + meshTextureChannelCount[i], + texturesOwned[i], + false); + } } file = extractFileFromDirectoryPath(texture->getPath()); @@ -730,12 +753,13 @@ void Model::load(const string &path, bool deletePixMapAfterLoad) { this->fileName = path; } -void Model::save(const string &path, string convertTextureToFormat) { +void Model::save(const string &path, string convertTextureToFormat, + bool keepsmallest) { string extension= path.substr(path.find_last_of('.')+1); if(extension=="g3d" ||extension=="G3D") { - saveG3d(path,convertTextureToFormat); + saveG3d(path,convertTextureToFormat,keepsmallest); } - else{ + else { throw runtime_error("Unknown model format: " + extension); } } @@ -820,10 +844,9 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) { } //save a model to a g3d file -void Model::saveG3d(const string &path, string convertTextureToFormat) { - +void Model::saveG3d(const string &path, string convertTextureToFormat, + bool keepsmallest) { string tempModelFilename = path + "cvt"; - FILE *f= fopen(tempModelFilename.c_str(), "wb"); if(f == NULL) { throw runtime_error("Cant open file for writting: [" + tempModelFilename + "]"); @@ -852,7 +875,8 @@ void Model::saveG3d(const string &path, string convertTextureToFormat) { std::map textureDeleteList; for(uint32 i = 0; i < meshCount; ++i) { meshes[i].save(i,tempModelFilename, f, textureManager, - convertTextureToFormat,textureDeleteList); + convertTextureToFormat,textureDeleteList, + keepsmallest); } removeFile(path); diff --git a/source/shared_lib/sources/platform/common/platform_common.cpp b/source/shared_lib/sources/platform/common/platform_common.cpp index 63f08b21..9a1a891e 100644 --- a/source/shared_lib/sources/platform/common/platform_common.cpp +++ b/source/shared_lib/sources/platform/common/platform_common.cpp @@ -1384,6 +1384,12 @@ bool renameFile(string oldFile, string newFile) { return (result == 0); } +long getFileSize(string filename) { + struct stat stbuf; + stat(filename.c_str(), &stbuf); + return stbuf.st_size; +} + // ===================================== // ModeInfo // =====================================