device.cpp 4.8 KB

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