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 //-------------------------------------------------------------------
16 #include <boost/filesystem.hpp>
17 #include <boost/filesystem/path.hpp>
18 #include <boost/filesystem/fstream.hpp>
23 namespace boostfs = boost::filesystem;
25 void CreateCPPFile(wxString dirfilename, wxString filename, int* counter)
27 //-------------------------------------------------------------------
28 // CreateCPPFile: Create a CPP file from the filename given.
30 // dirfilename wxString of the directory name.
31 // filename wxString of the filename.
32 // counter Pointer to a integer counter.
33 //-------------------------------------------------------------------
36 boostfs::path PNGFilename(filename.c_str());
37 boostfs::path CPPFilename = PNGFilename;
38 boostfs::path CPPFilenameOnly(CPPFilename.filename());
39 boostfs::path DirFilename(dirfilename.c_str());
40 boostfs::path DirFilenameOnly(DirFilename.filename());
41 CPPFilename.replace_extension(".cpp");
44 // Setup the PNG file reading and cpp file.
46 fstream cppfile, pngfile;
47 pngfile.open(PNGFilename.c_str(), ios::in | ios::binary );
48 cppfile.open(CPPFilename.c_str(), ios::out | ios::trunc );
50 CPPFilenameOnly.replace_extension();
52 outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
53 outname.Append(wxT("_"));
54 outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
57 // Setup the inclusion guard.
59 cppfile << "#include <iostream>" << endl << endl;
60 cppfile << "#ifndef " << outname.mb_str() << "_CPP" << endl;
61 cppfile << "#define " << outname.mb_str() << "_CPP" << endl << endl;
64 outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
65 outname.Append(wxT("_"));
66 outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
67 outname.Append(wxT("_png"));
70 // Convert the PNG file into an unsigned char array.
72 cppfile << "static unsigned char " << outname.mb_str() <<
76 pngfile.get(pngfile_char);
79 if ((unsigned short)pngfile_char > 255){
81 cppfile << hex << setw(2) << setfill('0') <<
82 ((unsigned short)pngfile_char - 65280);
86 cppfile << hex << setw(2) << setfill('0') <<
87 (unsigned short)pngfile_char;
100 // End the file, close it and increment the counter.
102 cppfile << "};" << endl << endl;
103 cppfile << "#endif" << endl << endl;
106 cout << "CPP\t" << CPPFilename.c_str() << endl;
112 int CreateHPPFileDir(wxString dirfilename, wxArrayString filelist,
115 //-------------------------------------------------------------------
116 // CreateHPPFileDir: Create a HPP Directory with the directory
117 // filename and list of files in the directory
120 // dirfilename wxString of the directory name.
121 // filelist wxArrayString of the directory filelist.
122 // counter Pointer to a integer counter.
123 //-------------------------------------------------------------------
124 boostfs::path HPPFilename(dirfilename.c_str());
125 boostfs::path CPPFile;
126 boostfs::path CPPFileOnly(CPPFile.filename());
127 boostfs::path DirFilename(dirfilename.c_str());
128 boostfs::path DirFilenameOnly(DirFilename.filename());
129 HPPFilename.replace_extension(".h");
133 wxString finaldirname;
135 // Make the directory filename upper case for writing to the
138 finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
139 finaldirname.MakeUpper();
141 // Write out the header file inclusion guard.
143 hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc );
145 hppfile << "#include <iostream>" << endl << endl;
146 hppfile << "#ifndef " << finaldirname.mb_str() << "_H" << endl;
147 hppfile << "#define " << finaldirname.mb_str() << "_H" << endl << endl;
148 hppfile << "// List all CPP files in the directory." << endl << endl;
150 // Write each CPP file into the header file.
152 for (int f = 0; f < filelist.GetCount() ; f++){
153 boostfs::path CPPFile(filelist[f].c_str());
154 boostfs::path CPPFileOnly(CPPFile.filename());
155 CPPFileOnly.replace_extension(".cpp");
157 for (boostfs::path::iterator filet=CPPFile.begin();
158 filet != CPPFile.end(); ++filet){
159 if (*filet == DirFilename.filename()){
168 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
169 "/" << CPPFileOnly.c_str() << "\"" << endl;
172 // Write the end if and close the file.
174 hppfile << endl << "#endif" << endl << endl;
177 // Increment the HPP file counter.
179 cout << "HPPDIR\t" << HPPFilename.c_str() << endl;
190 wxArrayString dirlist;
191 wxArrayString filelist;
192 wxString DirFilenameWxS;
194 fstream finalhppfile;
196 // Look in the subdirectories of the bitmaps directory and
197 // collect the names of the directories.
199 std::cout << "Working out directories in bitmaps directory..." <<
202 boostfs::path BitmapsDir("../bitmaps");
203 boostfs::path BitmapsDirSubName;
204 boostfs::path BitmapsFilename;
205 boostfs::directory_iterator dir_end;
207 if ( boostfs::exists(BitmapsDir) ){
209 for (boostfs::directory_iterator bitmapsidr_iter(BitmapsDir);
210 bitmapsidr_iter != dir_end ; ++bitmapsidr_iter){
212 if (boostfs::is_directory(bitmapsidr_iter->status())){
214 BitmapsDirSubName = boostfs::path(bitmapsidr_iter->path()).filename();
215 DirFilenameWxS.Append(wxString::FromUTF8("../bitmaps/"));
216 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsDirSubName.c_str()));
217 dirlist.Add(DirFilenameWxS, 1);
218 DirFilenameWxS = wxT("");
225 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
229 // Process each directory, generating a .cpp and .hpp file
230 // for each image and then a final .hpp for the directory
231 // containing the .hpp's for the directories.
233 if (dirlist.GetCount() == 0){
234 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
238 std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
240 DirFilenameWxS.Empty();
242 finalhppfile.open("../bitmaps.h", ios::out | ios::trunc );
243 finalhppfile << "#include <iostream>" << endl << endl;
244 finalhppfile << "#ifndef BITMAPS_H" << endl;
245 finalhppfile << "#define BITMAPS_H" << endl << endl;
247 for (int bi = 0; bi<dirlist.GetCount() ; bi++)
249 boostfs::path BitmapsSubDir(dirlist[bi].c_str());
250 for (boostfs::directory_iterator bitmapsidr_iter(BitmapsSubDir);
251 bitmapsidr_iter != dir_end ;
254 if (boostfs::path(bitmapsidr_iter->path()).extension() == ".png" ||
255 boostfs::path(bitmapsidr_iter->path()).extension() == ".PNG" ){
257 BitmapsFilename = boostfs::path(bitmapsidr_iter->path()).filename();
258 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].mb_str()));
259 DirFilenameWxS.Append(wxString::FromUTF8("/"));
260 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsFilename.c_str()));
262 filelist.Add(DirFilenameWxS, 1);
263 DirFilenameWxS = wxT("");
271 for (int fi = 0; fi < filelist.GetCount() ; fi++)
273 CreateCPPFile(dirlist[bi].c_str(), filelist[fi], &cppg);
277 if (filelist.GetCount() > 0)
279 CreateHPPFileDir(dirlist[bi].c_str(), filelist, &hppg);
284 finalhppfile << "#include \"bitmaps/"<< BitmapsSubDir.filename().c_str() <<
289 finalhppfile << endl << "#endif" << endl;
290 finalhppfile.close();
293 // Print out the results.
295 std::cout << "Finished processing PNGs into code." << std::endl;
296 std::cout << fp << " files processed." << std::endl;
297 std::cout << cppg << " .cpp files generated." << std::endl;
298 std::cout << hppg << " .hpp files generated." << std::endl;