device.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "material_manager.h"
  11. #include "resource_manager.h"
  12. #include "resource_package.h"
  13. #include "types.h"
  14. #include "world.h"
  15. #include "memory.h"
  16. #include "os.h"
  17. #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
  18. namespace crown
  19. {
  20. Device::Device(const ConfigSettings& cs, Filesystem& fs)
  21. : _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
  22. , _width(0)
  23. , _height(0)
  24. , _is_init(false)
  25. , _is_running(false)
  26. , _is_paused(false)
  27. , _frame_count(0)
  28. , _last_time(0)
  29. , _current_time(0)
  30. , _last_delta_time(0.0f)
  31. , _time_since_start(0.0)
  32. , _cs(cs)
  33. , _fs(fs)
  34. , _boot_package_id(cs.boot_package)
  35. , _boot_script_id(cs.boot_script)
  36. , _boot_package(NULL)
  37. , _lua_environment(NULL)
  38. , _resource_manager(NULL)
  39. , _worlds(default_allocator())
  40. {
  41. }
  42. void Device::init()
  43. {
  44. // Initialize
  45. CE_LOGI("Initializing Crown Engine %s...", version());
  46. // Create resource manager
  47. CE_LOGD("Creating resource manager...");
  48. _resource_manager = CE_NEW(_allocator, ResourceManager)(_fs);
  49. CE_LOGD("Creating material manager...");
  50. material_manager::init();
  51. debug_line::init();
  52. _lua_environment = CE_NEW(_allocator, LuaEnvironment)();
  53. _lua_environment->load_libs();
  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. _lua_environment->clear_temporaries();
  135. }
  136. void Device::render_world(World* world, Camera* camera)
  137. {
  138. world->render(camera);
  139. }
  140. World* Device::create_world()
  141. {
  142. World* w = CE_NEW(default_allocator(), World)(*_resource_manager, *_lua_environment);
  143. array::push_back(_worlds, w);
  144. return w;
  145. }
  146. void Device::destroy_world(World& w)
  147. {
  148. for (uint32_t i = 0, n = array::size(_worlds); i < n; ++i)
  149. {
  150. if (&w == _worlds[i])
  151. {
  152. CE_DELETE(default_allocator(), &w);
  153. _worlds[i] = _worlds[n - 1];
  154. array::pop_back(_worlds);
  155. return;
  156. }
  157. }
  158. CE_ASSERT(false, "Bad world");
  159. }
  160. ResourcePackage* Device::create_resource_package(StringId64 id)
  161. {
  162. return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
  163. }
  164. void Device::destroy_resource_package(ResourcePackage& package)
  165. {
  166. CE_DELETE(default_allocator(), &package);
  167. }
  168. void Device::reload(StringId64 type, StringId64 name)
  169. {
  170. }
  171. namespace device_globals
  172. {
  173. char _buffer[sizeof(Device)];
  174. Device* _device = NULL;
  175. void init(const ConfigSettings& cs, Filesystem& fs)
  176. {
  177. CE_ASSERT(_device == NULL, "Crown already initialized");
  178. _device = new (_buffer) Device(cs, fs);
  179. }
  180. void shutdown()
  181. {
  182. _device->~Device();
  183. _device = NULL;
  184. }
  185. } // namespace device_globals
  186. Device* device()
  187. {
  188. return device_globals::_device;
  189. }
  190. } // namespace crown