crown.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "crown.h"
  24. #include "memory.h"
  25. #include "input.h"
  26. #include "console_server.h"
  27. #include "bundle_compiler.h"
  28. #include "device.h"
  29. #include "command_line.h"
  30. #include "json_parser.h"
  31. #include "keyboard.h"
  32. #include "mouse.h"
  33. #include "touch.h"
  34. #include "main.h"
  35. #include "audio.h"
  36. #include "physics.h"
  37. #include "disk_filesystem.h"
  38. #include "config.h"
  39. #include "math_utils.h"
  40. #include "lua_system.h"
  41. #include "profiler.h"
  42. #include <bgfx.h>
  43. namespace crown
  44. {
  45. struct PlatformInfo
  46. {
  47. const char* name;
  48. Platform::Enum target;
  49. };
  50. static const PlatformInfo s_platform[Platform::COUNT] =
  51. {
  52. { "linux", Platform::LINUX },
  53. { "windows", Platform::WINDOWS },
  54. { "android", Platform::ANDROID }
  55. };
  56. static Platform::Enum string_to_platform(const char* platform)
  57. {
  58. for (uint32_t i = 0; platform != NULL && i < Platform::COUNT; i++)
  59. {
  60. if (string::strcmp(platform, s_platform[i].name) == 0)
  61. return s_platform[i].target;
  62. }
  63. return Platform::COUNT;
  64. }
  65. static void help(const char* msg = NULL)
  66. {
  67. if (msg)
  68. {
  69. printf("Error: %s\n", msg);
  70. }
  71. printf(
  72. "Usage: crown [options]\n"
  73. "Options:\n\n"
  74. " -h --help Show this help.\n"
  75. " --bundle-dir <path> Use <path> as the source directory for compiled resources.\n"
  76. " --console-port <port> Set port of the console.\n"
  77. " --parent-window <handle> Set the parent window <handle> of the main window.\n"
  78. " Used only by tools.\n"
  79. "\nAvailable only in debug and development builds:\n\n"
  80. " --source-dir <path> Use <path> as the source directory for resource compilation.\n"
  81. " --compile Do a full compile of the resources.\n"
  82. " --platform <platform> Compile resources for the given <platform>.\n"
  83. " Possible values for <platform> are:\n"
  84. " linux\n"
  85. " windows\n"
  86. " android\n"
  87. " --continue Continue the execution after the resource compilation step.\n"
  88. " --host Read resources from a remote engine instance.\n"
  89. " --wait-console Wait for a console connection before starting up.\n"
  90. );
  91. }
  92. void parse_command_line(int argc, char** argv, ConfigSettings& cs)
  93. {
  94. CommandLine cmd(argc, argv);
  95. if (cmd.has_argument("help", 'h'))
  96. {
  97. help();
  98. exit(EXIT_FAILURE);
  99. }
  100. cs.source_dir = cmd.get_parameter("source-dir");
  101. if (!cs.source_dir)
  102. {
  103. help("Source directory must be specified.");
  104. exit(EXIT_FAILURE);
  105. }
  106. cs.bundle_dir = cmd.get_parameter("bundle-dir");
  107. if (!cs.bundle_dir)
  108. {
  109. help("Bundle directory must be specified.");
  110. exit(EXIT_FAILURE);
  111. }
  112. cs.wait_console = cmd.has_argument("wait-console");
  113. cs.do_compile = cmd.has_argument("compile");
  114. cs.do_continue = cmd.has_argument("continue");
  115. cs.platform = string_to_platform(cmd.get_parameter("platform"));
  116. if (cs.do_compile && cs.platform == Platform::COUNT)
  117. {
  118. help("Platform must be specified.");
  119. exit(EXIT_FAILURE);
  120. }
  121. const char* parent = cmd.get_parameter("parent-window");
  122. if (parent)
  123. {
  124. cs.parent_window = string::parse_uint(parent);
  125. }
  126. }
  127. void parse_config_file(Filesystem& fs, ConfigSettings& cs)
  128. {
  129. File* tmpfile = fs.open("crown.config", FOM_READ);
  130. JSONParser config(*tmpfile);
  131. fs.close(tmpfile);
  132. JSONElement root = config.root();
  133. JSONElement cport = root.key_or_nil("console_port");
  134. if (!cport.is_nil())
  135. {
  136. cs.console_port = (int16_t) cport.to_int();
  137. }
  138. JSONElement window_width = root.key_or_nil("window_width");
  139. if (!window_width.is_nil())
  140. {
  141. cs.window_width = math::max((uint16_t)1, (uint16_t)window_width.to_int());
  142. }
  143. JSONElement window_height = root.key_or_nil("window_height");
  144. if (!window_height.is_nil())
  145. {
  146. cs.window_height = math::max((uint16_t)1, (uint16_t)window_height.to_int());
  147. }
  148. cs.boot_script = root.key("boot_script").to_resource_id("lua").name;
  149. cs.boot_package = root.key("boot_package").to_resource_id("package").name;
  150. }
  151. bool init(Filesystem& fs, const ConfigSettings& cs)
  152. {
  153. profiler_globals::init();
  154. input_globals::init();
  155. audio_globals::init();
  156. physics_globals::init();
  157. bgfx::init();
  158. lua_globals::init();
  159. device_globals::init(fs, cs.boot_package, cs.boot_script);
  160. device()->init();
  161. return true;
  162. }
  163. void update()
  164. {
  165. while (!process_events() && device()->is_running())
  166. {
  167. profiler_globals::clear();
  168. #if defined(CROWN_DEBUG)
  169. console_server_globals::console().update();
  170. #endif
  171. device()->update();
  172. bgfx::frame();
  173. lua_globals::clear_temporaries();
  174. input_globals::keyboard().update();
  175. input_globals::mouse().update();
  176. input_globals::touch().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