crown.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "crown.h"
  6. #include "memory.h"
  7. #include "input.h"
  8. #include "console_server.h"
  9. #include "bundle_compiler.h"
  10. #include "device.h"
  11. #include "command_line.h"
  12. #include "json_parser.h"
  13. #include "keyboard.h"
  14. #include "mouse.h"
  15. #include "touch.h"
  16. #include "main.h"
  17. #include "audio.h"
  18. #include "physics.h"
  19. #include "disk_filesystem.h"
  20. #include "config.h"
  21. #include "math_utils.h"
  22. #include "profiler.h"
  23. #include "temp_allocator.h"
  24. #include <bgfx.h>
  25. namespace crown
  26. {
  27. struct PlatformInfo
  28. {
  29. const char* name;
  30. Platform::Enum target;
  31. };
  32. static const PlatformInfo s_platform[Platform::COUNT] =
  33. {
  34. { "linux", Platform::LINUX },
  35. { "windows", Platform::WINDOWS },
  36. { "android", Platform::ANDROID }
  37. };
  38. static Platform::Enum string_to_platform(const char* platform)
  39. {
  40. for (uint32_t i = 0; platform != NULL && i < Platform::COUNT; i++)
  41. {
  42. if (strcmp(platform, s_platform[i].name) == 0)
  43. return s_platform[i].target;
  44. }
  45. return Platform::COUNT;
  46. }
  47. static void help(const char* msg = NULL)
  48. {
  49. if (msg)
  50. {
  51. printf("Error: %s\n", msg);
  52. }
  53. printf(
  54. "Usage: crown [options]\n"
  55. "Options:\n\n"
  56. " -h --help Show this help.\n"
  57. " -v --version Show version informations.\n"
  58. " --bundle-dir <path> Use <path> as the source directory for compiled resources.\n"
  59. " --console-port <port> Set port of the console.\n"
  60. " --parent-window <handle> Set the parent window <handle> of the main window.\n"
  61. " Used only by tools.\n"
  62. "\nAvailable only in debug and development builds:\n\n"
  63. " --source-dir <path> Use <path> as the source directory for resource compilation.\n"
  64. " --project <name> Start the project <name>.\n"
  65. " --compile Do a full compile of the resources.\n"
  66. " --platform <platform> Compile resources for the given <platform>.\n"
  67. " Possible values for <platform> are:\n"
  68. " linux\n"
  69. " windows\n"
  70. " android\n"
  71. " --continue Continue the execution after the resource compilation step.\n"
  72. " --host Read resources from a remote engine instance.\n"
  73. " --wait-console Wait for a console connection before starting up.\n"
  74. );
  75. }
  76. void parse_command_line(int argc, char** argv, ConfigSettings& cs)
  77. {
  78. CommandLine cmd(argc, argv);
  79. if (cmd.has_argument("help", 'h'))
  80. {
  81. help();
  82. exit(EXIT_SUCCESS);
  83. }
  84. if (cmd.has_argument("version", 'v'))
  85. {
  86. printf(CROWN_PLATFORM_NAME "-" CROWN_CPU_NAME " (" CROWN_ARCH_NAME ")" " (" CROWN_COMPILER_NAME ")\n");
  87. exit(EXIT_SUCCESS);
  88. }
  89. cs.source_dir = cmd.get_parameter("source-dir");
  90. if (!cs.source_dir)
  91. {
  92. help("Source directory must be specified.");
  93. exit(EXIT_FAILURE);
  94. }
  95. cs.bundle_dir = cmd.get_parameter("bundle-dir");
  96. if (!cs.bundle_dir)
  97. {
  98. help("Bundle directory must be specified.");
  99. exit(EXIT_FAILURE);
  100. }
  101. if (strcmp(cs.source_dir, cs.bundle_dir) == 0)
  102. {
  103. help("Source and Bundle directories must differ.");
  104. exit(EXIT_FAILURE);
  105. }
  106. cs.project = cmd.get_parameter("project");
  107. cs.wait_console = cmd.has_argument("wait-console");
  108. cs.do_compile = cmd.has_argument("compile");
  109. cs.do_continue = cmd.has_argument("continue");
  110. cs.platform = string_to_platform(cmd.get_parameter("platform"));
  111. if (cs.do_compile && cs.platform == Platform::COUNT)
  112. {
  113. help("Platform must be specified.");
  114. exit(EXIT_FAILURE);
  115. }
  116. const char* parent = cmd.get_parameter("parent-window");
  117. if (parent)
  118. {
  119. cs.parent_window = parse_uint(parent);
  120. }
  121. }
  122. void parse_config_file(Filesystem& fs, ConfigSettings& cs)
  123. {
  124. TempAllocator512 alloc;
  125. DynamicString project_path(alloc);
  126. if (cs.project != NULL)
  127. {
  128. project_path += cs.project;
  129. project_path += "/";
  130. }
  131. project_path += "crown.config";
  132. File* tmpfile = fs.open(project_path.c_str(), FOM_READ);
  133. JSONParser config(*tmpfile);
  134. fs.close(tmpfile);
  135. JSONElement root = config.root();
  136. JSONElement cport = root.key_or_nil("console_port");
  137. if (!cport.is_nil())
  138. {
  139. cs.console_port = (int16_t) cport.to_int();
  140. }
  141. JSONElement window_width = root.key_or_nil("window_width");
  142. if (!window_width.is_nil())
  143. {
  144. cs.window_width = max((uint16_t)1, (uint16_t)window_width.to_int());
  145. }
  146. JSONElement window_height = root.key_or_nil("window_height");
  147. if (!window_height.is_nil())
  148. {
  149. cs.window_height = max((uint16_t)1, (uint16_t)window_height.to_int());
  150. }
  151. cs.boot_script = root.key("boot_script").to_resource_id("lua").name;
  152. cs.boot_package = root.key("boot_package").to_resource_id("package").name;
  153. }
  154. bool init(Filesystem& fs, const ConfigSettings& cs)
  155. {
  156. profiler_globals::init();
  157. input_globals::init();
  158. audio_globals::init();
  159. physics_globals::init();
  160. bgfx::init();
  161. device_globals::init(cs, fs);
  162. device()->init();
  163. return true;
  164. }
  165. void update()
  166. {
  167. while (!process_events() && device()->is_running())
  168. {
  169. profiler_globals::clear();
  170. console_server_globals::update();
  171. device()->update();
  172. bgfx::frame();
  173. input_globals::update();
  174. profiler_globals::flush();
  175. }
  176. }
  177. void shutdown()
  178. {
  179. device()->shutdown();
  180. device_globals::shutdown();
  181. bgfx::shutdown();
  182. physics_globals::shutdown();
  183. audio_globals::shutdown();
  184. input_globals::shutdown();
  185. profiler_globals::shutdown();
  186. }
  187. } // namespace crown