Device.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. #if defined(LINUX) || defined(WINDOWS)
  52. #include "BundleCompiler.h"
  53. #endif
  54. #if defined(ANDROID)
  55. #include "ApkFilesystem.h"
  56. #endif
  57. #define MAX_SUBSYSTEMS_HEAP 16 * 1024 * 1024
  58. namespace crown
  59. {
  60. //-----------------------------------------------------------------------------
  61. Device::Device() :
  62. m_allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP),
  63. m_is_init(false),
  64. m_is_running(false),
  65. m_is_paused(false),
  66. m_is_really_paused(false),
  67. m_frame_count(0),
  68. m_last_time(0),
  69. m_current_time(0),
  70. m_last_delta_time(0.0f),
  71. m_time_since_start(0.0),
  72. m_filesystem(NULL),
  73. m_lua_environment(NULL),
  74. m_renderer(NULL),
  75. m_debug_renderer(NULL),
  76. m_bundle_compiler(NULL),
  77. m_resource_manager(NULL),
  78. m_resource_bundle(NULL),
  79. m_renderer_init_request(false)
  80. {
  81. // Bundle dir is current dir by default.
  82. string::strncpy(m_bundle_dir, os::get_cwd(), MAX_PATH_LENGTH);
  83. string::strncpy(m_source_dir, "", MAX_PATH_LENGTH);
  84. string::strncpy(m_boot_file, "lua/game", MAX_PATH_LENGTH);
  85. }
  86. //-----------------------------------------------------------------------------
  87. Device::~Device()
  88. {
  89. }
  90. //-----------------------------------------------------------------------------
  91. void Device::init()
  92. {
  93. // Initialize
  94. Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
  95. // Default bundle filesystem
  96. #if defined (LINUX) || defined(WINDOWS)
  97. m_filesystem = CE_NEW(m_allocator, DiskFilesystem)(m_bundle_dir);
  98. #elif defined(ANDROID)
  99. m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
  100. #endif
  101. Log::d("Filesystem created.");
  102. // Read settings from crown.config
  103. read_engine_settings();
  104. m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
  105. // Create resource manager
  106. m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, 0);
  107. Log::d("Resource manager created.");
  108. Log::d("Resource seed: %d", m_resource_manager->seed());
  109. // Create input devices
  110. m_keyboard = CE_NEW(m_allocator, Keyboard);
  111. m_mouse = CE_NEW(m_allocator, Mouse);
  112. m_touch = CE_NEW(m_allocator, Touch);
  113. // Create renderer
  114. m_renderer = CE_NEW(m_allocator, Renderer)(m_allocator);
  115. m_renderer->init();
  116. Log::d("Renderer created.");
  117. // Create debug renderer
  118. m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
  119. Log::d("Debug renderer created.");
  120. m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
  121. m_lua_environment->init();
  122. Log::d("Lua environment created.");
  123. Log::i("Crown Engine initialized.");
  124. Log::i("Initializing Game...");
  125. m_is_init = true;
  126. start();
  127. // Execute lua boot file
  128. if (m_lua_environment->load_and_execute(m_boot_file))
  129. {
  130. if (!m_lua_environment->call_global("init", 0))
  131. {
  132. pause();
  133. }
  134. }
  135. else
  136. {
  137. pause();
  138. }
  139. Log::d("Total allocated size: %llu", m_allocator.allocated_size());
  140. }
  141. //-----------------------------------------------------------------------------
  142. void Device::shutdown()
  143. {
  144. CE_ASSERT(is_init(), "Engine is not initialized");
  145. // Shutdowns the game
  146. m_lua_environment->call_global("shutdown", 0);
  147. Log::i("Releasing LuaEnvironment...");
  148. if (m_lua_environment)
  149. {
  150. m_lua_environment->shutdown();
  151. CE_DELETE(m_allocator, m_lua_environment);
  152. }
  153. Log::i("Releasing Input Devices...");
  154. CE_DELETE(m_allocator, m_touch);
  155. CE_DELETE(m_allocator, m_mouse);
  156. CE_DELETE(m_allocator, m_keyboard);
  157. Log::i("Releasing DebugRenderer...");
  158. if (m_debug_renderer)
  159. {
  160. CE_DELETE(m_allocator, m_debug_renderer);
  161. }
  162. Log::i("Releasing Renderer...");
  163. if (m_renderer)
  164. {
  165. m_renderer->shutdown();
  166. CE_DELETE(m_allocator, m_renderer);
  167. }
  168. Log::i("Releasing ResourceManager...");
  169. if (m_resource_manager)
  170. {
  171. CE_DELETE(m_allocator, m_resource_manager);
  172. }
  173. if (m_resource_bundle)
  174. {
  175. Bundle::destroy(m_allocator, m_resource_bundle);
  176. }
  177. Log::i("Releasing Filesystem...");
  178. if (m_filesystem)
  179. {
  180. CE_DELETE(m_allocator, m_filesystem);
  181. }
  182. m_allocator.clear();
  183. m_is_init = false;
  184. }
  185. //-----------------------------------------------------------------------------
  186. bool Device::is_init() const
  187. {
  188. return m_is_init;
  189. }
  190. //-----------------------------------------------------------------------------
  191. bool Device::is_paused() const
  192. {
  193. return m_is_paused;
  194. }
  195. //-----------------------------------------------------------------------------
  196. Filesystem* Device::filesystem()
  197. {
  198. return m_filesystem;
  199. }
  200. //-----------------------------------------------------------------------------
  201. ResourceManager* Device::resource_manager()
  202. {
  203. return m_resource_manager;
  204. }
  205. //-----------------------------------------------------------------------------
  206. LuaEnvironment* Device::lua_environment()
  207. {
  208. return m_lua_environment;
  209. }
  210. //-----------------------------------------------------------------------------
  211. Renderer* Device::renderer()
  212. {
  213. return m_renderer;
  214. }
  215. //-----------------------------------------------------------------------------
  216. DebugRenderer* Device::debug_renderer()
  217. {
  218. return m_debug_renderer;
  219. }
  220. //-----------------------------------------------------------------------------
  221. Keyboard* Device::keyboard()
  222. {
  223. return m_keyboard;
  224. }
  225. //-----------------------------------------------------------------------------
  226. Mouse* Device::mouse()
  227. {
  228. return m_mouse;
  229. }
  230. //-----------------------------------------------------------------------------
  231. Touch* Device::touch()
  232. {
  233. return m_touch;
  234. }
  235. //-----------------------------------------------------------------------------
  236. Accelerometer* Device::accelerometer()
  237. {
  238. return NULL;
  239. }
  240. //-----------------------------------------------------------------------------
  241. void Device::start()
  242. {
  243. CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
  244. m_is_running = true;
  245. m_last_time = os::milliseconds();
  246. }
  247. //-----------------------------------------------------------------------------
  248. void Device::stop()
  249. {
  250. CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
  251. m_is_running = false;
  252. }
  253. //-----------------------------------------------------------------------------
  254. void Device::pause()
  255. {
  256. m_is_paused = true;
  257. }
  258. //-----------------------------------------------------------------------------
  259. void Device::unpause()
  260. {
  261. m_is_paused = false;
  262. }
  263. //-----------------------------------------------------------------------------
  264. bool Device::is_running() const
  265. {
  266. return m_is_running;
  267. }
  268. //-----------------------------------------------------------------------------
  269. uint64_t Device::frame_count() const
  270. {
  271. return m_frame_count;
  272. }
  273. //-----------------------------------------------------------------------------
  274. float Device::last_delta_time() const
  275. {
  276. return m_last_delta_time;
  277. }
  278. //-----------------------------------------------------------------------------
  279. double Device::time_since_start() const
  280. {
  281. return m_time_since_start;
  282. }
  283. //-----------------------------------------------------------------------------
  284. void Device::frame()
  285. {
  286. m_current_time = os::microseconds();
  287. m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
  288. m_last_time = m_current_time;
  289. m_time_since_start += m_last_delta_time;
  290. if (!m_is_paused)
  291. {
  292. m_resource_manager->poll_resource_loader();
  293. m_renderer->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
  294. m_renderer->commit(0);
  295. if (!m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time()))
  296. {
  297. pause();
  298. }
  299. m_renderer->frame();
  300. }
  301. m_frame_count++;
  302. }
  303. //-----------------------------------------------------------------------------
  304. ResourcePackage* Device::create_resource_package(const char* name)
  305. {
  306. CE_ASSERT_NOT_NULL(name);
  307. ResourceId package_id = m_resource_manager->load("package", name);
  308. m_resource_manager->flush();
  309. PackageResource* package_res = (PackageResource*) m_resource_manager->data(package_id);
  310. ResourcePackage* package = CE_NEW(default_allocator(), ResourcePackage)(*m_resource_manager, package_id, package_res);
  311. return package;
  312. }
  313. //-----------------------------------------------------------------------------
  314. void Device::destroy_resource_package(ResourcePackage* package)
  315. {
  316. CE_ASSERT_NOT_NULL(package);
  317. m_resource_manager->unload(package->resource_id());
  318. CE_DELETE(default_allocator(), package);
  319. }
  320. //-----------------------------------------------------------------------------
  321. void Device::compile(const char* , const char* , const char* )
  322. {
  323. }
  324. //-----------------------------------------------------------------------------
  325. void Device::reload(ResourceId name)
  326. {
  327. (void)name;
  328. }
  329. //-----------------------------------------------------------------------------
  330. void Device::read_engine_settings()
  331. {
  332. // // Check crown.config existance
  333. // CE_ASSERT(m_filesystem->is_file("crown.config"), "Unable to open crown.config");
  334. // // Copy crown config in a buffer
  335. // TempAllocator4096 allocator;
  336. // File* config_file = m_filesystem->open("crown.config", FOM_READ);
  337. // char* json_string = (char*)allocator.allocate(config_file->size());
  338. // config_file->read(json_string, config_file->size());
  339. // m_filesystem->close(config_file);
  340. // // Parse crown.config
  341. // JSONParser parser(json_string);
  342. // JSONElement root = parser.root();
  343. // // Boot
  344. // if (root.has_key("boot"))
  345. // {
  346. // const char* boot = root.key("boot").string_value();
  347. // const size_t boot_length = string::strlen(boot) + 1;
  348. // string::strncpy(m_boot_file, boot, boot_length);
  349. // }
  350. // // Window width
  351. // if (root.has_key("window_width"))
  352. // {
  353. // m_preferred_window_width = root.key("window_width").int_value();
  354. // }
  355. // // Window height
  356. // if (root.has_key("window_height"))
  357. // {
  358. // m_preferred_window_height = root.key("window_height").int_value();
  359. // }
  360. // allocator.deallocate(json_string);
  361. // Log::i("Configuration set");
  362. }
  363. static Device* g_device;
  364. void set_device(Device* device)
  365. {
  366. g_device = device;
  367. }
  368. Device* device()
  369. {
  370. return g_device;
  371. }
  372. } // namespace crown