main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "Base.h"
  2. #include "FBXSceneEncoder.h"
  3. #include "TMXSceneEncoder.h"
  4. #include "TTFFontEncoder.h"
  5. #include "GPBDecoder.h"
  6. #include "EncoderArguments.h"
  7. #include "NormalMapGenerator.h"
  8. #include "Font.h"
  9. using namespace gameplay;
  10. #define FONT_SIZE_DISTANCEFIELD 48
  11. /**
  12. * Prompts the user for a font size until a valid font size is entered.
  13. *
  14. * @return A valid font size.
  15. */
  16. static std::vector<unsigned int> promptUserFontSize()
  17. {
  18. static const int lowerBound = 8;
  19. static const int upperBound = 96;
  20. std::vector<unsigned int> fontSizes;
  21. bool parseError = false;
  22. char buffer[4096];
  23. do
  24. {
  25. parseError = false;
  26. fontSizes.clear();
  27. // Prompt for font sizes
  28. printf("Enter a comma-separated list of font sizes (pixels) to generate:\n");
  29. std::cin.getline(buffer, 4096);
  30. // Parse comma-separated list of fonts sizes and validate
  31. char* ptr = const_cast<char*>(buffer);
  32. std::string sizeStr;
  33. while (ptr)
  34. {
  35. char* end = strchr(ptr, ',');
  36. if (end)
  37. {
  38. sizeStr = std::string(ptr, end - ptr);
  39. ptr = end + 1;
  40. }
  41. else
  42. {
  43. sizeStr = ptr;
  44. ptr = NULL;
  45. }
  46. if (sizeStr.length() > 0)
  47. {
  48. int size = atoi(sizeStr.c_str());
  49. if (size < lowerBound || size > upperBound)
  50. {
  51. printf("Invalid font size: %d. Must be between %d-%d.\n", size, lowerBound, upperBound);
  52. parseError = true;
  53. continue;
  54. }
  55. fontSizes.push_back((unsigned int)size);
  56. }
  57. }
  58. } while (parseError);
  59. return fontSizes;
  60. }
  61. /**
  62. * Main application entry point.
  63. *
  64. * @param argc The number of command line arguments
  65. * @param argv The array of command line arguments.
  66. *
  67. * usage: gameplay-encoder[options] <file_list>
  68. * example: gameplay-encoder C:/assets/duck.fbx
  69. * example: gameplay-encoder -i boy duck.fbx
  70. *
  71. * @stod: Improve argument parsing.
  72. */
  73. int main(int argc, const char** argv)
  74. {
  75. EncoderArguments arguments(argc, argv);
  76. if (arguments.parseErrorOccured())
  77. {
  78. arguments.printUsage();
  79. return 0;
  80. }
  81. // Check if the file exists.
  82. if (!arguments.fileExists())
  83. {
  84. LOG(1, "Error: File not found: %s\n", arguments.getFilePathPointer());
  85. return -1;
  86. }
  87. // File exists
  88. LOG(1, "Encoding file: %s\n", arguments.getFilePathPointer());
  89. switch (arguments.getFileFormat())
  90. {
  91. case EncoderArguments::FILEFORMAT_FBX:
  92. {
  93. std::string realpath(arguments.getFilePath());
  94. FBXSceneEncoder fbxEncoder;
  95. fbxEncoder.write(realpath, arguments);
  96. break;
  97. }
  98. case EncoderArguments::FILEFORMAT_TMX:
  99. {
  100. TMXSceneEncoder tmxEncoder;
  101. tmxEncoder.write(arguments);
  102. break;
  103. }
  104. case EncoderArguments::FILEFORMAT_TTF:
  105. {
  106. std::vector<unsigned int> fontSizes = arguments.getFontSizes();
  107. Font::FontFormat fontFormat = arguments.getFontFormat();
  108. if (fontFormat == Font::BITMAP)
  109. {
  110. if (fontSizes.size() == 0)
  111. {
  112. fontSizes = promptUserFontSize();
  113. }
  114. }
  115. else
  116. {
  117. // Distance fields use size
  118. if (fontSizes.size() == 0)
  119. {
  120. fontSizes.push_back(FONT_SIZE_DISTANCEFIELD);
  121. }
  122. }
  123. std::string id = getBaseName(arguments.getFilePath());
  124. writeFont(arguments.getFilePath().c_str(), arguments.getOutputFilePath().c_str(), fontSizes, id.c_str(), arguments.fontPreviewEnabled(), fontFormat);
  125. break;
  126. }
  127. case EncoderArguments::FILEFORMAT_GPB:
  128. {
  129. std::string realpath(arguments.getFilePath());
  130. GPBDecoder decoder;
  131. decoder.readBinary(realpath);
  132. break;
  133. }
  134. case EncoderArguments::FILEFORMAT_PNG:
  135. case EncoderArguments::FILEFORMAT_RAW:
  136. {
  137. if (arguments.normalMapGeneration())
  138. {
  139. int x, y;
  140. arguments.getHeightmapResolution(&x, &y);
  141. NormalMapGenerator generator(arguments.getFilePath().c_str(), arguments.getOutputFilePath().c_str(), x, y, arguments.getHeightmapWorldSize());
  142. generator.generate();
  143. }
  144. else
  145. {
  146. LOG(1, "Error: Nothing to do for specified file format. Did you forget an option?\n");
  147. return -1;
  148. }
  149. break;
  150. }
  151. default:
  152. {
  153. LOG(1, "Error: Unsupported file format: %s\n", arguments.getFilePathPointer());
  154. return -1;
  155. }
  156. }
  157. return 0;
  158. }