From 20f4825686080b3b0392878dd9c822cf4cb0c1bc Mon Sep 17 00:00:00 2001 From: kervala Date: Fri, 23 Sep 2016 13:26:55 +0200 Subject: [PATCH 1/4] Changed: Use CCmdArgs for build_interface --HG-- branch : develop --- code/nel/tools/3d/build_interface/main.cpp | 71 +++++++++------------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/code/nel/tools/3d/build_interface/main.cpp b/code/nel/tools/3d/build_interface/main.cpp index 1b13a0f71..edaf7b5b0 100644 --- a/code/nel/tools/3d/build_interface/main.cpp +++ b/code/nel/tools/3d/build_interface/main.cpp @@ -24,8 +24,7 @@ #include "nel/misc/log.h" #include "nel/misc/path.h" #include "nel/misc/uv.h" - -//#include "windows.h" +#include "nel/misc/cmd_args.h" #include #include @@ -191,57 +190,44 @@ void enlargeCanvas (NLMISC::CBitmap &b) // *************************************************************************** // main // *************************************************************************** -int main(int nNbArg, char **ppArgs) +int main(int argc, char **argv) { - //GetCurrentDirectory (MAX_PATH, sExeDir); - sExeDir = CPath::getCurrentPath(); + CApplicationContext applicationContext; - if (nNbArg < 3) - { - outString ("ERROR : Wrong number of arguments\n"); - outString ("USAGE : build_interface [-s] [path_maps2] [path_maps3] ....\n"); - outString (" -s : build a subset of an existing interface definition while preserving the existing texture ids,"); - outString (" to support freeing up VRAM by switching to the subset without rebuilding the entire interface\n"); - return -1; - } + // Parse Command Line. + NLMISC::CCmdArgs args; + + args.setDescription("Build a huge interface texture from several small elements to optimize video memory usage."); + args.addArg("s", "subset", "existing_uv_txt_name", "Build a subset of an existing interface definition while preserving the existing texture ids, to support freeing up VRAM by switching to the subset without rebuilding the entire interface."); + args.addAdditionalArg("output_filename", "PNG or TGA file to generate", true); + args.addAdditionalArg("input_path", "Path that containts interfaces elements", false); + + if (!args.parse(argc, argv)) return 1; // build as a subset of existing interface bool buildSubset = false; string existingUVfilename; - list inputDirs; - for ( uint i=1; (sint)i inputDirs = args.getAdditionalArg("input_path"); + + string fmtName = args.getAdditionalArg("output_filename").front(); + + // append PNG extension if no one provided + if (fmtName.rfind('.') == string::npos) fmtName += ".png"; + vector AllMapNames; - list::iterator it = inputDirs.begin(); - list::iterator itEnd = inputDirs.end(); + vector::iterator it = inputDirs.begin(), itEnd = inputDirs.end(); + while( it != itEnd ) { string sDir = *it++; + if( !CFile::isDirectory(sDir) ) { outString (string("ERROR : directory ") + sDir + " does not exist\n"); @@ -427,14 +413,15 @@ int main(int nNbArg, char **ppArgs) string filename = CPath::lookup (existingUVfilename, false); if( (filename == "") || (!iFile.open(filename)) ) { - outString (string("ERROR : could not open file ") + existingUVfilename + "\n"); + outString(toString("ERROR: Unable to open %s", existingUVfilename.c_str())); return -1; } // Write subset UV text file fmtName = fmtName.substr(0, fmtName.rfind('.')); fmtName += ".txt"; - FILE *f = fopen (fmtName.c_str(), "wt"); + FILE *f = nlfopen(fmtName, "wt"); + if (f == NULL) { outString (string("ERROR: Cannot write UV file : ") + fmtName + "\n"); From 39275558c00c5684884030ffd70d00c99574e471 Mon Sep 17 00:00:00 2001 From: kervala Date: Fri, 23 Sep 2016 13:29:00 +0200 Subject: [PATCH 2/4] Changed: Simplify outString (a simple printf should be enough) --HG-- branch : develop --- code/nel/tools/3d/build_interface/main.cpp | 49 ++++++---------------- 1 file changed, 12 insertions(+), 37 deletions(-) diff --git a/code/nel/tools/3d/build_interface/main.cpp b/code/nel/tools/3d/build_interface/main.cpp index edaf7b5b0..b147f7349 100644 --- a/code/nel/tools/3d/build_interface/main.cpp +++ b/code/nel/tools/3d/build_interface/main.cpp @@ -35,21 +35,9 @@ using namespace std; using namespace NLMISC; // *************************************************************************** -//char sExeDir[MAX_PATH]; -std::string sExeDir; -NLMISC::CApplicationContext _ApplicationContext; - -void outString (const string &sText) +void outString(const string &sText) { - std::string sCurDir = CPath::getCurrentPath(); - CPath::setCurrentPath(sExeDir.c_str()); - //char sCurDir[MAX_PATH]; - //GetCurrentDirectory (MAX_PATH, sCurDir); - //SetCurrentDirectory (sExeDir); - NLMISC::createDebug (); - NLMISC::InfoLog->displayRaw(sText.c_str()); - //SetCurrentDirectory (sCurDir); - CPath::setCurrentPath(sCurDir.c_str()); + printf("%s\n", sText.c_str()); } // *************************************************************************** @@ -230,7 +218,7 @@ int main(int argc, char **argv) if( !CFile::isDirectory(sDir) ) { - outString (string("ERROR : directory ") + sDir + " does not exist\n"); + outString(toString("ERROR: directory %s does not exist", sDir.c_str())); return -1; } CPath::getPathContent(sDir, false, false, true, AllMapNames); @@ -256,7 +244,7 @@ int main(int argc, char **argv) if (pBtmp->getPixelFormat() != CBitmap::RGBA) { - nlwarning("Converting %s to RGBA (32 bits), originally using %u bits...", AllMapNames[i].c_str(), (uint)colors); + outString(toString("Converting %s to RGBA (32 bits), originally using %u bits...", AllMapNames[i].c_str(), (uint)colors)); pBtmp->convertToType(CBitmap::RGBA); } @@ -266,7 +254,7 @@ int main(int argc, char **argv) { if (pBtmp) delete pBtmp; - outString (string("ERROR :") + e.what()); + outString(toString("ERROR : %s", e.what())); return -1; } } @@ -363,24 +351,11 @@ int main(int argc, char **argv) fmtName += ".tga"; if (outTga.open(fmtName)) { - std::string ext; - if (toLower(fmtName).find(".png") != string::npos) - { - ext = "png"; - GlobalTexture.writePNG (outTga, 32); - } - else - { - ext = "tga"; - GlobalTexture.writeTGA (outTga, 32); - } - - outTga.close(); - outString (toString("Writing %s file : %s\n", ext.c_str(), fmtName.c_str())); + outString(toString("Writing %s", fmtName.c_str())); } else { - outString (string("ERROR: Cannot write tga file : ") + fmtName + "\n"); + outString(toString("ERROR: Unable to write %s", fmtName.c_str())); } // Write UV text file @@ -399,11 +374,12 @@ int main(int argc, char **argv) UVMax[i].U, UVMax[i].V); } fclose (f); - outString (string("Writing UV file : ") + fmtName + "\n"); + + outString(toString("Writing UV file %s", fmtName.c_str())); } else { - outString (string("ERROR: Cannot write UV file : ") + fmtName + "\n"); + outString(toString("ERROR: Cannot write UV file %s", fmtName.c_str())); } } else // build as a subset @@ -424,8 +400,7 @@ int main(int argc, char **argv) if (f == NULL) { - outString (string("ERROR: Cannot write UV file : ") + fmtName + "\n"); -// fclose (iFile); + outString(toString("ERROR: Unable to write UV file %s", fmtName.c_str())); return -1; } @@ -467,7 +442,7 @@ int main(int argc, char **argv) } // fclose (iFile); fclose (f); - outString (string("Writing UV file : ") + fmtName + "\n"); + outString(toString("Writing UV file: %s", fmtName.c_str())); } return 0; From 35b4a3d98ab8baa49bb0ea3352042dd8f101247b Mon Sep 17 00:00:00 2001 From: kervala Date: Fri, 23 Sep 2016 13:29:50 +0200 Subject: [PATCH 3/4] Changed: Minor changes --HG-- branch : develop --- code/nel/tools/3d/build_interface/main.cpp | 66 ++++++++-------------- 1 file changed, 24 insertions(+), 42 deletions(-) diff --git a/code/nel/tools/3d/build_interface/main.cpp b/code/nel/tools/3d/build_interface/main.cpp index b147f7349..94cdb56cf 100644 --- a/code/nel/tools/3d/build_interface/main.cpp +++ b/code/nel/tools/3d/build_interface/main.cpp @@ -46,7 +46,7 @@ const uint32 posStep= 4; // *************************************************************************** // Try all position to put pSrc in pDst -bool tryAllPos (NLMISC::CBitmap *pSrc, NLMISC::CBitmap *pDst, sint32 &x, sint32 &y) +bool tryAllPos(NLMISC::CBitmap *pSrc, NLMISC::CBitmap *pDst, sint32 &x, sint32 &y) { uint32 i, j; CObjectVector &rSrcPix = pSrc->getPixels(); @@ -86,7 +86,7 @@ bool tryAllPos (NLMISC::CBitmap *pSrc, NLMISC::CBitmap *pDst, sint32 &x, sint32 } // *************************************************************************** -void putPixel(uint8 *dst, uint8 *src, bool alphaTransfert) +void putPixel(uint8 *dst, uint8 *src, bool alphaTransfert) { dst[0] = src[0]; dst[1] = src[1]; @@ -98,7 +98,7 @@ void putPixel(uint8 *dst, uint8 *src, bool alphaTransfert) } // *************************************************************************** -bool putIn (NLMISC::CBitmap *pSrc, NLMISC::CBitmap *pDst, sint32 x, sint32 y, bool alphaTransfert=true) +bool putIn(NLMISC::CBitmap *pSrc, NLMISC::CBitmap *pDst, sint32 x, sint32 y, bool alphaTransfert=true) { uint8 *rSrcPix = &pSrc->getPixels()[0]; uint8 *rDstPix = &pDst->getPixels()[0]; @@ -145,18 +145,17 @@ bool putIn (NLMISC::CBitmap *pSrc, NLMISC::CBitmap *pDst, sint32 x, sint32 y, bo } // *************************************************************************** -string getBaseName (const string &fullname) +string getBaseName(const string &fullname) { - string sTmp2; + string basename; string::size_type pos = fullname.rfind('_'); - if (pos != string::npos) - sTmp2 = fullname.substr(0, pos+1); - return sTmp2; + if (pos != string::npos) basename = fullname.substr(0, pos+1); + return basename; } // *************************************************************************** // resize the bitmap to the next power of 2 and preserve content -void enlargeCanvas (NLMISC::CBitmap &b) +void enlargeCanvas(NLMISC::CBitmap &b) { sint32 nNewWidth = b.getWidth(), nNewHeight = b.getHeight(); if (nNewWidth > nNewHeight) @@ -221,6 +220,7 @@ int main(int argc, char **argv) outString(toString("ERROR: directory %s does not exist", sDir.c_str())); return -1; } + CPath::getPathContent(sDir, false, false, true, AllMapNames); } @@ -238,7 +238,8 @@ int main(int argc, char **argv) { pBtmp = new NLMISC::CBitmap; NLMISC::CIFile inFile; - if (!inFile.open( AllMapNames[i] )) throw NLMISC::Exception("Unable to open " + AllMapNames[i]); + + if (!inFile.open(AllMapNames[i])) throw NLMISC::Exception("Unable to open " + AllMapNames[i]); uint8 colors = pBtmp->load(inFile); @@ -288,6 +289,7 @@ int main(int argc, char **argv) vector UVMin, UVMax; UVMin.resize (mapSize, NLMISC::CUV(0.0f, 0.0f)); UVMax.resize (mapSize, NLMISC::CUV(0.0f, 0.0f)); + for (sint i = 0; i < mapSize; ++i) { sint32 x, y; @@ -297,40 +299,20 @@ int main(int argc, char **argv) enlargeCanvas (GlobalTexture); enlargeCanvas (GlobalMask); } + putIn (AllMaps[i], &GlobalTexture, x, y); putIn (AllMaps[i], &GlobalMask, x, y, false); + UVMin[i].U = (float)x; UVMin[i].V = (float)y; UVMax[i].U = (float)x+AllMaps[i]->getWidth(); UVMax[i].V = (float)y+AllMaps[i]->getHeight(); - /* // Do not remove this is useful for debugging - { - NLMISC::COFile outTga; - string fmtName = ppArgs[1]; - if (fmtName.rfind('.') == string::npos) - fmtName += ".tga"; - if (outTga.open(fmtName)) - { - GlobalTexture.writeTGA (outTga, 32); - outTga.close(); - } - } - { - NLMISC::COFile outTga; - string fmtName = ppArgs[1]; - if (fmtName.rfind('.') == string::npos) - fmtName += "_msk.tga"; - else - fmtName = fmtName.substr(0,fmtName.rfind('.')) + "_msk.tga"; - if (outTga.open(fmtName)) - { - GlobalMask.writeTGA (outTga, 32); - outTga.close(); - } - }*/ - - +#if 0 + // Do not remove this is useful for debugging + writeFileDependingOnFilename(fmtName.substr(0, fmtName.rfind('.')) + "_txt.png", GlobalTexture); + writeFileDependingOnFilename(fmtName.substr(0, fmtName.rfind('.')) + "_msk.png", GlobalMask); +#endif } // Convert UV from pixel to ratio @@ -369,10 +351,10 @@ int main(int argc, char **argv) for (sint i = 0; i < mapSize; ++i) { // get the string whitout path - string fileName= CFile::getFilename(AllMapNames[i]); - fprintf (f, "%s %.12f %.12f %.12f %.12f\n", fileName.c_str(), UVMin[i].U, UVMin[i].V, - UVMax[i].U, UVMax[i].V); + string fileName = CFile::getFilename(AllMapNames[i]); + fprintf (f, "%s %.12f %.12f %.12f %.12f\n", fileName.c_str(), UVMin[i].U, UVMin[i].V, UVMax[i].U, UVMax[i].V); } + fclose (f); outString(toString("Writing UV file %s", fmtName.c_str())); @@ -387,7 +369,8 @@ int main(int argc, char **argv) // Load existing uv file CIFile iFile; string filename = CPath::lookup (existingUVfilename, false); - if( (filename == "") || (!iFile.open(filename)) ) + + if( filename.empty() || !iFile.open(filename) ) { outString(toString("ERROR: Unable to open %s", existingUVfilename.c_str())); return -1; @@ -440,7 +423,6 @@ int main(int argc, char **argv) UVMax[i].U, UVMax[i].V); } } -// fclose (iFile); fclose (f); outString(toString("Writing UV file: %s", fmtName.c_str())); } From 536483f8a71f833ff8455087a94829c1d3255338 Mon Sep 17 00:00:00 2001 From: kervala Date: Fri, 23 Sep 2016 13:31:41 +0200 Subject: [PATCH 4/4] Changed: Factorize saving to right bitmap format --HG-- branch : develop --- code/nel/tools/3d/build_interface/main.cpp | 34 +++++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/code/nel/tools/3d/build_interface/main.cpp b/code/nel/tools/3d/build_interface/main.cpp index 94cdb56cf..04213d34b 100644 --- a/code/nel/tools/3d/build_interface/main.cpp +++ b/code/nel/tools/3d/build_interface/main.cpp @@ -174,6 +174,30 @@ void enlargeCanvas(NLMISC::CBitmap &b) b = b2; } +bool writeFileDependingOnFilename(const std::string &filename, CBitmap &bitmap) +{ + NLMISC::COFile out; + + if (out.open(filename)) + { + if (toLower(filename).find(".png") != string::npos) + { + bitmap.writePNG(out, 32); + } + else + { + bitmap.writeTGA(out, 32); + } + + out.close(); + + return true; + } + + return false; +} + + // *************************************************************************** // main // *************************************************************************** @@ -325,13 +349,7 @@ int main(int argc, char **argv) } // Write global texture file - //SetCurrentDirectory (sExeDir); - CPath::setCurrentPath(sExeDir.c_str()); - - NLMISC::COFile outTga; - if (fmtName.rfind('.') == string::npos) - fmtName += ".tga"; - if (outTga.open(fmtName)) + if (writeFileDependingOnFilename(fmtName, GlobalTexture)) { outString(toString("Writing %s", fmtName.c_str())); } @@ -345,7 +363,7 @@ int main(int argc, char **argv) { fmtName = fmtName.substr(0, fmtName.rfind('.')); fmtName += ".txt"; - FILE *f = fopen (fmtName.c_str(), "wt"); + FILE *f = nlfopen(fmtName, "wt"); if (f != NULL) { for (sint i = 0; i < mapSize; ++i)