main.cpp 4.9 KB

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