Device.cpp 11 KB

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