Device.cpp 13 KB

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