device.cpp 4.7 KB

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