crown.cpp 5.4 KB

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