Device.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 "ConsoleServer.h"
  30. #include "DebugRenderer.h"
  31. #include "DiskFile.h"
  32. #include "DiskFilesystem.h"
  33. #include "JSONParser.h"
  34. #include "Keyboard.h"
  35. #include "Log.h"
  36. #include "LuaEnvironment.h"
  37. #include "Memory.h"
  38. #include "Mouse.h"
  39. #include "OS.h"
  40. #include "OsWindow.h"
  41. #include "Renderer.h"
  42. #include "ResourceManager.h"
  43. #include "StringSetting.h"
  44. #include "StringUtils.h"
  45. #include "TextReader.h"
  46. #include "Touch.h"
  47. #include "Types.h"
  48. #include "Bundle.h"
  49. #include "TempAllocator.h"
  50. #include "ResourcePackage.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_resource_manager(NULL),
  80. m_resource_bundle(NULL)
  81. {
  82. // Bundle dir is current dir by default.
  83. string::strncpy(m_bundle_dir, os::get_cwd(), MAX_PATH_LENGTH);
  84. string::strncpy(m_source_dir, "", MAX_PATH_LENGTH);
  85. string::strncpy(m_boot_file, "lua/game", MAX_PATH_LENGTH);
  86. }
  87. //-----------------------------------------------------------------------------
  88. Device::~Device()
  89. {
  90. }
  91. //-----------------------------------------------------------------------------
  92. void Device::init()
  93. {
  94. // Initialize
  95. Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  96. // Default bundle filesystem
  97. #if defined (LINUX) || defined(WINDOWS)
  98. m_filesystem = CE_NEW(m_allocator, DiskFilesystem)(m_bundle_dir);
  99. #elif defined(ANDROID)
  100. m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
  101. #endif
  102. Log::d("Filesystem created.");
  103. // Read settings from crown.config
  104. read_engine_settings();
  105. m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
  106. // Create resource manager
  107. m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, 0);
  108. Log::d("Resource manager created.");
  109. Log::d("Resource seed: %d", m_resource_manager->seed());
  110. // Create window
  111. m_window = CE_NEW(m_allocator, OsWindow);
  112. // Create input devices
  113. m_keyboard = CE_NEW(m_allocator, Keyboard);
  114. m_mouse = CE_NEW(m_allocator, Mouse);
  115. m_touch = CE_NEW(m_allocator, Touch);
  116. // Create renderer
  117. m_renderer = CE_NEW(m_allocator, Renderer)(m_allocator);
  118. m_renderer->init();
  119. Log::d("Renderer created.");
  120. // Create debug renderer
  121. m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
  122. Log::d("Debug renderer created.");
  123. m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
  124. m_lua_environment->init();
  125. Log::d("Lua environment created.");
  126. m_sound_renderer = CE_NEW(m_allocator, SoundRenderer)(m_allocator);
  127. m_sound_renderer->init();
  128. Log::d("SoundRenderer created.");
  129. ResourceId id = m_resource_manager->load("sound", "sounds/untrue");
  130. m_resource_manager->flush();
  131. SoundResource* res = (SoundResource*)m_resource_manager->data(id);
  132. SoundId sound = m_sound_renderer->create_sound(res);
  133. m_sound_renderer->set_sound_loop(sound, true);
  134. m_sound_renderer->play_sound(sound);
  135. Log::i("Crown Engine initialized.");
  136. Log::i("Initializing Game...");
  137. m_is_init = true;
  138. start();
  139. // Execute lua boot file
  140. if (m_lua_environment->load_and_execute(m_boot_file))
  141. {
  142. if (!m_lua_environment->call_global("init", 0))
  143. {
  144. pause();
  145. }
  146. }
  147. else
  148. {
  149. pause();
  150. }
  151. Log::d("Total allocated size: %llu", m_allocator.allocated_size());
  152. }
  153. //-----------------------------------------------------------------------------
  154. void Device::shutdown()
  155. {
  156. CE_ASSERT(is_init(), "Engine is not initialized");
  157. // Shutdowns the game
  158. m_lua_environment->call_global("shutdown", 0);
  159. Log::d("Releasing SoundRenderer...");
  160. if (m_sound_renderer)
  161. {
  162. m_sound_renderer->shutdown();
  163. CE_DELETE(m_allocator, m_sound_renderer);
  164. }
  165. Log::i("Releasing LuaEnvironment...");
  166. if (m_lua_environment)
  167. {
  168. m_lua_environment->shutdown();
  169. CE_DELETE(m_allocator, m_lua_environment);
  170. }
  171. Log::i("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. Log::i("Releasing DebugRenderer...");
  176. if (m_debug_renderer)
  177. {
  178. CE_DELETE(m_allocator, m_debug_renderer);
  179. }
  180. Log::i("Releasing Renderer...");
  181. if (m_renderer)
  182. {
  183. m_renderer->shutdown();
  184. CE_DELETE(m_allocator, m_renderer);
  185. }
  186. Log::i("Releasing ResourceManager...");
  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. Log::i("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 m_renderer;
  237. }
  238. //-----------------------------------------------------------------------------
  239. DebugRenderer* Device::debug_renderer()
  240. {
  241. return m_debug_renderer;
  242. }
  243. //-----------------------------------------------------------------------------
  244. SoundRenderer* Device::sound_renderer()
  245. {
  246. return m_sound_renderer;
  247. }
  248. //-----------------------------------------------------------------------------
  249. Keyboard* Device::keyboard()
  250. {
  251. return m_keyboard;
  252. }
  253. //-----------------------------------------------------------------------------
  254. Mouse* Device::mouse()
  255. {
  256. return m_mouse;
  257. }
  258. //-----------------------------------------------------------------------------
  259. Touch* Device::touch()
  260. {
  261. return m_touch;
  262. }
  263. //-----------------------------------------------------------------------------
  264. Accelerometer* Device::accelerometer()
  265. {
  266. return NULL;
  267. }
  268. //-----------------------------------------------------------------------------
  269. void Device::start()
  270. {
  271. CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
  272. m_is_running = true;
  273. m_last_time = os::milliseconds();
  274. }
  275. //-----------------------------------------------------------------------------
  276. void Device::stop()
  277. {
  278. CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
  279. m_is_running = false;
  280. }
  281. //-----------------------------------------------------------------------------
  282. void Device::pause()
  283. {
  284. m_is_paused = true;
  285. }
  286. //-----------------------------------------------------------------------------
  287. void Device::unpause()
  288. {
  289. m_is_paused = false;
  290. }
  291. //-----------------------------------------------------------------------------
  292. bool Device::is_running() const
  293. {
  294. return m_is_running;
  295. }
  296. //-----------------------------------------------------------------------------
  297. uint64_t Device::frame_count() const
  298. {
  299. return m_frame_count;
  300. }
  301. //-----------------------------------------------------------------------------
  302. float Device::last_delta_time() const
  303. {
  304. return m_last_delta_time;
  305. }
  306. //-----------------------------------------------------------------------------
  307. double Device::time_since_start() const
  308. {
  309. return m_time_since_start;
  310. }
  311. //-----------------------------------------------------------------------------
  312. void Device::frame()
  313. {
  314. m_current_time = os::microseconds();
  315. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  316. m_last_time = m_current_time;
  317. m_time_since_start += m_last_delta_time;
  318. if (!m_is_paused)
  319. {
  320. m_resource_manager->poll_resource_loader();
  321. m_renderer->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
  322. m_renderer->commit(0);
  323. if (!m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time()))
  324. {
  325. pause();
  326. }
  327. m_renderer->frame();
  328. // FIXME: SoundRenderer should not be updated each frame
  329. m_sound_renderer->frame();
  330. }
  331. m_frame_count++;
  332. }
  333. //-----------------------------------------------------------------------------
  334. ResourcePackage* Device::create_resource_package(const char* name)
  335. {
  336. CE_ASSERT_NOT_NULL(name);
  337. ResourceId package_id = m_resource_manager->load("package", name);
  338. m_resource_manager->flush();
  339. PackageResource* package_res = (PackageResource*) m_resource_manager->data(package_id);
  340. ResourcePackage* package = CE_NEW(default_allocator(), ResourcePackage)(*m_resource_manager, package_id, package_res);
  341. return package;
  342. }
  343. //-----------------------------------------------------------------------------
  344. void Device::destroy_resource_package(ResourcePackage* package)
  345. {
  346. CE_ASSERT_NOT_NULL(package);
  347. m_resource_manager->unload(package->resource_id());
  348. CE_DELETE(default_allocator(), package);
  349. }
  350. //-----------------------------------------------------------------------------
  351. void Device::compile(const char* , const char* , const char* )
  352. {
  353. }
  354. //-----------------------------------------------------------------------------
  355. void Device::reload(ResourceId name)
  356. {
  357. (void)name;
  358. }
  359. //-----------------------------------------------------------------------------
  360. void Device::read_engine_settings()
  361. {
  362. // // Check crown.config existance
  363. // CE_ASSERT(m_filesystem->is_file("crown.config"), "Unable to open crown.config");
  364. // // Copy crown config in a buffer
  365. // TempAllocator4096 allocator;
  366. // File* config_file = m_filesystem->open("crown.config", FOM_READ);
  367. // char* json_string = (char*)allocator.allocate(config_file->size());
  368. // config_file->read(json_string, config_file->size());
  369. // m_filesystem->close(config_file);
  370. // // Parse crown.config
  371. // JSONParser parser(json_string);
  372. // JSONElement root = parser.root();
  373. // // Boot
  374. // if (root.has_key("boot"))
  375. // {
  376. // const char* boot = root.key("boot").string_value();
  377. // const size_t boot_length = string::strlen(boot) + 1;
  378. // string::strncpy(m_boot_file, boot, boot_length);
  379. // }
  380. // // Window width
  381. // if (root.has_key("window_width"))
  382. // {
  383. // m_preferred_window_width = root.key("window_width").int_value();
  384. // }
  385. // // Window height
  386. // if (root.has_key("window_height"))
  387. // {
  388. // m_preferred_window_height = root.key("window_height").int_value();
  389. // }
  390. // allocator.deallocate(json_string);
  391. // Log::i("Configuration set");
  392. }
  393. static Device* g_device;
  394. void set_device(Device* device)
  395. {
  396. g_device = device;
  397. }
  398. Device* device()
  399. {
  400. return g_device;
  401. }
  402. } // namespace crown