device.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "config.h"
  25. #include "device.h"
  26. #include "log.h"
  27. #include "lua_environment.h"
  28. #include "resource_manager.h"
  29. #include "types.h"
  30. #include "bundle.h"
  31. #include "resource_package.h"
  32. #include "world.h"
  33. #include "lua_stack.h"
  34. #include "world_manager.h"
  35. #include "network_filesystem.h"
  36. #include "lua_system.h"
  37. #include "debug_line.h"
  38. #include "material_manager.h"
  39. #include <cstdlib>
  40. #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
  41. namespace crown
  42. {
  43. //-----------------------------------------------------------------------------
  44. Device::Device(Filesystem& fs, StringId64 boot_package, StringId64 boot_script)
  45. : _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
  46. , _width(0)
  47. , _height(0)
  48. , _is_init(false)
  49. , _is_running(false)
  50. , _is_paused(false)
  51. , _frame_count(0)
  52. , _last_time(0)
  53. , _current_time(0)
  54. , _last_delta_time(0.0f)
  55. , _time_since_start(0.0)
  56. , _fs(fs)
  57. , _boot_package_id(boot_package)
  58. , _boot_script_id(boot_script)
  59. , _boot_package(NULL)
  60. , _lua_environment(NULL)
  61. , _resource_manager(NULL)
  62. , _resource_bundle(NULL)
  63. , _world_manager(NULL)
  64. {
  65. }
  66. //-----------------------------------------------------------------------------
  67. void Device::init()
  68. {
  69. // Initialize
  70. CE_LOGI("Initializing Crown Engine %s...", version());
  71. _resource_bundle = Bundle::create(_allocator, _fs);
  72. // Create resource manager
  73. CE_LOGD("Creating resource manager...");
  74. _resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_bundle);
  75. // Create world manager
  76. CE_LOGD("Creating world manager...");
  77. _world_manager = CE_NEW(_allocator, WorldManager)();
  78. CE_LOGD("Creating material manager...");
  79. material_manager::init();
  80. debug_line::init();
  81. CE_LOGD("Creating lua system...");
  82. lua_system::init();
  83. _lua_environment = CE_NEW(_allocator, LuaEnvironment)(lua_system::state());
  84. CE_LOGD("Crown Engine initialized.");
  85. CE_LOGD("Initializing Game...");
  86. _is_init = true;
  87. _is_running = true;
  88. _last_time = os::clocktime();
  89. _boot_package = create_resource_package(_boot_package_id);
  90. _boot_package->load();
  91. _boot_package->flush();
  92. ResourceId bootid;
  93. bootid.type = LUA_TYPE;
  94. bootid.name = _boot_script_id;
  95. _lua_environment->execute((LuaResource*) _resource_manager->get(bootid));
  96. _lua_environment->call_global("init", 0);
  97. }
  98. //-----------------------------------------------------------------------------
  99. void Device::shutdown()
  100. {
  101. CE_ASSERT(_is_init, "Engine is not initialized");
  102. // Shutdowns the game
  103. _lua_environment->call_global("shutdown", 0);
  104. _boot_package->unload();
  105. destroy_resource_package(_boot_package);
  106. CE_LOGD("Releasing lua system...");
  107. lua_system::shutdown();
  108. CE_DELETE(_allocator, _lua_environment);
  109. CE_LOGD("Releasing material manager...");
  110. debug_line::shutdown();
  111. material_manager::shutdown();
  112. CE_LOGD("Releasing world manager...");
  113. CE_DELETE(_allocator, _world_manager);
  114. CE_LOGD("Releasing resource manager...");
  115. CE_DELETE(_allocator, _resource_manager);
  116. Bundle::destroy(_allocator, _resource_bundle);
  117. _allocator.clear();
  118. _is_init = false;
  119. }
  120. //-----------------------------------------------------------------------------
  121. ResourceManager* Device::resource_manager()
  122. {
  123. return _resource_manager;
  124. }
  125. //-----------------------------------------------------------------------------
  126. LuaEnvironment* Device::lua_environment()
  127. {
  128. return _lua_environment;
  129. }
  130. //-----------------------------------------------------------------------------
  131. void Device::quit()
  132. {
  133. _is_running = false;
  134. }
  135. //-----------------------------------------------------------------------------
  136. void Device::pause()
  137. {
  138. _is_paused = true;
  139. CE_LOGI("Engine paused.");
  140. }
  141. //-----------------------------------------------------------------------------
  142. void Device::unpause()
  143. {
  144. _is_paused = false;
  145. CE_LOGI("Engine unpaused.");
  146. }
  147. //-----------------------------------------------------------------------------
  148. bool Device::is_running() const
  149. {
  150. return _is_running;
  151. }
  152. //-----------------------------------------------------------------------------
  153. uint64_t Device::frame_count() const
  154. {
  155. return _frame_count;
  156. }
  157. //-----------------------------------------------------------------------------
  158. float Device::last_delta_time() const
  159. {
  160. return _last_delta_time;
  161. }
  162. //-----------------------------------------------------------------------------
  163. double Device::time_since_start() const
  164. {
  165. return _time_since_start;
  166. }
  167. //-----------------------------------------------------------------------------
  168. void Device::update()
  169. {
  170. _current_time = os::clocktime();
  171. const int64_t time = _current_time - _last_time;
  172. _last_time = _current_time;
  173. const double freq = (double) os::clockfrequency();
  174. _last_delta_time = time * (1.0 / freq);
  175. _time_since_start += _last_delta_time;
  176. if (!_is_paused)
  177. {
  178. _resource_manager->complete_requests();
  179. _lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
  180. }
  181. lua_system::clear_temporaries();
  182. _frame_count++;
  183. }
  184. //-----------------------------------------------------------------------------
  185. void Device::render_world(World* world, Camera* camera)
  186. {
  187. world->render(camera);
  188. }
  189. //-----------------------------------------------------------------------------
  190. WorldId Device::create_world()
  191. {
  192. return _world_manager->create_world();
  193. }
  194. //-----------------------------------------------------------------------------
  195. void Device::destroy_world(WorldId world)
  196. {
  197. _world_manager->destroy_world(world);
  198. }
  199. //-----------------------------------------------------------------------------
  200. ResourcePackage* Device::create_resource_package(const char* name)
  201. {
  202. ResourceId resid("package", name);
  203. return create_resource_package((StringId64) resid.name);
  204. }
  205. //-----------------------------------------------------------------------------
  206. ResourcePackage* Device::create_resource_package(StringId64 id)
  207. {
  208. return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
  209. }
  210. //-----------------------------------------------------------------------------
  211. void Device::destroy_resource_package(ResourcePackage* package)
  212. {
  213. CE_DELETE(default_allocator(), package);
  214. }
  215. //-----------------------------------------------------------------------------
  216. void Device::reload(const char* , const char* )
  217. {
  218. }
  219. namespace device_globals
  220. {
  221. Device* _device = NULL;
  222. void init(Filesystem& fs, StringId64 boot_package, StringId64 boot_script)
  223. {
  224. CE_ASSERT(_device == NULL, "Crown already initialized");
  225. _device = CE_NEW(default_allocator(), Device)(fs, boot_package, boot_script);
  226. }
  227. void shutdown()
  228. {
  229. CE_DELETE(default_allocator(), _device);
  230. }
  231. } // namespace device_globals
  232. Device* device()
  233. {
  234. return device_globals::_device;
  235. }
  236. } // namespace crown