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 header
6 // files and library files are there.
8 // This file is licenced under the GNU General Public License
9 // version 3 only. (GPLv3 only).
10 //-------------------------------------------------------------------
18 #include <wx/tokenzr.h>
23 void CreateCPPFile(wxString dirfilename, wxString PNGFilename, int* counter)
25 //-------------------------------------------------------------------
26 // CreateCPPFile: Create a CPP file from the filename given.
28 // dirfilename wxString of the directory name.
29 // filename wxString of the filename.
30 // counter Pointer to a integer counter.
31 //-------------------------------------------------------------------
37 CPPFilename = PNGFilename;
38 CPPFilename.RemoveLast(4);
39 CPPFilename.Append(wxT(".cpp"));
41 wxString DirFilenameOnly;
42 wxString CPPFilenameOnly;
44 #if defined(__HAIKU__)
46 #elif defined(__WIN32__)
48 wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("\\"));
49 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
53 wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("/"));
54 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
58 while (CPPFilenameTokens.HasMoreTokens()){
60 CPPFilenameOnly = CPPFilenameTokens.GetNextToken();
64 while (DirFilenameTokens.HasMoreTokens()){
66 DirFilenameOnly = DirFilenameTokens.GetNextToken();
70 // Setup the PNG file reading and cpp file.
72 fstream cppfile, pngfile;
74 #if defined(__WIN32__)
76 pngfile.open(PNGFilename.wc_str(), ios::in | ios::binary );
77 cppfile.open(CPPFilename.wc_str(), ios::out | ios::trunc );
81 pngfile.open(PNGFilename.c_str(), ios::in | ios::binary );
82 cppfile.open(CPPFilename.c_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 //-------------------------------------------------------------------
165 wxString finaldirname;
166 wxString HPPFilename = dirfilename.c_str();
168 wxString DirFilenameOnly;
169 wxString HPPFilenameOnly;
170 wxString CPPFilename;
172 #if defined(__WIN32__)
174 wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("\\"));
175 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
179 wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("/"));
180 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
184 while (HPPFilenameTokens.HasMoreTokens()){
186 HPPFilenameOnly = HPPFilenameTokens.GetNextToken();
190 while (DirFilenameTokens.HasMoreTokens()){
192 DirFilenameOnly = DirFilenameTokens.GetNextToken();
196 // Make the directory filename upper case for writing to the
199 wxString CPPFileOnly;
201 finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
202 finaldirname.MakeUpper();
204 // Write out the header file inclusion guard.
206 HPPFilename.Append(wxT(".h"));
208 #if defined(__WIN32__)
210 hppfile.open(HPPFilename.wc_str(), ios::out | ios::trunc );
214 hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc );
218 hppfile << "#include <iostream>" << endl << endl;
219 hppfile << "#ifndef " << finaldirname.c_str() << "_H" << endl;
220 hppfile << "#define " << finaldirname.c_str() << "_H" << endl << endl;
221 hppfile << "// List all CPP files in the directory." << endl << endl;
223 // Write each CPP file into the header file.
225 for (int f = 0; f < filelist.GetCount() ; f++){
227 CPPFilename = filelist[f];
229 #if defined(__HAIKU__)
231 #elif defined(__WIN32__)
233 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("\\"));
237 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("/"));
241 while (CPPFilenameTokenizer.HasMoreTokens()){
243 CPPFileOnly = CPPFilenameTokenizer.GetNextToken();
247 CPPFileOnly.RemoveLast(4);
248 CPPFileOnly.Append(wxT(".cpp"));
250 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
251 "/" << CPPFileOnly.c_str() << "\"" << endl;
255 // Write the end if and close the file.
257 hppfile << endl << "#endif" << endl << endl;
260 // Increment the HPP file counter.
262 cout << "HPPDIR\t" << HPPFilename.c_str() << endl;
267 int main(int argc, char *argv[])
274 wxArrayString dirlist;
275 wxArrayString filelist;
276 wxString BitmapHeaderFilename;
277 wxString DirFilenameWxS;
279 // Check if completed file exists before doing anything
280 // else and write an error message if it does exist.
282 if (wxFileExists(wxT("bitmapsdone"))){
284 std::cout << "Bitmap files have already been generated!" << std::endl << std::endl;
285 std::cout << "To regenerate the files simply delete the bitmapsdone file where this helper application is run." << std::endl;
292 fstream finalhppfile;
295 std::cout << "Error: No directory name given!" << std::endl;
299 // Look in the subdirectories of the bitmaps directory and
300 // collect the names of the directories.
302 std::cout << "Working out directories in bitmaps directory..." <<
305 const char *dirarg = argv[1];
307 wxString BitmapDirName = wxString(dirarg, wxConvUTF8);
309 #if defined(__HAIKU__)
311 #elif defined(__WIN32__)
313 BitmapHeaderFilename.Append(BitmapDirName);
314 BitmapHeaderFilename.Append(wxT("\\..\\bitmaps.h"));
318 BitmapHeaderFilename.Append(BitmapDirName);
319 BitmapHeaderFilename.Append(wxT("/../bitmaps.h"));
323 if ( wxDirExists(BitmapDirName) ){
325 wxDir BitmapDir(BitmapDirName);
327 wxString BitmapSubDir;
328 wxString BitmapSubDirFull;
330 bool ContinueProcess = BitmapDir.GetFirst(&BitmapSubDir, wxEmptyString, wxDIR_DEFAULT);
332 while (ContinueProcess){
334 #if defined(__HAIKU__)
336 BitmapSubDirFull.Append(BitmapSubDir);
337 BitmapSubDirFull.Append();
339 #elif defined(__WIN32__)
341 BitmapSubDirFull.Append(BitmapDirName);
342 BitmapSubDirFull.Append(wxT("\\"));
343 BitmapSubDirFull.Append(BitmapSubDir);
347 BitmapSubDirFull.Append(BitmapDirName);
348 BitmapSubDirFull.Append(wxT("/"));
349 BitmapSubDirFull.Append(BitmapSubDir);
353 if (wxDirExists(BitmapSubDirFull)){
354 dirlist.Add(BitmapSubDirFull);
357 ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
359 BitmapSubDirFull.Clear();
364 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
368 // Process each directory, generating a .cpp and .hpp file
369 // for each image and then a final .hpp for the directory
370 // containing the .hpp's for the directories.
372 if (dirlist.GetCount() == 0){
373 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
377 std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
379 DirFilenameWxS.Empty();
381 std::cout << BitmapHeaderFilename.c_str() << std::endl;
383 #if defined(__WIN32__)
385 finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
389 finalhppfile.open(BitmapHeaderFilename.c_str(), ios::out | ios::trunc);
393 finalhppfile << "#include <iostream>" << endl << endl;
394 finalhppfile << "#ifndef BITMAPS_H" << endl;
395 finalhppfile << "#define BITMAPS_H" << endl << endl;
397 for (int bi = 0; bi < dirlist.GetCount(); bi++)
400 wxString BitmapSubDirName = dirlist[bi];
401 wxString BitmapFilename;
402 wxDir BitmapSubDir(BitmapSubDirName);
404 bool ContinueProcess = BitmapSubDir.GetFirst(&BitmapFilename, wxEmptyString, wxDIR_DEFAULT);
406 while (ContinueProcess){
408 if (BitmapFilename.Right(4) == wxT(".PNG") ||
410 BitmapFilename.Right(4) == wxT(".png")){
412 #if defined(__HAIKU__)
414 BitmapSubDirFull.Append(BitmapSubDir);
415 BitmapSubDirFull.Append();
417 #elif defined(__WIN32__)
419 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
420 DirFilenameWxS.Append(wxT("\\"));
421 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
425 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
426 DirFilenameWxS.Append(wxString::FromUTF8("/"));
427 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
431 filelist.Add(DirFilenameWxS);
435 DirFilenameWxS.Clear();
436 ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
440 for (int fi = 0; fi < filelist.GetCount(); fi++)
443 CreateCPPFile(dirlist[bi].wc_str(), filelist[fi].wc_str(), &cppg);
448 if (filelist.GetCount() > 0)
451 CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
457 wxString DirNameSplit;
459 #if defined(__HAIKU__)
461 #elif defined(__WIN32__)
463 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
467 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
472 while (DirListFile.HasMoreTokens()){
474 DirNameSplit = DirListFile.GetNextToken();
478 #if defined(__HAIKU__)
480 #elif defined(__WIN32__)
482 finalhppfile << "#include \"bitmaps\\" << DirNameSplit.c_str() <<
487 finalhppfile << "#include \"bitmaps/" << DirNameSplit.c_str() <<
494 finalhppfile << endl << "#endif" << endl;
495 finalhppfile.close();
498 // Print out the results.
500 std::cout << "Finished processing PNGs into code." << std::endl;
501 std::cout << fp << " files processed." << std::endl;
502 std::cout << cppg << " .cpp files generated." << std::endl;
503 std::cout << hppg << " .hpp files generated." << std::endl;
505 // Write a success flag so that future runs won't take
506 // place thus speeding up the compilation process as
511 bitmapflag.open("bitmapsdone", ios::out | ios::trunc);
512 bitmapflag << "Bitmaps as code generated. To recreate, simply delete this file and run the bitmap code generation tool again." << endl;