device.cpp 12 KB

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