device.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 "disk_file.h"
  30. #include "disk_filesystem.h"
  31. #include "json_parser.h"
  32. #include "keyboard.h"
  33. #include "log.h"
  34. #include "lua_environment.h"
  35. #include "memory.h"
  36. #include "mouse.h"
  37. #include "os.h"
  38. #include "os_window.h"
  39. #include "renderer.h"
  40. #include "resource_manager.h"
  41. #include "string_setting.h"
  42. #include "string_utils.h"
  43. #include "touch.h"
  44. #include "types.h"
  45. #include "bundle.h"
  46. #include "temp_allocator.h"
  47. #include "resource_package.h"
  48. #include "console_server.h"
  49. #include "world.h"
  50. #include "lua_stack.h"
  51. #include "world_manager.h"
  52. #include "network_filesystem.h"
  53. #include "lua_system.h"
  54. #if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
  55. #include "bundle_compiler.h"
  56. #endif
  57. #if CROWN_PLATFORM_ANDROID
  58. #include "apk_filesystem.h"
  59. #endif
  60. #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
  61. namespace crown
  62. {
  63. //-----------------------------------------------------------------------------
  64. Device::Device()
  65. : m_allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
  66. , m_argc(0)
  67. , m_argv(NULL)
  68. , m_fileserver(0)
  69. , m_console_port(10001)
  70. , m_is_init(false)
  71. , m_is_running(false)
  72. , m_is_paused(false)
  73. , m_frame_count(0)
  74. , m_last_time(0)
  75. , m_current_time(0)
  76. , m_last_delta_time(0.0f)
  77. , m_time_since_start(0.0)
  78. , m_filesystem(NULL)
  79. , m_lua_environment(NULL)
  80. , m_renderer(NULL)
  81. , m_bundle_compiler(NULL)
  82. , m_console(NULL)
  83. , m_resource_manager(NULL)
  84. , m_resource_bundle(NULL)
  85. , m_world_manager(NULL)
  86. {
  87. // Bundle dir is current dir by default.
  88. string::strncpy(m_bundle_dir, os::get_cwd(), MAX_PATH_LENGTH);
  89. string::strncpy(m_source_dir, "", MAX_PATH_LENGTH);
  90. string::strncpy(m_boot_file, "lua/game", MAX_PATH_LENGTH);
  91. }
  92. //-----------------------------------------------------------------------------
  93. Device::~Device()
  94. {
  95. }
  96. //-----------------------------------------------------------------------------
  97. void Device::init()
  98. {
  99. // Initialize
  100. CE_LOGI("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  101. CE_LOGD("Creating filesystem...");
  102. // Default bundle filesystem
  103. #if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
  104. if (m_fileserver == 1)
  105. {
  106. m_filesystem = CE_NEW(m_allocator, NetworkFilesystem)(NetAddress(127, 0, 0, 1), 10001);
  107. }
  108. else
  109. {
  110. m_filesystem = CE_NEW(m_allocator, DiskFilesystem)(m_bundle_dir);
  111. }
  112. #elif CROWN_PLATFORM_ANDROID
  113. if (m_fileserver == 1)
  114. {
  115. m_filesystem = CE_NEW(m_allocator, NetworkFilesystem)(NetAddress(192, 168, 0, 7), 10001);
  116. }
  117. else
  118. {
  119. m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
  120. }
  121. #endif
  122. m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
  123. // Create resource manager
  124. CE_LOGD("Creating resource manager...");
  125. m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle);
  126. // Create world manager
  127. CE_LOGD("Creating world manager...");
  128. m_world_manager = CE_NEW(m_allocator, WorldManager)();
  129. // Create window
  130. CE_LOGD("Creating main window...");
  131. m_window = CE_NEW(m_allocator, OsWindow);
  132. // Create input devices
  133. m_keyboard = CE_NEW(m_allocator, Keyboard);
  134. m_mouse = CE_NEW(m_allocator, Mouse);
  135. m_touch = CE_NEW(m_allocator, Touch);
  136. // Create renderer
  137. CE_LOGD("Creating renderer...");
  138. m_renderer = CE_NEW(m_allocator, Renderer)(m_allocator);
  139. m_renderer->init();
  140. CE_LOGD("Creating lua system...");
  141. lua_system::init();
  142. m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)(lua_system::state());
  143. CE_LOGD("Creating physics...");
  144. physics_system::init();
  145. CE_LOGD("Creating audio...");
  146. audio_system::init();
  147. CE_LOGD("Crown Engine initialized.");
  148. CE_LOGD("Initializing Game...");
  149. m_is_init = true;
  150. start();
  151. // Execute lua boot file
  152. m_lua_environment->load_and_execute(m_boot_file);
  153. m_lua_environment->call_global("init", 0);
  154. CE_LOGD("Total allocated size: %ld", m_allocator.allocated_size());
  155. }
  156. //-----------------------------------------------------------------------------
  157. void Device::shutdown()
  158. {
  159. CE_ASSERT(is_init(), "Engine is not initialized");
  160. // Shutdowns the game
  161. m_lua_environment->call_global("shutdown", 0);
  162. CE_LOGD("Releasing audio...");
  163. audio_system::shutdown();
  164. CE_LOGD("Releasing physics...");
  165. physics_system::shutdown();
  166. CE_LOGD("Releasing lua system...");
  167. lua_system::shutdown();
  168. if (m_lua_environment)
  169. {
  170. CE_DELETE(m_allocator, m_lua_environment);
  171. }
  172. CE_LOGD("Releasing input devices...");
  173. CE_DELETE(m_allocator, m_touch);
  174. CE_DELETE(m_allocator, m_mouse);
  175. CE_DELETE(m_allocator, m_keyboard);
  176. CE_LOGD("Releasing renderer...");
  177. if (m_renderer)
  178. {
  179. m_renderer->shutdown();
  180. CE_DELETE(m_allocator, m_renderer);
  181. }
  182. CE_LOGD("Releasing world manager...");
  183. CE_DELETE(m_allocator, m_world_manager);
  184. CE_LOGD("Releasing resource manager...");
  185. if (m_resource_manager)
  186. {
  187. CE_DELETE(m_allocator, m_resource_manager);
  188. }
  189. if (m_resource_bundle)
  190. {
  191. Bundle::destroy(m_allocator, m_resource_bundle);
  192. }
  193. CE_LOGD("Releasing filesystem...");
  194. if (m_filesystem)
  195. {
  196. CE_DELETE(m_allocator, m_filesystem);
  197. }
  198. m_allocator.clear();
  199. m_is_init = false;
  200. }
  201. //-----------------------------------------------------------------------------
  202. bool Device::is_init() const
  203. {
  204. return m_is_init;
  205. }
  206. //-----------------------------------------------------------------------------
  207. bool Device::is_paused() const
  208. {
  209. return m_is_paused;
  210. }
  211. //-----------------------------------------------------------------------------
  212. Filesystem* Device::filesystem()
  213. {
  214. return m_filesystem;
  215. }
  216. //-----------------------------------------------------------------------------
  217. ResourceManager* Device::resource_manager()
  218. {
  219. return m_resource_manager;
  220. }
  221. //-----------------------------------------------------------------------------
  222. LuaEnvironment* Device::lua_environment()
  223. {
  224. return m_lua_environment;
  225. }
  226. //-----------------------------------------------------------------------------
  227. OsWindow* Device::window()
  228. {
  229. return m_window;
  230. }
  231. //-----------------------------------------------------------------------------
  232. Renderer* Device::renderer()
  233. {
  234. return m_renderer;
  235. }
  236. //-----------------------------------------------------------------------------
  237. Keyboard* Device::keyboard()
  238. {
  239. return m_keyboard;
  240. }
  241. //-----------------------------------------------------------------------------
  242. Mouse* Device::mouse()
  243. {
  244. return m_mouse;
  245. }
  246. //-----------------------------------------------------------------------------
  247. Touch* Device::touch()
  248. {
  249. return m_touch;
  250. }
  251. //-----------------------------------------------------------------------------
  252. Accelerometer* Device::accelerometer()
  253. {
  254. return NULL;
  255. }
  256. //-----------------------------------------------------------------------------
  257. void Device::start()
  258. {
  259. CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
  260. m_is_running = true;
  261. m_last_time = os::milliseconds();
  262. }
  263. //-----------------------------------------------------------------------------
  264. void Device::stop()
  265. {
  266. CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
  267. m_is_running = false;
  268. }
  269. //-----------------------------------------------------------------------------
  270. void Device::pause()
  271. {
  272. m_is_paused = true;
  273. CE_LOGI("Engine paused.");
  274. }
  275. //-----------------------------------------------------------------------------
  276. void Device::unpause()
  277. {
  278. m_is_paused = false;
  279. CE_LOGI("Engine unpaused.");
  280. }
  281. //-----------------------------------------------------------------------------
  282. bool Device::is_running() const
  283. {
  284. return m_is_running;
  285. }
  286. //-----------------------------------------------------------------------------
  287. uint64_t Device::frame_count() const
  288. {
  289. return m_frame_count;
  290. }
  291. //-----------------------------------------------------------------------------
  292. float Device::last_delta_time() const
  293. {
  294. return m_last_delta_time;
  295. }
  296. //-----------------------------------------------------------------------------
  297. double Device::time_since_start() const
  298. {
  299. return m_time_since_start;
  300. }
  301. //-----------------------------------------------------------------------------
  302. void Device::frame()
  303. {
  304. m_current_time = os::microseconds();
  305. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  306. m_last_time = m_current_time;
  307. m_time_since_start += m_last_delta_time;
  308. if (!m_is_paused)
  309. {
  310. m_resource_manager->complete_requests();
  311. m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time());
  312. m_renderer->frame();
  313. }
  314. lua_system::clear_temporaries();
  315. m_frame_count++;
  316. }
  317. //-----------------------------------------------------------------------------
  318. void Device::update_world(World* world, float dt)
  319. {
  320. world->update(dt);
  321. }
  322. //-----------------------------------------------------------------------------
  323. void Device::render_world(World* world, Camera* camera)
  324. {
  325. world->render(camera);
  326. }
  327. //-----------------------------------------------------------------------------
  328. WorldId Device::create_world()
  329. {
  330. return m_world_manager->create_world();
  331. }
  332. //-----------------------------------------------------------------------------
  333. void Device::destroy_world(WorldId world)
  334. {
  335. m_world_manager->destroy_world(world);
  336. }
  337. //-----------------------------------------------------------------------------
  338. ResourcePackage* Device::create_resource_package(const char* name)
  339. {
  340. return CE_NEW(default_allocator(), ResourcePackage)(name, *m_resource_manager);
  341. }
  342. //-----------------------------------------------------------------------------
  343. void Device::destroy_resource_package(ResourcePackage* package)
  344. {
  345. CE_DELETE(default_allocator(), package);
  346. }
  347. //-----------------------------------------------------------------------------
  348. void Device::reload(const char* , const char* )
  349. {
  350. /*
  351. #if defined(LINUX) || defined(WINDOWS)
  352. TempAllocator4096 temp;
  353. DynamicString filename(temp);
  354. filename += name;
  355. filename += '.';
  356. filename += type;
  357. if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir, filename.c_str()))
  358. {
  359. CE_LOGD("Compilation failed.");
  360. return;
  361. }
  362. ResourceId old_res_id(type, name);
  363. const void* old_res = m_resource_manager->get(old_res_id);
  364. m_resource_manager->unload(old_res_id, true);
  365. ResourceId res_id = m_resource_manager->load(type, name);
  366. m_resource_manager->flush();
  367. const void* new_res = m_resource_manager->get(res_id);
  368. uint32_t type_hash = string::murmur2_32(type, string::strlen(type), 0);
  369. switch (type_hash)
  370. {
  371. case UNIT_TYPE:
  372. {
  373. CE_LOGD("Reloading unit: %s", name);
  374. /// Reload unit in all worlds
  375. for (uint32_t i = 0; i < id_array::size(m_world_manager->worlds()); i++)
  376. {
  377. m_world_manager->worlds()[i]->reload_units((UnitResource*) old_res, (UnitResource*) new_res);
  378. }
  379. break;
  380. }
  381. case SOUND_TYPE:
  382. {
  383. CE_LOGD("Reloading sound: %s", name);
  384. for (uint32_t i = 0; i < id_array::size(m_world_manager->worlds()); i++)
  385. {
  386. m_world_manager->worlds()[i]->sound_world()->reload_sounds((SoundResource*) old_res, (SoundResource*) new_res);
  387. }
  388. break;
  389. }
  390. default:
  391. {
  392. CE_ASSERT(false, "Oops, unknown resource type: %s", type);
  393. break;
  394. }
  395. }
  396. #endif
  397. */
  398. }
  399. static Device* g_device;
  400. void set_device(Device* device)
  401. {
  402. g_device = device;
  403. }
  404. Device* device()
  405. {
  406. return g_device;
  407. }
  408. } // namespace crown