device.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "types.h"
  25. #include "config.h"
  26. #include "os.h"
  27. #include "linear_allocator.h"
  28. #include "resource.h"
  29. #include "world_types.h"
  30. namespace crown
  31. {
  32. class Bundle;
  33. class Filesystem;
  34. struct LuaEnvironment;
  35. class ResourceManager;
  36. struct ResourcePackage;
  37. class World;
  38. class WorldManager;
  39. struct Camera;
  40. /// @defgroup Device Device
  41. /// Holds data for a display mode.
  42. ///
  43. /// @ingroup Device
  44. struct DisplayMode
  45. {
  46. uint32_t id;
  47. uint16_t width;
  48. uint16_t height;
  49. };
  50. /// This is the place where to look for accessing all of
  51. /// the engine subsystems and related stuff.
  52. ///
  53. /// @ingroup Device
  54. struct Device
  55. {
  56. Device(Filesystem& fs, StringId64 boot_package, StringId64 boot_script);
  57. void init();
  58. /// Shutdowns the engine freeing all the allocated resources
  59. void shutdown();
  60. /// Returns a string identifying what platform the engine is running on.
  61. const char* platform() const { return CROWN_PLATFORM_NAME; }
  62. /// Returns a string identifying what architecture the engine is running on.
  63. const char* architecture() const { return CROWN_ARCH_NAME; }
  64. /// Returns a string identifying the engine version.
  65. const char* version() const { return CROWN_VERSION_MAJOR "." CROWN_VERSION_MINOR "." CROWN_VERSION_MICRO; }
  66. /// Returns wheter the engine is running (i.e. it is advancing
  67. /// the simulation).
  68. bool is_running() const;
  69. /// Return the number of frames rendered from the first
  70. /// call to Device::start()
  71. uint64_t frame_count() const;
  72. /// Returns the time in seconds needed to render the last frame
  73. float last_delta_time() const;
  74. /// Returns the time in seconds since the first call to start().
  75. double time_since_start() const;
  76. /// Quits the application.
  77. void quit();
  78. /// Pauses the engine
  79. void pause();
  80. /// Unpauses the engine
  81. void unpause();
  82. void update_resolution(uint16_t width, uint16_t height)
  83. {
  84. _width = width;
  85. _height = height;
  86. }
  87. /// Returns the main window resolution.
  88. void resolution(uint16_t& width, uint16_t& height)
  89. {
  90. width = _width;
  91. height = _height;
  92. }
  93. /// Updates all the subsystems
  94. void update();
  95. /// Renders the given @a world from the point of view of the given @a camera.
  96. void render_world(World* world, Camera* camera);
  97. /// Creates a new world.
  98. WorldId create_world();
  99. /// Destroys the given @a world.
  100. void destroy_world(WorldId world);
  101. /// Returns the resource package with the given @a package_name name.
  102. ResourcePackage* create_resource_package(const char* name);
  103. ResourcePackage* create_resource_package(StringId64 id);
  104. /// Destroy a previously created resource @a package.
  105. /// @note
  106. /// To unload the resources loaded by the package, you have to call
  107. /// ResourcePackage::unload() first.
  108. void destroy_resource_package(ResourcePackage* package);
  109. void reload(const char* type, const char* name);
  110. ResourceManager* resource_manager();
  111. LuaEnvironment* lua_environment();
  112. WorldManager* world_manager() { return _world_manager; }
  113. private:
  114. // Used to allocate all subsystems
  115. LinearAllocator _allocator;
  116. uint16_t _width;
  117. uint16_t _height;
  118. bool _is_init : 1;
  119. bool _is_running : 1;
  120. bool _is_paused : 1;
  121. uint64_t _frame_count;
  122. int64_t _last_time;
  123. int64_t _current_time;
  124. float _last_delta_time;
  125. double _time_since_start;
  126. Filesystem& _fs;
  127. StringId64 _boot_package_id;
  128. StringId64 _boot_script_id;
  129. ResourcePackage* _boot_package;
  130. LuaEnvironment* _lua_environment;
  131. ResourceManager* _resource_manager;
  132. Bundle* _resource_bundle;
  133. WorldManager* _world_manager;
  134. private:
  135. // Disable copying
  136. Device(const Device&);
  137. Device& operator=(const Device&);
  138. };
  139. namespace device_globals
  140. {
  141. void init(Filesystem& fs, StringId64 boot_package, StringId64 boot_script);
  142. void shutdown();
  143. } // namespace device_globals
  144. Device* device();
  145. } // namespace crown