device.cpp 4.8 KB

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