//------------------------------------------------------------------- // bitmapcode: Helper application which coverts PNG files into // C++ code outputting binary code. // // This application also is a simple test if the wxWidgets & Boost // header files and library files are there. // // This file is licenced under the GNU General Public License // version 3 only. (GPLv3 only). //------------------------------------------------------------------- #include #include #include #include #include #include #include #include using namespace std; namespace boostfs = boost::filesystem; void CreateCPPFile(wxString dirfilename, wxString filename, int* counter) { //------------------------------------------------------------------- // CreateCPPFile: Create a CPP file from the filename given. // // dirfilename wxString of the directory name. // filename wxString of the filename. // counter Pointer to a integer counter. //------------------------------------------------------------------- char pngfile_char; int pageseek = 0; boostfs::path PNGFilename(filename.c_str()); boostfs::path CPPFilename = PNGFilename; boostfs::path CPPFilenameOnly(CPPFilename.filename()); boostfs::path DirFilename(dirfilename.c_str()); boostfs::path DirFilenameOnly(DirFilename.filename()); CPPFilename.replace_extension(".cpp"); wxString outname; // Setup the PNG file reading and cpp file. fstream cppfile, pngfile; pngfile.open(PNGFilename.c_str(), ios::in | ios::binary ); cppfile.open(CPPFilename.c_str(), ios::out | ios::trunc ); CPPFilenameOnly.replace_extension(); outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str())); outname.Append(wxT("_")); outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str())); outname.MakeUpper(); // Setup the inclusion guard. cppfile << "#include " << endl << endl; cppfile << "#ifndef " << outname.mb_str() << "_CPP" << endl; cppfile << "#define " << outname.mb_str() << "_CPP" << endl << endl; outname.Clear(); outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str())); outname.Append(wxT("_")); outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str())); outname.Append(wxT("_png")); outname.MakeLower(); // Convert the PNG file into an unsigned char array. cppfile << "static unsigned char " << outname.mb_str() << "[] = {" << endl; while(pngfile){ pngfile.get(pngfile_char); cppfile << "0x"; if ((unsigned short)pngfile_char > 255){ cppfile << hex << setw(2) << setfill('0') << ((unsigned short)pngfile_char - 65280); } else { cppfile << hex << setw(2) << setfill('0') << (unsigned short)pngfile_char; } cppfile << ", "; pageseek++; if (pageseek == 8){ cppfile << endl; pageseek = 0; } } // End the file, close it and increment the counter. cppfile << "};" << endl << endl; cppfile << "#endif" << endl << endl; cppfile.close(); cout << "CPP\t" << CPPFilename.c_str() << endl; ++*counter; } int CreateHPPFileDir(wxString dirfilename, wxArrayString filelist, int* counter) { //------------------------------------------------------------------- // CreateHPPFileDir: Create a HPP Directory with the directory // filename and list of files in the directory // given. // // dirfilename wxString of the directory name. // filelist wxArrayString of the directory filelist. // counter Pointer to a integer counter. //------------------------------------------------------------------- boostfs::path HPPFilename(dirfilename.c_str()); boostfs::path CPPFile; boostfs::path CPPFileOnly(CPPFile.filename()); boostfs::path DirFilename(dirfilename.c_str()); boostfs::path DirFilenameOnly(DirFilename.filename()); HPPFilename.replace_extension(".h"); fstream hppfile; bool fmatch = FALSE; wxString finaldirname; // Make the directory filename upper case for writing to the // header file. finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str())); finaldirname.MakeUpper(); // Write out the header file inclusion guard. hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc ); hppfile << "#include " << endl << endl; hppfile << "#ifndef " << finaldirname.mb_str() << "_H" << endl; hppfile << "#define " << finaldirname.mb_str() << "_H" << endl << endl; hppfile << "// List all CPP files in the directory." << endl << endl; // Write each CPP file into the header file. for (int f = 0; f < filelist.GetCount() ; f++){ boostfs::path CPPFile(filelist[f].c_str()); boostfs::path CPPFileOnly(CPPFile.filename()); CPPFileOnly.replace_extension(".cpp"); for (boostfs::path::iterator filet=CPPFile.begin(); filet != CPPFile.end(); ++filet){ if (*filet == DirFilename.filename()){ fmatch = TRUE; } if (fmatch == TRUE){ } } hppfile << "#include \"" << DirFilenameOnly.c_str() << "/" << CPPFileOnly.c_str() << "\"" << endl; } // Write the end if and close the file. hppfile << endl << "#endif" << endl << endl; hppfile.close(); // Increment the HPP file counter. cout << "HPPDIR\t" << HPPFilename.c_str() << endl; ++*counter; } int main() { int fp = 0; int cppg = 0; int hppg = 0; wxArrayString dirlist; wxArrayString filelist; wxString DirFilenameWxS; fstream finalhppfile; // Look in the subdirectories of the bitmaps directory and // collect the names of the directories. std::cout << "Working out directories in bitmaps directory..." << std::endl; boostfs::path BitmapsDir("../bitmaps"); boostfs::path BitmapsDirSubName; boostfs::path BitmapsFilename; boostfs::directory_iterator dir_end; if ( boostfs::exists(BitmapsDir) ){ for (boostfs::directory_iterator bitmapsidr_iter(BitmapsDir); bitmapsidr_iter != dir_end ; ++bitmapsidr_iter){ if (boostfs::is_directory(bitmapsidr_iter->status())){ BitmapsDirSubName = boostfs::path(bitmapsidr_iter->path()).filename(); DirFilenameWxS.Append(wxString::FromUTF8("../bitmaps/")); DirFilenameWxS.Append(wxString::FromUTF8(BitmapsDirSubName.c_str())); dirlist.Add(DirFilenameWxS, 1); DirFilenameWxS = wxT(""); } } } else { std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl; return 1; } // Process each directory, generating a .cpp and .hpp file // for each image and then a final .hpp for the directory // containing the .hpp's for the directories. if (dirlist.GetCount() == 0){ cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl; return 1; } std::cout << "Looking in bitmaps folder for PNGs..." << std::endl; DirFilenameWxS.Empty(); finalhppfile.open("../bitmaps.h", ios::out | ios::trunc ); finalhppfile << "#include " << endl << endl; finalhppfile << "#ifndef BITMAPS_H" << endl; finalhppfile << "#define BITMAPS_H" << endl << endl; for (int bi = 0; bipath()).extension() == ".png" || boostfs::path(bitmapsidr_iter->path()).extension() == ".PNG" ){ BitmapsFilename = boostfs::path(bitmapsidr_iter->path()).filename(); DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].mb_str())); DirFilenameWxS.Append(wxString::FromUTF8("/")); DirFilenameWxS.Append(wxString::FromUTF8(BitmapsFilename.c_str())); filelist.Add(DirFilenameWxS, 1); DirFilenameWxS = wxT(""); } } for (int fi = 0; fi < filelist.GetCount() ; fi++) { CreateCPPFile(dirlist[bi].c_str(), filelist[fi], &cppg); fp++; } if (filelist.GetCount() > 0) { CreateHPPFileDir(dirlist[bi].c_str(), filelist, &hppg); } filelist.Clear(); finalhppfile << "#include \"bitmaps/"<< BitmapsSubDir.filename().c_str() << ".h\"" << endl; } finalhppfile << endl << "#endif" << endl; finalhppfile.close(); ++hppg; // Print out the results. std::cout << "Finished processing PNGs into code." << std::endl; std::cout << fp << " files processed." << std::endl; std::cout << cppg << " .cpp files generated." << std::endl; std::cout << hppg << " .hpp files generated." << std::endl; return 0; }