1 //-------------------------------------------------------------------
2 // bitmapcode: Helper application which coverts PNG files into
3 // C++ code outputting binary code.
5 // This application also is a simple test if the wxWidgets & Boost
6 // header files and library files are there.
8 // This file is licenced under the GNU General Public License
9 // version 3 only. (GPLv3 only).
10 //-------------------------------------------------------------------
17 /*#include <boost/filesystem.hpp>
18 #include <boost/filesystem/path.hpp>
19 #include <boost/filesystem/fstream.hpp>*/
22 #include <wx/tokenzr.h>
26 //namespace boostfs = boost::filesystem;
28 void CreateCPPFile(wxString dirfilename, wxString PNGFilename, int* counter)
30 //-------------------------------------------------------------------
31 // CreateCPPFile: Create a CPP file from the filename given.
33 // dirfilename wxString of the directory name.
34 // filename wxString of the filename.
35 // counter Pointer to a integer counter.
36 //-------------------------------------------------------------------
39 /*boostfs::path PNGFilename(filename.c_str());
40 boostfs::path CPPFilename = PNGFilename;
41 boostfs::path CPPFilenameOnly(CPPFilename.filename());
42 boostfs::path DirFilename(dirfilename.c_str());
43 boostfs::path DirFilenameOnly(DirFilename.filename());
44 CPPFilename.replace_extension(".cpp");*/
47 CPPFilename = PNGFilename;
48 CPPFilename.RemoveLast(4);
49 CPPFilename.Append(wxT(".cpp"));
51 wxString DirFilenameOnly;
52 wxString CPPFilenameOnly;
54 #if defined(__HAIKU__)
56 #elif defined(__WIN32__)
58 wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("\\"));
59 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
63 wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("/"));
64 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
68 while (CPPFilenameTokens.HasMoreTokens()){
70 CPPFilenameOnly = CPPFilenameTokens.GetNextToken();
74 while (DirFilenameTokens.HasMoreTokens()){
76 DirFilenameOnly = DirFilenameTokens.GetNextToken();
80 // Setup the PNG file reading and cpp file.
82 fstream cppfile, pngfile;
83 pngfile.open(PNGFilename.wc_str(), ios::in | ios::binary );
84 cppfile.open(CPPFilename.wc_str(), ios::out | ios::trunc );
86 outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
87 outname.Append(wxT("_"));
88 outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
90 outname.RemoveLast(4);
92 // Setup the inclusion guard.
94 cppfile << "#include <iostream>" << endl << endl;
95 cppfile << "#ifndef " << outname.c_str() << "_CPP" << endl;
96 cppfile << "#define " << outname.c_str() << "_CPP" << endl << endl;
99 outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
100 outname.Append(wxT("_"));
101 outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
102 outname.RemoveLast(4);
103 outname.Append(wxT("_png"));
106 // Convert the PNG file into an unsigned char array.
108 cppfile << "static unsigned char " << outname.c_str() <<
112 pngfile.get(pngfile_char);
115 if ((unsigned short)pngfile_char > 255){
117 cppfile << hex << setw(2) << setfill('0') <<
118 ((unsigned short)pngfile_char - 65280);
122 cppfile << hex << setw(2) << setfill('0') <<
123 (unsigned short)pngfile_char;
137 // End the file, close it and increment the counter.
139 cppfile << "};" << endl << endl;
140 cppfile << "#endif" << endl << endl;
143 cout << "CPP\t" << CPPFilename.c_str() << endl;
149 void CreateHPPFileDir(wxString dirfilename, wxArrayString filelist,
152 //-------------------------------------------------------------------
153 // CreateHPPFileDir: Create a HPP Directory with the directory
154 // filename and list of files in the directory
157 // dirfilename wxString of the directory name.
158 // filelist wxArrayString of the directory filelist.
159 // counter Pointer to a integer counter.
160 //-------------------------------------------------------------------
161 /* boostfs::path HPPFilename(dirfilename.c_str());
162 boostfs::path CPPFile;
163 boostfs::path CPPFileOnly(CPPFile.filename());
164 boostfs::path DirFilename(dirfilename.c_str());
165 boostfs::path DirFilenameOnly(DirFilename.filename());
166 HPPFilename.replace_extension(".h");*/
170 wxString finaldirname;
171 wxString HPPFilename = dirfilename.c_str();
173 wxString DirFilenameOnly;
174 wxString HPPFilenameOnly;
175 wxString CPPFilename;
177 #if defined(__HAIKU__)
179 #elif defined(__WIN32__)
181 wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("\\"));
182 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
186 wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("/"));
187 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
191 while (HPPFilenameTokens.HasMoreTokens()){
193 HPPFilenameOnly = HPPFilenameTokens.GetNextToken();
197 while (DirFilenameTokens.HasMoreTokens()){
199 DirFilenameOnly = DirFilenameTokens.GetNextToken();
203 // Make the directory filename upper case for writing to the
206 wxString CPPFileOnly;
208 finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
209 finaldirname.MakeUpper();
211 // Write out the header file inclusion guard.
213 HPPFilename.Append(wxT(".h"));
215 hppfile.open(HPPFilename.wc_str(), ios::out | ios::trunc );
217 hppfile << "#include <iostream>" << endl << endl;
218 hppfile << "#ifndef " << finaldirname.c_str() << "_H" << endl;
219 hppfile << "#define " << finaldirname.c_str() << "_H" << endl << endl;
220 hppfile << "// List all CPP files in the directory." << endl << endl;
222 // Write each CPP file into the header file.
224 for (int f = 0; f < filelist.GetCount() ; f++){
226 CPPFilename = filelist[f];
228 #if defined(__HAIKU__)
230 #elif defined(__WIN32__)
232 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("\\"));
236 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("/"));
240 while (CPPFilenameTokenizer.HasMoreTokens()){
242 CPPFileOnly = CPPFilenameTokenizer.GetNextToken();
246 CPPFileOnly.RemoveLast(4);
247 CPPFileOnly.Append(wxT(".cpp"));
249 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
250 "/" << CPPFileOnly.c_str() << "\"" << endl;
254 /*for (int f = 0; f < filelist.GetCount(); f++){
255 boostfs::path CPPFile(filelist[f].c_str());
256 boostfs::path CPPFileOnly(CPPFile.filename());
257 CPPFileOnly.replace_extension(".cpp");
259 for (boostfs::path::iterator filet = CPPFile.begin();
260 filet != CPPFile.end(); ++filet){
261 if (*filet == DirFilename.filename()){
270 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
271 "/" << CPPFileOnly.c_str() << "\"" << endl;
274 // Write the end if and close the file.
276 hppfile << endl << "#endif" << endl << endl;
279 // Increment the HPP file counter.
281 cout << "HPPDIR\t" << HPPFilename.c_str() << endl;
286 int main(int argc, char *argv[])
292 wxArrayString dirlist;
293 wxArrayString filelist;
294 wxString BitmapHeaderFilename;
295 wxString DirFilenameWxS;
297 // Check if completed file exists before doing anything
298 // else and write an error message if it does exist.
300 if (wxFileExists(wxT("bitmapsdone"))){
302 std::cout << "Bitmap files have already been generated!" << std::endl << std::endl;
303 std::cout << "To regenerate the files simply delete the bitmapsdone file where this helper application is run." << std::endl;
310 fstream finalhppfile;
313 std::cout << "Error: No directory name given!" << std::endl;
317 // Look in the subdirectories of the bitmaps directory and
318 // collect the names of the directories.
320 std::cout << "Working out directories in bitmaps directory..." <<
323 const char *dirarg = argv[1];
325 wxString BitmapDirName = wxString(dirarg, wxConvUTF8);
327 #if defined(__HAIKU__)
329 #elif defined(__WIN32__)
331 BitmapHeaderFilename.Append(BitmapDirName);
332 BitmapHeaderFilename.Append(wxT("\\..\\bitmaps.h"));
336 BitmapFilename.Append(BitmapDirName);
337 BitmapFilename.Append(wxT("/../bitmaps.h"));
341 /*boostfs::path BitmapsDir(argv[1]);
342 boostfs::path BitmapsDirSubName;
343 boostfs::path BitmapsFilename;
344 boostfs::directory_iterator dir_end;*/
346 if ( wxDirExists(BitmapDirName) ){
348 wxDir BitmapDir(BitmapDirName);
350 wxString BitmapSubDir;
351 wxString BitmapSubDirFull;
353 bool ContinueProcess = BitmapDir.GetFirst(&BitmapSubDir, wxEmptyString, wxDIR_DEFAULT);
355 while (ContinueProcess){
357 #if defined(__HAIKU__)
359 BitmapSubDirFull.Append(BitmapSubDir);
360 BitmapSubDirFull.Append();
362 #elif defined(__WIN32__)
364 BitmapSubDirFull.Append(BitmapDirName);
365 BitmapSubDirFull.Append(wxT("\\"));
366 BitmapSubDirFull.Append(BitmapSubDir);
370 BitmapSubDirFull.Append(BitmapDirName);
371 BitmapSubDirFull.Append(wxT("/"));
372 BitmapSubDirFull.Append(BitmapSubDirFull);
376 if (wxDirExists(BitmapSubDirFull)){
377 dirlist.Add(BitmapSubDirFull);
380 ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
382 BitmapSubDirFull.Clear();
386 /*for (boostfs::directory_iterator bitmapsidr_iter(BitmapsDir);
387 bitmapsidr_iter != dir_end; ++bitmapsidr_iter){
389 if (boostfs::is_directory(bitmapsidr_iter->status())){
391 BitmapsDirSubName = boostfs::path(bitmapsidr_iter->path()).filename();
392 DirFilenameWxS.Append(wxString::FromUTF8(argv[1]));
393 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsDirSubName.c_str()));
394 dirlist.Add(DirFilenameWxS, 1);
395 DirFilenameWxS = wxT("");
402 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
406 // Process each directory, generating a .cpp and .hpp file
407 // for each image and then a final .hpp for the directory
408 // containing the .hpp's for the directories.
410 if (dirlist.GetCount() == 0){
411 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
415 std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
417 DirFilenameWxS.Empty();
419 std::cout << BitmapHeaderFilename.c_str() << std::endl;
421 finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
422 finalhppfile << "#include <iostream>" << endl << endl;
423 finalhppfile << "#ifndef BITMAPS_H" << endl;
424 finalhppfile << "#define BITMAPS_H" << endl << endl;
426 for (int bi = 0; bi < dirlist.GetCount(); bi++)
429 wxString BitmapSubDirName = wxString::FromUTF8(dirlist[bi]);
430 wxString BitmapFilename;
431 wxDir BitmapSubDir(BitmapSubDirName);
433 bool ContinueProcess = BitmapSubDir.GetFirst(&BitmapFilename, wxEmptyString, wxDIR_DEFAULT);
435 while (ContinueProcess){
437 if (BitmapFilename.Right(4) == wxT(".PNG") ||
438 BitmapFilename.Right(4) == wxT(".png")){
440 #if defined(__HAIKU__)
442 BitmapSubDirFull.Append(BitmapSubDir);
443 BitmapSubDirFull.Append();
445 #elif defined(__WIN32__)
447 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
448 DirFilenameWxS.Append(wxT("\\"));
449 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
453 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
454 DirFilenameWxS.Append(wxString::FromUTF8("/"));
455 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
459 filelist.Add(DirFilenameWxS);
463 DirFilenameWxS.Clear();
464 ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
468 /*boostfs::path BitmapsSubDir(dirlist[bi].c_str());
469 for (boostfs::directory_iterator bitmapsidr_iter(BitmapsSubDir);
470 bitmapsidr_iter != dir_end;
473 if (boostfs::path(bitmapsidr_iter->path()).extension() == ".png" ||
474 boostfs::path(bitmapsidr_iter->path()).extension() == ".PNG"){
476 BitmapsFilename = boostfs::path(bitmapsidr_iter->path()).filename();
477 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].mb_str()));
478 DirFilenameWxS.Append(wxString::FromUTF8("/"));
479 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsFilename.c_str()));
481 filelist.Add(DirFilenameWxS, 1);
482 DirFilenameWxS = wxT("");
490 for (int fi = 0; fi < filelist.GetCount(); fi++)
492 CreateCPPFile(dirlist[bi].wc_str(), filelist[fi].wc_str(), &cppg);
496 if (filelist.GetCount() > 0)
498 CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
503 wxString DirNameSplit;
505 #if defined(__HAIKU__)
507 #elif defined(__WIN32__)
509 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
513 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
518 while (DirListFile.HasMoreTokens()){
520 DirNameSplit = DirListFile.GetNextToken();
524 #if defined(__HAIKU__)
526 #elif defined(__WIN32__)
528 finalhppfile << "#include \"bitmaps\\" << DirNameSplit.c_str() <<
533 finalhppfile << "#include \"bitmaps/" << DirNameSplit.c_str() <<
540 finalhppfile << endl << "#endif" << endl;
541 finalhppfile.close();
544 // Print out the results.
546 std::cout << "Finished processing PNGs into code." << std::endl;
547 std::cout << fp << " files processed." << std::endl;
548 std::cout << cppg << " .cpp files generated." << std::endl;
549 std::cout << hppg << " .hpp files generated." << std::endl;
551 // Write a success flag so that future runs won't take
552 // place thus speeding up the compilation process as
557 bitmapflag.open("bitmapsdone", ios::out | ios::trunc);
558 bitmapflag << "Bitmaps as code generated. To recreate, simply delete this file and run the bitmap code generation tool again." << endl;