device.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #include "device.h"
  7. #include "log.h"
  8. #include "lua_environment.h"
  9. #include "resource_manager.h"
  10. #include "types.h"
  11. #include "bundle.h"
  12. #include "resource_package.h"
  13. #include "world.h"
  14. #include "lua_stack.h"
  15. #include "world_manager.h"
  16. #include "network_filesystem.h"
  17. #include "lua_system.h"
  18. #include "debug_line.h"
  19. #include "material_manager.h"
  20. #include <cstdlib>
  21. #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
  22. namespace crown
  23. {
  24. Device::Device(Filesystem& fs, StringId64 boot_package, StringId64 boot_script)
  25. : _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
  26. , _width(0)
  27. , _height(0)
  28. , _is_init(false)
  29. , _is_running(false)
  30. , _is_paused(false)
  31. , _frame_count(0)
  32. , _last_time(0)
  33. , _current_time(0)
  34. , _last_delta_time(0.0f)
  35. , _time_since_start(0.0)
  36. , _fs(fs)
  37. , _boot_package_id(boot_package)
  38. , _boot_script_id(boot_script)
  39. , _boot_package(NULL)
  40. , _lua_environment(NULL)
  41. , _resource_manager(NULL)
  42. , _resource_bundle(NULL)
  43. , _world_manager(NULL)
  44. {
  45. }
  46. void Device::init()
  47. {
  48. // Initialize
  49. CE_LOGI("Initializing Crown Engine %s...", version());
  50. _resource_bundle = Bundle::create(_allocator, _fs);
  51. // Create resource manager
  52. CE_LOGD("Creating resource manager...");
  53. _resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_bundle);
  54. // Create world manager
  55. CE_LOGD("Creating world manager...");
  56. _world_manager = CE_NEW(_allocator, WorldManager)();
  57. CE_LOGD("Creating material manager...");
  58. material_manager::init();
  59. debug_line::init();
  60. _lua_environment = CE_NEW(_allocator, LuaEnvironment)(lua_globals::state());
  61. CE_LOGD("Crown Engine initialized.");
  62. CE_LOGD("Initializing Game...");
  63. _is_init = true;
  64. _is_running = true;
  65. _last_time = os::clocktime();
  66. _boot_package = create_resource_package(_boot_package_id);
  67. _boot_package->load();
  68. _boot_package->flush();
  69. _lua_environment->execute((LuaResource*)_resource_manager->get(LUA_TYPE, _boot_script_id));
  70. _lua_environment->call_global("init", 0);
  71. }
  72. void Device::shutdown()
  73. {
  74. CE_ASSERT(_is_init, "Engine is not initialized");
  75. // Shutdowns the game
  76. _lua_environment->call_global("shutdown", 0);
  77. _boot_package->unload();
  78. destroy_resource_package(_boot_package);
  79. CE_DELETE(_allocator, _lua_environment);
  80. CE_LOGD("Releasing material manager...");
  81. debug_line::shutdown();
  82. material_manager::shutdown();
  83. CE_LOGD("Releasing world manager...");
  84. CE_DELETE(_allocator, _world_manager);
  85. CE_LOGD("Releasing resource manager...");
  86. CE_DELETE(_allocator, _resource_manager);
  87. Bundle::destroy(_allocator, _resource_bundle);
  88. _allocator.clear();
  89. _is_init = false;
  90. }
  91. ResourceManager* Device::resource_manager()
  92. {
  93. return _resource_manager;
  94. }
  95. LuaEnvironment* Device::lua_environment()
  96. {
  97. return _lua_environment;
  98. }
  99. void Device::quit()
  100. {
  101. _is_running = false;
  102. }
  103. void Device::pause()
  104. {
  105. _is_paused = true;
  106. CE_LOGI("Engine paused.");
  107. }
  108. void Device::unpause()
  109. {
  110. _is_paused = false;
  111. CE_LOGI("Engine unpaused.");
  112. }
  113. bool Device::is_running() const
  114. {
  115. return _is_running;
  116. }
  117. uint64_t Device::frame_count() const
  118. {
  119. return _frame_count;
  120. }
  121. float Device::last_delta_time() const
  122. {
  123. return _last_delta_time;
  124. }
  125. double Device::time_since_start() const
  126. {
  127. return _time_since_start;
  128. }
  129. void Device::update()
  130. {
  131. _current_time = os::clocktime();
  132. const int64_t time = _current_time - _last_time;
  133. _last_time = _current_time;
  134. const double freq = (double) os::clockfrequency();
  135. _last_delta_time = time * (1.0 / freq);
  136. _time_since_start += _last_delta_time;
  137. if (!_is_paused)
  138. {
  139. _resource_manager->complete_requests();
  140. _lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
  141. _lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
  142. }
  143. _frame_count++;
  144. }
  145. void Device::render_world(World* world, Camera* camera)
  146. {
  147. world->render(camera);
  148. }
  149. WorldId Device::create_world()
  150. {
  151. return _world_manager->create_world();
  152. }
  153. void Device::destroy_world(WorldId world)
  154. {
  155. _world_manager->destroy_world(world);
  156. }
  157. ResourcePackage* Device::create_resource_package(const char* name)
  158. {
  159. ResourceId resid("package", name);
  160. return create_resource_package((StringId64) resid.name);
  161. }
  162. ResourcePackage* Device::create_resource_package(StringId64 id)
  163. {
  164. return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
  165. }
  166. void Device::destroy_resource_package(ResourcePackage* package)
  167. {
  168. CE_DELETE(default_allocator(), package);
  169. }
  170. void Device::reload(const char* , const char* )
  171. {
  172. }
  173. namespace device_globals
  174. {
  175. char _buffer[sizeof(Device)];
  176. Device* _device = NULL;
  177. void init(Filesystem& fs, StringId64 boot_package, StringId64 boot_script)
  178. {
  179. CE_ASSERT(_device == NULL, "Crown already initialized");
  180. _device = new (_buffer) Device(fs, boot_package, boot_script);
  181. }
  182. void shutdown()
  183. {
  184. _device->~Device();
  185. _device = NULL;
  186. }
  187. } // namespace device_globals
  188. Device* device()
  189. {
  190. return device_globals::_device;
  191. }
  192. } // namespace crown