device.cpp 12 KB

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