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;
84 #if defined(__WIN32__)
86 pngfile.open(PNGFilename.wc_str(), ios::in | ios::binary );
87 cppfile.open(CPPFilename.wc_str(), ios::out | ios::trunc );
91 pngfile.open(PNGFilename.c_str(), ios::in | ios::binary );
92 cppfile.open(CPPFilename.c_str(), ios::out | ios::trunc );
96 outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
97 outname.Append(wxT("_"));
98 outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
100 outname.RemoveLast(4);
102 // Setup the inclusion guard.
104 cppfile << "#include <iostream>" << endl << endl;
105 cppfile << "#ifndef " << outname.c_str() << "_CPP" << endl;
106 cppfile << "#define " << outname.c_str() << "_CPP" << endl << endl;
109 outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
110 outname.Append(wxT("_"));
111 outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
112 outname.RemoveLast(4);
113 outname.Append(wxT("_png"));
116 // Convert the PNG file into an unsigned char array.
118 cppfile << "static unsigned char " << outname.c_str() <<
122 pngfile.get(pngfile_char);
125 if ((unsigned short)pngfile_char > 255){
127 cppfile << hex << setw(2) << setfill('0') <<
128 ((unsigned short)pngfile_char - 65280);
132 cppfile << hex << setw(2) << setfill('0') <<
133 (unsigned short)pngfile_char;
147 // End the file, close it and increment the counter.
149 cppfile << "};" << endl << endl;
150 cppfile << "#endif" << endl << endl;
153 cout << "CPP\t" << CPPFilename.c_str() << endl;
159 void CreateHPPFileDir(wxString dirfilename, wxArrayString filelist,
162 //-------------------------------------------------------------------
163 // CreateHPPFileDir: Create a HPP Directory with the directory
164 // filename and list of files in the directory
167 // dirfilename wxString of the directory name.
168 // filelist wxArrayString of the directory filelist.
169 // counter Pointer to a integer counter.
170 //-------------------------------------------------------------------
171 /* boostfs::path HPPFilename(dirfilename.c_str());
172 boostfs::path CPPFile;
173 boostfs::path CPPFileOnly(CPPFile.filename());
174 boostfs::path DirFilename(dirfilename.c_str());
175 boostfs::path DirFilenameOnly(DirFilename.filename());
176 HPPFilename.replace_extension(".h");*/
180 wxString finaldirname;
181 wxString HPPFilename = dirfilename.c_str();
183 wxString DirFilenameOnly;
184 wxString HPPFilenameOnly;
185 wxString CPPFilename;
187 #if defined(__WIN32__)
189 wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("\\"));
190 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
194 wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("/"));
195 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
199 while (HPPFilenameTokens.HasMoreTokens()){
201 HPPFilenameOnly = HPPFilenameTokens.GetNextToken();
205 while (DirFilenameTokens.HasMoreTokens()){
207 DirFilenameOnly = DirFilenameTokens.GetNextToken();
211 // Make the directory filename upper case for writing to the
214 wxString CPPFileOnly;
216 finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
217 finaldirname.MakeUpper();
219 // Write out the header file inclusion guard.
221 HPPFilename.Append(wxT(".h"));
223 #if defined(__WIN32__)
225 hppfile.open(HPPFilename.wc_str(), ios::out | ios::trunc );
229 hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc );
233 hppfile << "#include <iostream>" << endl << endl;
234 hppfile << "#ifndef " << finaldirname.c_str() << "_H" << endl;
235 hppfile << "#define " << finaldirname.c_str() << "_H" << endl << endl;
236 hppfile << "// List all CPP files in the directory." << endl << endl;
238 // Write each CPP file into the header file.
240 for (int f = 0; f < filelist.GetCount() ; f++){
242 CPPFilename = filelist[f];
244 #if defined(__HAIKU__)
246 #elif defined(__WIN32__)
248 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("\\"));
252 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("/"));
256 while (CPPFilenameTokenizer.HasMoreTokens()){
258 CPPFileOnly = CPPFilenameTokenizer.GetNextToken();
262 CPPFileOnly.RemoveLast(4);
263 CPPFileOnly.Append(wxT(".cpp"));
265 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
266 "/" << CPPFileOnly.c_str() << "\"" << endl;
270 /*for (int f = 0; f < filelist.GetCount(); f++){
271 boostfs::path CPPFile(filelist[f].c_str());
272 boostfs::path CPPFileOnly(CPPFile.filename());
273 CPPFileOnly.replace_extension(".cpp");
275 for (boostfs::path::iterator filet = CPPFile.begin();
276 filet != CPPFile.end(); ++filet){
277 if (*filet == DirFilename.filename()){
286 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
287 "/" << CPPFileOnly.c_str() << "\"" << endl;
290 // Write the end if and close the file.
292 hppfile << endl << "#endif" << endl << endl;
295 // Increment the HPP file counter.
297 cout << "HPPDIR\t" << HPPFilename.c_str() << endl;
302 int main(int argc, char *argv[])
308 wxArrayString dirlist;
309 wxArrayString filelist;
310 wxString BitmapHeaderFilename;
311 wxString DirFilenameWxS;
313 // Check if completed file exists before doing anything
314 // else and write an error message if it does exist.
316 if (wxFileExists(wxT("bitmapsdone"))){
318 std::cout << "Bitmap files have already been generated!" << std::endl << std::endl;
319 std::cout << "To regenerate the files simply delete the bitmapsdone file where this helper application is run." << std::endl;
326 fstream finalhppfile;
329 std::cout << "Error: No directory name given!" << std::endl;
333 // Look in the subdirectories of the bitmaps directory and
334 // collect the names of the directories.
336 std::cout << "Working out directories in bitmaps directory..." <<
339 const char *dirarg = argv[1];
341 wxString BitmapDirName = wxString(dirarg, wxConvUTF8);
343 #if defined(__HAIKU__)
345 #elif defined(__WIN32__)
347 BitmapHeaderFilename.Append(BitmapDirName);
348 BitmapHeaderFilename.Append(wxT("\\..\\bitmaps.h"));
352 BitmapHeaderFilename.Append(BitmapDirName);
353 BitmapHeaderFilename.Append(wxT("/../bitmaps.h"));
357 /*boostfs::path BitmapsDir(argv[1]);
358 boostfs::path BitmapsDirSubName;
359 boostfs::path BitmapsFilename;
360 boostfs::directory_iterator dir_end;*/
362 if ( wxDirExists(BitmapDirName) ){
364 wxDir BitmapDir(BitmapDirName);
366 wxString BitmapSubDir;
367 wxString BitmapSubDirFull;
369 bool ContinueProcess = BitmapDir.GetFirst(&BitmapSubDir, wxEmptyString, wxDIR_DEFAULT);
371 while (ContinueProcess){
373 #if defined(__HAIKU__)
375 BitmapSubDirFull.Append(BitmapSubDir);
376 BitmapSubDirFull.Append();
378 #elif defined(__WIN32__)
380 BitmapSubDirFull.Append(BitmapDirName);
381 BitmapSubDirFull.Append(wxT("\\"));
382 BitmapSubDirFull.Append(BitmapSubDir);
386 BitmapSubDirFull.Append(BitmapDirName);
387 BitmapSubDirFull.Append(wxT("/"));
388 BitmapSubDirFull.Append(BitmapSubDir);
392 if (wxDirExists(BitmapSubDirFull)){
393 dirlist.Add(BitmapSubDirFull);
396 ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
398 BitmapSubDirFull.Clear();
402 /*for (boostfs::directory_iterator bitmapsidr_iter(BitmapsDir);
403 bitmapsidr_iter != dir_end; ++bitmapsidr_iter){
405 if (boostfs::is_directory(bitmapsidr_iter->status())){
407 BitmapsDirSubName = boostfs::path(bitmapsidr_iter->path()).filename();
408 DirFilenameWxS.Append(wxString::FromUTF8(argv[1]));
409 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsDirSubName.c_str()));
410 dirlist.Add(DirFilenameWxS, 1);
411 DirFilenameWxS = wxT("");
418 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
422 // Process each directory, generating a .cpp and .hpp file
423 // for each image and then a final .hpp for the directory
424 // containing the .hpp's for the directories.
426 if (dirlist.GetCount() == 0){
427 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
431 std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
433 DirFilenameWxS.Empty();
435 std::cout << BitmapHeaderFilename.c_str() << std::endl;
437 #if defined(__WIN32__)
439 finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
443 finalhppfile.open(BitmapHeaderFilename.c_str(), ios::out | ios::trunc);
447 finalhppfile << "#include <iostream>" << endl << endl;
448 finalhppfile << "#ifndef BITMAPS_H" << endl;
449 finalhppfile << "#define BITMAPS_H" << endl << endl;
451 for (int bi = 0; bi < dirlist.GetCount(); bi++)
454 wxString BitmapSubDirName = dirlist[bi];
455 wxString BitmapFilename;
456 wxDir BitmapSubDir(BitmapSubDirName);
458 bool ContinueProcess = BitmapSubDir.GetFirst(&BitmapFilename, wxEmptyString, wxDIR_DEFAULT);
460 while (ContinueProcess){
462 if (BitmapFilename.Right(4) == wxT(".PNG") ||
463 BitmapFilename.Right(4) == wxT(".png")){
465 #if defined(__HAIKU__)
467 BitmapSubDirFull.Append(BitmapSubDir);
468 BitmapSubDirFull.Append();
470 #elif defined(__WIN32__)
472 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
473 DirFilenameWxS.Append(wxT("\\"));
474 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
478 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
479 DirFilenameWxS.Append(wxString::FromUTF8("/"));
480 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
484 filelist.Add(DirFilenameWxS);
488 DirFilenameWxS.Clear();
489 ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
493 /*boostfs::path BitmapsSubDir(dirlist[bi].c_str());
494 for (boostfs::directory_iterator bitmapsidr_iter(BitmapsSubDir);
495 bitmapsidr_iter != dir_end;
498 if (boostfs::path(bitmapsidr_iter->path()).extension() == ".png" ||
499 boostfs::path(bitmapsidr_iter->path()).extension() == ".PNG"){
501 BitmapsFilename = boostfs::path(bitmapsidr_iter->path()).filename();
502 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].mb_str()));
503 DirFilenameWxS.Append(wxString::FromUTF8("/"));
504 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsFilename.c_str()));
506 filelist.Add(DirFilenameWxS, 1);
507 DirFilenameWxS = wxT("");
515 for (int fi = 0; fi < filelist.GetCount(); fi++)
517 CreateCPPFile(dirlist[bi].wc_str(), filelist[fi].wc_str(), &cppg);
521 if (filelist.GetCount() > 0)
523 CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
528 wxString DirNameSplit;
530 #if defined(__HAIKU__)
532 #elif defined(__WIN32__)
534 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
538 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
543 while (DirListFile.HasMoreTokens()){
545 DirNameSplit = DirListFile.GetNextToken();
549 #if defined(__HAIKU__)
551 #elif defined(__WIN32__)
553 finalhppfile << "#include \"bitmaps\\" << DirNameSplit.c_str() <<
558 finalhppfile << "#include \"bitmaps/" << DirNameSplit.c_str() <<
565 finalhppfile << endl << "#endif" << endl;
566 finalhppfile.close();
569 // Print out the results.
571 std::cout << "Finished processing PNGs into code." << std::endl;
572 std::cout << fp << " files processed." << std::endl;
573 std::cout << cppg << " .cpp files generated." << std::endl;
574 std::cout << hppg << " .hpp files generated." << std::endl;
576 // Write a success flag so that future runs won't take
577 // place thus speeding up the compilation process as
582 bitmapflag.open("bitmapsdone", ios::out | ios::trunc);
583 bitmapflag << "Bitmaps as code generated. To recreate, simply delete this file and run the bitmap code generation tool again." << endl;