resource-compiler.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "Crown.h"
  2. #include "tga/TGACompiler.h"
  3. #include "txt/TXTCompiler.h"
  4. #include "vs/VSCompiler.h"
  5. #include "ps/PSCompiler.h"
  6. using namespace crown;
  7. // Max number of requests per run
  8. const uint32_t MAX_COMPILE_REQUESTS = 512;
  9. const char* root_path = NULL;
  10. const char* dest_path = NULL;
  11. uint32_t hash_seed = 0;
  12. // Help functions
  13. int32_t parse_command_line(int argc, char* argv[]);
  14. void print_help_message(const char* program_name);
  15. void check_arguments(const char* root_path, const char* dest_path);
  16. void compile_by_type(const char* resource);
  17. //-----------------------------------------------------------------------------
  18. int main(int argc, char** argv)
  19. {
  20. int32_t first_resource = parse_command_line(argc, argv);
  21. // Check if all the mandatory options are set
  22. check_arguments(root_path, dest_path);
  23. // If there are no resources
  24. if (first_resource >= argc)
  25. {
  26. Log::e("you have to specify at least one resource.");
  27. exit(EXIT_FAILURE);
  28. }
  29. TGACompiler tga(root_path, dest_path);
  30. TXTCompiler txt(root_path, dest_path);
  31. VSCompiler vs(root_path, dest_path);
  32. PSCompiler ps(root_path, dest_path);
  33. char resource_name[MAX_RESOURCE_NAME_LENGTH];
  34. char resource_type[MAX_RESOURCE_TYPE_LENGTH];
  35. // Dispatch requests to the appropriate compiler
  36. for (int32_t res = first_resource; res < argc; res++)
  37. {
  38. char* resource = argv[res];
  39. path::filename_without_extension(resource, resource_name, MAX_RESOURCE_NAME_LENGTH);
  40. path::extension(resource, resource_type, MAX_RESOURCE_TYPE_LENGTH);
  41. uint32_t resource_name_hash = hash::murmur2_32(resource_name, string::strlen(resource_name), hash_seed);
  42. uint32_t resource_type_hash = hash::murmur2_32(resource_type, string::strlen(resource_type), 0);
  43. switch (resource_type_hash)
  44. {
  45. case TEXTURE_TYPE:
  46. {
  47. tga.compile(resource, resource_name_hash, resource_type_hash);
  48. break;
  49. }
  50. case TEXT_TYPE:
  51. {
  52. txt.compile(resource, resource_name_hash, resource_type_hash);
  53. break;
  54. }
  55. case VERTEX_SHADER_TYPE:
  56. {
  57. vs.compile(resource, resource_name_hash, resource_type_hash);
  58. break;
  59. }
  60. case PIXEL_SHADER_TYPE:
  61. {
  62. ps.compile(resource, resource_name_hash, resource_type_hash);
  63. break;
  64. }
  65. default:
  66. {
  67. Log::e("Resource type not supported.");
  68. break;
  69. }
  70. }
  71. }
  72. return 0;
  73. }
  74. //-----------------------------------------------------------------------------
  75. int32_t parse_command_line(int argc, char* argv[])
  76. {
  77. // Parse arguments
  78. static ArgsOption options[] =
  79. {
  80. "help", AOA_NO_ARGUMENT, NULL, 'h',
  81. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  82. "dest-path", AOA_REQUIRED_ARGUMENT, NULL, 'd',
  83. "seed", AOA_REQUIRED_ARGUMENT, NULL, 's',
  84. NULL, 0, NULL, 0
  85. };
  86. Args args(argc, argv, "", options);
  87. int32_t opt;
  88. while ((opt = args.getopt()) != -1)
  89. {
  90. switch (opt)
  91. {
  92. // Root path
  93. case 'r':
  94. {
  95. root_path = args.optarg();
  96. break;
  97. }
  98. // Dest path
  99. case 'd':
  100. {
  101. dest_path = args.optarg();
  102. break;
  103. }
  104. case 's':
  105. {
  106. hash_seed = atoi(args.optarg());
  107. break;
  108. }
  109. case 'h':
  110. case '?':
  111. default:
  112. {
  113. print_help_message(argv[0]);
  114. exit(EXIT_FAILURE);
  115. }
  116. }
  117. }
  118. return args.optind();
  119. }
  120. //-----------------------------------------------------------------------------
  121. void print_help_message(const char* program_name)
  122. {
  123. printf("Usage: %s [options] [resources]\n", program_name);
  124. printf
  125. (
  126. "Options:\n\n"
  127. " --help Show this help.\n"
  128. " --root-path <path> The absolute <path> whether to look for the input resources.\n"
  129. " --dest-path <path> The absolute <path> whether to put the compiled resources.\n"
  130. " --seed <value> The seed to use for generating output resource hashes.\n"
  131. );
  132. }
  133. //-----------------------------------------------------------------------------
  134. void check_arguments(const char* root_path, const char* dest_path)
  135. {
  136. if (root_path == NULL)
  137. {
  138. Log::e("you have to specify the root path with `--root-path`\n");
  139. exit(EXIT_FAILURE);
  140. }
  141. if (dest_path == NULL)
  142. {
  143. Log::e("you have to specify the destination path with `--dest-path`\n");
  144. exit(EXIT_FAILURE);
  145. }
  146. }