Device.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <cstdlib>
  25. #include "Config.h"
  26. #include "Device.h"
  27. #include "Accelerometer.h"
  28. #include "Args.h"
  29. #include "DebugRenderer.h"
  30. #include "DiskFile.h"
  31. #include "DiskFilesystem.h"
  32. #include "JSONParser.h"
  33. #include "Keyboard.h"
  34. #include "Log.h"
  35. #include "LuaEnvironment.h"
  36. #include "Memory.h"
  37. #include "Mouse.h"
  38. #include "OS.h"
  39. #include "OsWindow.h"
  40. #include "Renderer.h"
  41. #include "ResourceManager.h"
  42. #include "StringSetting.h"
  43. #include "StringUtils.h"
  44. #include "TextReader.h"
  45. #include "Touch.h"
  46. #include "Types.h"
  47. #include "Bundle.h"
  48. #include "TempAllocator.h"
  49. #include "ResourcePackage.h"
  50. #include "RPCServer.h"
  51. #include "SoundRenderer.h"
  52. #if defined(LINUX) || defined(WINDOWS)
  53. #include "BundleCompiler.h"
  54. #endif
  55. #if defined(ANDROID)
  56. #include "ApkFilesystem.h"
  57. #endif
  58. #define MAX_SUBSYSTEMS_HEAP 16 * 1024 * 1024
  59. namespace crown
  60. {
  61. //-----------------------------------------------------------------------------
  62. Device::Device()
  63. : m_allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
  64. , m_is_init(false)
  65. , m_is_running(false)
  66. , m_is_paused(false)
  67. , m_is_really_paused(false)
  68. , m_frame_count(0)
  69. , m_last_time(0)
  70. , m_current_time(0)
  71. , m_last_delta_time(0.0f)
  72. , m_time_since_start(0.0)
  73. , m_filesystem(NULL)
  74. , m_lua_environment(NULL)
  75. , m_renderer(NULL)
  76. , m_debug_renderer(NULL)
  77. , m_sound_renderer(NULL)
  78. , m_bundle_compiler(NULL)
  79. , m_rpc(NULL)
  80. , m_resource_manager(NULL)
  81. , m_resource_bundle(NULL)
  82. , m_renderer_init_request(false)
  83. {
  84. // Bundle dir is current dir by default.
  85. string::strncpy(m_bundle_dir, os::get_cwd(), MAX_PATH_LENGTH);
  86. string::strncpy(m_source_dir, "", MAX_PATH_LENGTH);
  87. string::strncpy(m_boot_file, "lua/game", MAX_PATH_LENGTH);
  88. }
  89. //-----------------------------------------------------------------------------
  90. Device::~Device()
  91. {
  92. }
  93. //-----------------------------------------------------------------------------
  94. void Device::init()
  95. {
  96. // Initialize
  97. Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  98. // RPC only in debug or development builds
  99. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  100. m_rpc = CE_NEW(m_allocator, RPCServer);
  101. m_rpc->add_handler(&m_command_handler);
  102. m_rpc->add_handler(&m_script_handler);
  103. m_rpc->add_handler(&m_stats_handler);
  104. m_rpc->add_handler(&m_ping_handler);
  105. m_rpc->init(false);
  106. #endif
  107. // Default bundle filesystem
  108. #if defined (LINUX) || defined(WINDOWS)
  109. m_filesystem = CE_NEW(m_allocator, DiskFilesystem)(m_bundle_dir);
  110. #elif defined(ANDROID)
  111. m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
  112. #endif
  113. Log::d("Filesystem created.");
  114. // Read settings from crown.config
  115. read_engine_settings();
  116. m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
  117. // Create resource manager
  118. m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, 0);
  119. Log::d("Resource manager created.");
  120. Log::d("Resource seed: %d", m_resource_manager->seed());
  121. // Create window
  122. m_window = CE_NEW(m_allocator, OsWindow);
  123. // Create input devices
  124. m_keyboard = CE_NEW(m_allocator, Keyboard);
  125. m_mouse = CE_NEW(m_allocator, Mouse);
  126. m_touch = CE_NEW(m_allocator, Touch);
  127. // Create renderer
  128. m_renderer = CE_NEW(m_allocator, Renderer)(m_allocator);
  129. m_renderer->init();
  130. Log::d("Renderer created.");
  131. // Create debug renderer
  132. m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
  133. Log::d("Debug renderer created.");
  134. m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
  135. m_lua_environment->init();
  136. Log::d("Lua environment created.");
  137. m_sound_renderer = CE_NEW(m_allocator, SoundRenderer)(m_allocator);
  138. m_sound_renderer->init();
  139. Log::d("SoundRenderer created.");
  140. Log::i("Crown Engine initialized.");
  141. Log::i("Initializing Game...");
  142. m_is_init = true;
  143. start();
  144. // Execute lua boot file
  145. if (m_lua_environment->load_and_execute(m_boot_file))
  146. {
  147. if (!m_lua_environment->call_global("init", 0))
  148. {
  149. pause();
  150. }
  151. }
  152. else
  153. {
  154. pause();
  155. }
  156. Log::d("Total allocated size: %llu", m_allocator.allocated_size());
  157. }
  158. //-----------------------------------------------------------------------------
  159. void Device::shutdown()
  160. {
  161. CE_ASSERT(is_init(), "Engine is not initialized");
  162. // Shutdowns the game
  163. m_lua_environment->call_global("shutdown", 0);
  164. Log::d("Releasing SoundRenderer...");
  165. if (m_sound_renderer)
  166. {
  167. m_sound_renderer->shutdown();
  168. CE_DELETE(m_allocator, m_sound_renderer);
  169. }
  170. Log::i("Releasing LuaEnvironment...");
  171. if (m_lua_environment)
  172. {
  173. m_lua_environment->shutdown();
  174. CE_DELETE(m_allocator, m_lua_environment);
  175. }
  176. Log::i("Releasing Input Devices...");
  177. CE_DELETE(m_allocator, m_touch);
  178. CE_DELETE(m_allocator, m_mouse);
  179. CE_DELETE(m_allocator, m_keyboard);
  180. Log::i("Releasing DebugRenderer...");
  181. if (m_debug_renderer)
  182. {
  183. CE_DELETE(m_allocator, m_debug_renderer);
  184. }
  185. Log::i("Releasing Renderer...");
  186. if (m_renderer)
  187. {
  188. m_renderer->shutdown();
  189. CE_DELETE(m_allocator, m_renderer);
  190. }
  191. Log::i("Releasing ResourceManager...");
  192. if (m_resource_manager)
  193. {
  194. CE_DELETE(m_allocator, m_resource_manager);
  195. }
  196. if (m_resource_bundle)
  197. {
  198. Bundle::destroy(m_allocator, m_resource_bundle);
  199. }
  200. Log::i("Releasing Filesystem...");
  201. if (m_filesystem)
  202. {
  203. CE_DELETE(m_allocator, m_filesystem);
  204. }
  205. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  206. Log::flush();
  207. m_rpc->execute_callbacks();
  208. m_rpc->shutdown();
  209. CE_DELETE(m_allocator, m_rpc);
  210. #endif
  211. m_allocator.clear();
  212. m_is_init = false;
  213. }
  214. //-----------------------------------------------------------------------------
  215. bool Device::is_init() const
  216. {
  217. return m_is_init;
  218. }
  219. //-----------------------------------------------------------------------------
  220. bool Device::is_paused() const
  221. {
  222. return m_is_paused;
  223. }
  224. //-----------------------------------------------------------------------------
  225. Filesystem* Device::filesystem()
  226. {
  227. return m_filesystem;
  228. }
  229. //-----------------------------------------------------------------------------
  230. ResourceManager* Device::resource_manager()
  231. {
  232. return m_resource_manager;
  233. }
  234. //-----------------------------------------------------------------------------
  235. LuaEnvironment* Device::lua_environment()
  236. {
  237. return m_lua_environment;
  238. }
  239. //-----------------------------------------------------------------------------
  240. OsWindow* Device::window()
  241. {
  242. return m_window;
  243. }
  244. //-----------------------------------------------------------------------------
  245. Renderer* Device::renderer()
  246. {
  247. return m_renderer;
  248. }
  249. //-----------------------------------------------------------------------------
  250. DebugRenderer* Device::debug_renderer()
  251. {
  252. return m_debug_renderer;
  253. }
  254. //-----------------------------------------------------------------------------
  255. SoundRenderer* Device::sound_renderer()
  256. {
  257. return m_sound_renderer;
  258. }
  259. //-----------------------------------------------------------------------------
  260. Keyboard* Device::keyboard()
  261. {
  262. return m_keyboard;
  263. }
  264. //-----------------------------------------------------------------------------
  265. Mouse* Device::mouse()
  266. {
  267. return m_mouse;
  268. }
  269. //-----------------------------------------------------------------------------
  270. Touch* Device::touch()
  271. {
  272. return m_touch;
  273. }
  274. //-----------------------------------------------------------------------------
  275. Accelerometer* Device::accelerometer()
  276. {
  277. return NULL;
  278. }
  279. //-----------------------------------------------------------------------------
  280. void Device::start()
  281. {
  282. CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
  283. m_is_running = true;
  284. m_last_time = os::milliseconds();
  285. }
  286. //-----------------------------------------------------------------------------
  287. void Device::stop()
  288. {
  289. CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
  290. m_is_running = false;
  291. }
  292. //-----------------------------------------------------------------------------
  293. void Device::pause()
  294. {
  295. m_is_paused = true;
  296. }
  297. //-----------------------------------------------------------------------------
  298. void Device::unpause()
  299. {
  300. m_is_paused = false;
  301. }
  302. //-----------------------------------------------------------------------------
  303. bool Device::is_running() const
  304. {
  305. return m_is_running;
  306. }
  307. //-----------------------------------------------------------------------------
  308. uint64_t Device::frame_count() const
  309. {
  310. return m_frame_count;
  311. }
  312. //-----------------------------------------------------------------------------
  313. float Device::last_delta_time() const
  314. {
  315. return m_last_delta_time;
  316. }
  317. //-----------------------------------------------------------------------------
  318. double Device::time_since_start() const
  319. {
  320. return m_time_since_start;
  321. }
  322. //-----------------------------------------------------------------------------
  323. void Device::frame()
  324. {
  325. m_current_time = os::microseconds();
  326. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  327. m_last_time = m_current_time;
  328. m_time_since_start += m_last_delta_time;
  329. m_rpc->update();
  330. if (!m_is_paused)
  331. {
  332. m_resource_manager->poll_resource_loader();
  333. m_renderer->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
  334. m_renderer->commit(0);
  335. if (!m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time()))
  336. {
  337. pause();
  338. }
  339. m_renderer->frame();
  340. // FIXME: SoundRenderer should not be updated each frame
  341. m_sound_renderer->frame();
  342. }
  343. Log::flush();
  344. m_rpc->execute_callbacks();
  345. m_frame_count++;
  346. }
  347. //-----------------------------------------------------------------------------
  348. ResourcePackage* Device::create_resource_package(const char* name)
  349. {
  350. CE_ASSERT_NOT_NULL(name);
  351. ResourceId package_id = m_resource_manager->load("package", name);
  352. m_resource_manager->flush();
  353. PackageResource* package_res = (PackageResource*) m_resource_manager->data(package_id);
  354. ResourcePackage* package = CE_NEW(default_allocator(), ResourcePackage)(*m_resource_manager, package_id, package_res);
  355. return package;
  356. }
  357. //-----------------------------------------------------------------------------
  358. void Device::destroy_resource_package(ResourcePackage* package)
  359. {
  360. CE_ASSERT_NOT_NULL(package);
  361. m_resource_manager->unload(package->resource_id());
  362. CE_DELETE(default_allocator(), package);
  363. }
  364. //-----------------------------------------------------------------------------
  365. void Device::compile(const char* , const char* , const char* )
  366. {
  367. }
  368. //-----------------------------------------------------------------------------
  369. void Device::reload(const char* type, const char* name)
  370. {
  371. TempAllocator4096 temp;
  372. DynamicString filename(temp);
  373. filename += name;
  374. filename += '.';
  375. filename += type;
  376. if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir, filename.c_str()))
  377. {
  378. Log::d("Compilation failed.");
  379. return;
  380. }
  381. uint32_t type_hash = hash::murmur2_32(type, string::strlen(type), 0);
  382. switch (type_hash)
  383. {
  384. case LUA_TYPE:
  385. {
  386. m_lua_environment->load_and_execute(name);
  387. break;
  388. }
  389. default:
  390. {
  391. CE_ASSERT(false, "Oops, unknown resource type: %s", type);
  392. break;
  393. }
  394. }
  395. }
  396. //-------------------------------------------------------------------------
  397. void Device::read_engine_settings()
  398. {
  399. // // Check crown.config existance
  400. // CE_ASSERT(m_filesystem->is_file("crown.config"), "Unable to open crown.config");
  401. // // Copy crown config in a buffer
  402. // TempAllocator4096 allocator;
  403. // File* config_file = m_filesystem->open("crown.config", FOM_READ);
  404. // char* json_string = (char*)allocator.allocate(config_file->size());
  405. // config_file->read(json_string, config_file->size());
  406. // m_filesystem->close(config_file);
  407. // // Parse crown.config
  408. // JSONParser parser(json_string);
  409. // JSONElement root = parser.root();
  410. // // Boot
  411. // if (root.has_key("boot"))
  412. // {
  413. // const char* boot = root.key("boot").string_value();
  414. // const size_t boot_length = string::strlen(boot) + 1;
  415. // string::strncpy(m_boot_file, boot, boot_length);
  416. // }
  417. // // Window width
  418. // if (root.has_key("window_width"))
  419. // {
  420. // m_preferred_window_width = root.key("window_width").int_value();
  421. // }
  422. // // Window height
  423. // if (root.has_key("window_height"))
  424. // {
  425. // m_preferred_window_height = root.key("window_height").int_value();
  426. // }
  427. // allocator.deallocate(json_string);
  428. // Log::i("Configuration set");
  429. }
  430. static Device* g_device;
  431. void set_device(Device* device)
  432. {
  433. g_device = device;
  434. }
  435. Device* device()
  436. {
  437. return g_device;
  438. }
  439. } // namespace crown