Merge with develop

--HG--
branch : compatibility-develop
This commit is contained in:
kervala 2016-09-23 13:32:12 +02:00
commit 3d84a68df4

View file

@ -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 <vector>
#include <string>
@ -36,21 +35,9 @@ using namespace std;
using namespace NLMISC;
// ***************************************************************************
//char sExeDir[MAX_PATH];
std::string sExeDir;
NLMISC::CApplicationContext _ApplicationContext;
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());
}
// ***************************************************************************
@ -160,11 +147,10 @@ bool putIn (NLMISC::CBitmap *pSrc, NLMISC::CBitmap *pDst, sint32 x, sint32 y, bo
// ***************************************************************************
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;
}
// ***************************************************************************
@ -188,65 +174,77 @@ 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
// ***************************************************************************
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<existing_uv_txt_name>] <out_tga_name> <path_maps1> [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<string> inputDirs;
for ( uint i=1; (sint)i<nNbArg; ++i )
if (args.haveArg("s"))
{
if ( ppArgs[i][0] == '-' )
{
switch ( ppArgs[i][1] )
{
case 'S':
case 's':
buildSubset = true;
existingUVfilename = string( ppArgs[i]+2 );
break;
default:
break;
}
}
else
inputDirs.push_back(ppArgs[i]);
existingUVfilename = args.getArg("s").front();
}
string fmtName;
uint iNumDirs = (uint)inputDirs.size();
if( iNumDirs )
{
fmtName = inputDirs.front();
inputDirs.pop_front();
--iNumDirs;
}
std::vector<std::string> 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<string> AllMapNames;
list<string>::iterator it = inputDirs.begin();
list<string>::iterator itEnd = inputDirs.end();
vector<string>::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");
outString(toString("ERROR: directory %s does not exist", sDir.c_str()));
return -1;
}
CPath::getPathContent(sDir, false, false, true, AllMapNames);
}
@ -264,13 +262,14 @@ int main(int nNbArg, char **ppArgs)
{
pBtmp = new NLMISC::CBitmap;
NLMISC::CIFile inFile;
if (!inFile.open(AllMapNames[i])) throw NLMISC::Exception("Unable to open " + AllMapNames[i]);
uint8 colors = pBtmp->load(inFile);
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);
}
@ -280,7 +279,7 @@ int main(int nNbArg, char **ppArgs)
{
if (pBtmp) delete pBtmp;
outString (string("ERROR :") + e.what());
outString(toString("ERROR : %s", e.what()));
return -1;
}
}
@ -314,6 +313,7 @@ int main(int nNbArg, char **ppArgs)
vector<NLMISC::CUV> 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;
@ -323,40 +323,20 @@ int main(int nNbArg, char **ppArgs)
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,32 +349,13 @@ int main(int nNbArg, char **ppArgs)
}
// 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))
{
std::string ext;
if (toLower(fmtName).find(".png") != string::npos)
{
ext = "png";
GlobalTexture.writePNG (outTga, 32);
outString(toString("Writing %s", fmtName.c_str()));
}
else
{
ext = "tga";
GlobalTexture.writeTGA (outTga, 32);
}
outTga.close();
outString (toString("Writing %s file : %s\n", ext.c_str(), 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
@ -402,22 +363,23 @@ int main(int nNbArg, char **ppArgs)
{
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)
{
// 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);
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 (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
@ -425,20 +387,21 @@ int main(int nNbArg, char **ppArgs)
// Load existing uv file
CIFile iFile;
string filename = CPath::lookup (existingUVfilename, false);
if( (filename == "") || (!iFile.open(filename)) )
if( filename.empty() || !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");
// fclose (iFile);
outString(toString("ERROR: Unable to write UV file %s", fmtName.c_str()));
return -1;
}
@ -478,9 +441,8 @@ int main(int nNbArg, char **ppArgs)
UVMax[i].U, UVMax[i].V);
}
}
// fclose (iFile);
fclose (f);
outString (string("Writing UV file : ") + fmtName + "\n");
outString(toString("Writing UV file: %s", fmtName.c_str()));
}
return 0;