device.cpp 5.9 KB

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