device.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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(Filesystem& fs, StringId64 boot_package, StringId64 boot_script)
  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. , _fs(fs)
  34. , _boot_package_id(boot_package)
  35. , _boot_script_id(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)(lua_globals::state());
  53. CE_LOGD("Crown Engine initialized.");
  54. CE_LOGD("Initializing Game...");
  55. _is_init = true;
  56. _is_running = true;
  57. _last_time = os::clocktime();
  58. _boot_package = create_resource_package(_boot_package_id);
  59. _boot_package->load();
  60. _boot_package->flush();
  61. _lua_environment->execute((LuaResource*)_resource_manager->get(LUA_TYPE, _boot_script_id));
  62. _lua_environment->call_global("init", 0);
  63. }
  64. void Device::shutdown()
  65. {
  66. CE_ASSERT(_is_init, "Engine is not initialized");
  67. // Shutdowns the game
  68. _lua_environment->call_global("shutdown", 0);
  69. _boot_package->unload();
  70. destroy_resource_package(_boot_package);
  71. CE_DELETE(_allocator, _lua_environment);
  72. CE_LOGD("Releasing material manager...");
  73. debug_line::shutdown();
  74. material_manager::shutdown();
  75. CE_LOGD("Releasing resource manager...");
  76. CE_DELETE(_allocator, _resource_manager);
  77. _allocator.clear();
  78. _is_init = false;
  79. }
  80. ResourceManager* Device::resource_manager()
  81. {
  82. return _resource_manager;
  83. }
  84. LuaEnvironment* Device::lua_environment()
  85. {
  86. return _lua_environment;
  87. }
  88. void Device::quit()
  89. {
  90. _is_running = false;
  91. }
  92. void Device::pause()
  93. {
  94. _is_paused = true;
  95. CE_LOGI("Engine paused.");
  96. }
  97. void Device::unpause()
  98. {
  99. _is_paused = false;
  100. CE_LOGI("Engine unpaused.");
  101. }
  102. bool Device::is_running() const
  103. {
  104. return _is_running;
  105. }
  106. uint64_t Device::frame_count() const
  107. {
  108. return _frame_count;
  109. }
  110. float Device::last_delta_time() const
  111. {
  112. return _last_delta_time;
  113. }
  114. double Device::time_since_start() const
  115. {
  116. return _time_since_start;
  117. }
  118. void Device::update()
  119. {
  120. _current_time = os::clocktime();
  121. const int64_t time = _current_time - _last_time;
  122. _last_time = _current_time;
  123. const double freq = (double) os::clockfrequency();
  124. _last_delta_time = time * (1.0 / freq);
  125. _time_since_start += _last_delta_time;
  126. if (!_is_paused)
  127. {
  128. _resource_manager->complete_requests();
  129. _lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
  130. _lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
  131. }
  132. _frame_count++;
  133. }
  134. void Device::render_world(World* world, Camera* camera)
  135. {
  136. world->render(camera);
  137. }
  138. World* Device::create_world()
  139. {
  140. World* w = CE_NEW(default_allocator(), World)();
  141. array::push_back(_worlds, w);
  142. return w;
  143. }
  144. void Device::destroy_world(World& w)
  145. {
  146. for (uint32_t i = 0, n = array::size(_worlds); i < n; ++i)
  147. {
  148. if (&w == _worlds[i])
  149. {
  150. CE_DELETE(default_allocator(), &w);
  151. _worlds[i] = _worlds[n - 1];
  152. array::pop_back(_worlds);
  153. return;
  154. }
  155. }
  156. CE_ASSERT(false, "Bad world");
  157. }
  158. ResourcePackage* Device::create_resource_package(const char* name)
  159. {
  160. ResourceId resid("package", name);
  161. return create_resource_package((StringId64) resid.name);
  162. }
  163. ResourcePackage* Device::create_resource_package(StringId64 id)
  164. {
  165. return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
  166. }
  167. void Device::destroy_resource_package(ResourcePackage* package)
  168. {
  169. CE_DELETE(default_allocator(), package);
  170. }
  171. void Device::reload(const char* , const char* )
  172. {
  173. }
  174. namespace device_globals
  175. {
  176. char _buffer[sizeof(Device)];
  177. Device* _device = NULL;
  178. void init(Filesystem& fs, StringId64 boot_package, StringId64 boot_script)
  179. {
  180. CE_ASSERT(_device == NULL, "Crown already initialized");
  181. _device = new (_buffer) Device(fs, boot_package, boot_script);
  182. }
  183. void shutdown()
  184. {
  185. _device->~Device();
  186. _device = NULL;
  187. }
  188. } // namespace device_globals
  189. Device* device()
  190. {
  191. return device_globals::_device;
  192. }
  193. } // namespace crown