main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (C) 2012 Gaz Davidson
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. /***
  5. This tool creates a .h file from a given input file by encoding it into a C string,
  6. allowing you to build your resources directly into your binaries, just like Irrlicht's
  7. built-in font.
  8. To distribute your app as a single executable file of minimal size:
  9. 1. Put all your resources into a single directory and add it to Irrlicht's filesystem
  10. as a folder through IFileSystem::addArchive. Develop and test your app as usual.
  11. 2. Open IrrCompileConfig.h and comment out all the options that your app does not use.
  12. This will reduce the size of the Irrlicht library considerably.
  13. * You should remove the D3D video drivers, because they rely on external DLLs.
  14. * You should keep either the TAR or ZIP archive loader, used in step 6.
  15. * If you remove the JPEG loader, you don't have to say you use JPEG code in your
  16. documentation.
  17. 3. Recompile Irrlicht as a static library, editing the IRR_STATIC_LIB line in
  18. IrrCompileConfig.h.
  19. The next time you compile your application it will take a while longer, but
  20. Irrlicht will be built into your binary.
  21. 4. TAR or ZIP your resource directory using your favourite archiving tool (ie 7zip).
  22. * If you chose ZIP but compiled without zlib, don't compress this archive or it
  23. won't open.
  24. 5. Run this tool to convert your resource file into a .h file, like so:
  25. FileToHeader res.zip > EmbeddedResources.h
  26. 6. Add the .h file to your project, create the embedded read file then mount as a
  27. ZIP or TAR archive instead of the folder, like so:
  28. io::IReadFile *f = io::createEmbeddedFile(device->getFileSystem(), "res.zip");
  29. device->getFileSystem()->addFileArchive(f);
  30. archive->drop();
  31. 7. Recompile your app.
  32. * If you use Microsoft's compiler, make sure your CRT (common run-time) is
  33. the static library version, otherwise you'll have a dependency on the CRT DLLs.
  34. Your binary should now be completely portable; you can distribute just the exe file.
  35. 8. Optional: Use UPX (upx.sf.net) to compress your binary.
  36. */
  37. #include <iostream>
  38. #include <fstream>
  39. #include <sstream>
  40. using namespace std;
  41. int main(int argc, char* argv[])
  42. {
  43. if (argc < 2)
  44. {
  45. // print usage
  46. cerr << "You must to specify at least one input file" << endl;
  47. cerr << "usage: " << argv[0] << "<file1> [file2...]" << endl;
  48. cerr << "outputs a header file to stdout, so for example use";
  49. return 1;
  50. }
  51. int i = 1;
  52. // write file header
  53. cout << "// File made by FileToHeader, part of the Irrlicht Engine" << endl
  54. << endl
  55. << "#ifndef _EMBEDDED_FILES_H_INCLUDED_" << endl
  56. << "#define _EMBEDDED_FILES_H_INCLUDED_" << endl
  57. << endl
  58. << "#include \"irrTypes.h\"" << endl
  59. << "#include \"IReadFile.h\"" << endl
  60. << "#include \"IFileSystem.h\"" << endl
  61. << endl
  62. << "namespace irr" << endl
  63. << "{" << endl
  64. << "namespace io" << endl
  65. << "{" << endl
  66. << endl
  67. << " const c8* EmbeddedFileData[] = " << endl
  68. << " {" << endl;
  69. // store sizes and file names
  70. stringstream sizes;
  71. stringstream names;
  72. sizes << "const u32 EmbeddedFileSizes[] = {";
  73. names << "const c8* EmbeddedFileNames[] = {";
  74. int fileCount = 0;
  75. // char to hex digit table, probably doesn't help for speed due to fstream. better than using sprintf though
  76. char hextable[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  77. while (i < argc)
  78. {
  79. // open and seek to end of file
  80. ifstream input;
  81. input.open(argv[i], ios::in | ios::binary | ios::ate);
  82. if (input.is_open())
  83. {
  84. int size = input.tellg();
  85. input.seekg(0, ios::beg);
  86. // read the file into RAM
  87. char *entireFile = new char[size];
  88. input.read(entireFile, size);
  89. if (fileCount)
  90. {
  91. sizes << ", ";
  92. names << ", ";
  93. cout << "," << endl;
  94. }
  95. // save file size and name
  96. sizes << size;
  97. names << '"' << argv[i] << '"';
  98. // write the file data
  99. cout << " \"";
  100. for (int count=0; count < size; ++count)
  101. {
  102. if (count && (count % 16) == 0)
  103. cout << "\"" << endl << " \"";
  104. cout << "\\x" << hextable[(entireFile[count] >> 4) & 0xF] << hextable[entireFile[count] & 0x0F];
  105. }
  106. cout << "\"";
  107. delete [] entireFile;
  108. //
  109. input.close();
  110. fileCount++;
  111. }
  112. else
  113. {
  114. cerr << "Failed to open file: " << argv[i] << endl;
  115. }
  116. ++i;
  117. }
  118. // close binary file list and write file metadata
  119. cout << endl
  120. << " , 0};" << endl
  121. << endl
  122. << " const u32 EmbeddedFileCount = " << fileCount << ";" << endl
  123. << " " << sizes.str() << "};" << endl
  124. << " " << names.str() << "};" << endl
  125. << endl;
  126. // write functions to create embedded IReadFiles
  127. cout << " IReadFile* createEmbeddedFile(IFileSystem *fs, u32 index)" << endl
  128. << " {" << endl
  129. << " if (EmbeddedFileCount < index)" << endl
  130. << " return 0;" << endl
  131. << endl
  132. << " return fs->createMemoryReadFile((void*)EmbeddedFileData[index], " << endl
  133. << " EmbeddedFileSizes[index], EmbeddedFileNames[index]);" << endl
  134. << " }" << endl
  135. << endl
  136. << " IReadFile* createEmbeddedFile(IFileSystem *fs, path filename)" << endl
  137. << " {" << endl
  138. << " for (u32 i=0; i < EmbeddedFileCount; ++i)" << endl
  139. << " if (filename == EmbeddedFileNames[i])" << endl
  140. << " return createEmbeddedFile(fs, i);" << endl
  141. << endl
  142. << " return 0;" << endl
  143. << " }" << endl
  144. << endl;
  145. // write footer
  146. cout << "} // namespace io" << endl
  147. << "} // namespace irr" << endl
  148. << endl
  149. << "#endif // _EMBEDDED_FILES_H_INCLUDED_" << endl;
  150. return 0;
  151. }