device.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. struct Accelerometer;
  33. class Bundle;
  34. class BundleCompiler;
  35. class ConsoleServer;
  36. class Filesystem;
  37. struct Keyboard;
  38. class LuaEnvironment;
  39. struct Mouse;
  40. class OsWindow;
  41. class Renderer;
  42. class ResourceManager;
  43. struct ResourcePackage;
  44. struct Touch;
  45. class World;
  46. class WorldManager;
  47. struct Camera;
  48. /// @defgroup Device Device
  49. /// Holds data for a display mode.
  50. ///
  51. /// @ingroup Device
  52. struct DisplayMode
  53. {
  54. uint32_t id;
  55. uint16_t width;
  56. uint16_t height;
  57. };
  58. /// This is the place where to look for accessing all of
  59. /// the engine subsystems and related stuff.
  60. ///
  61. /// @ingroup Device
  62. class Device
  63. {
  64. public:
  65. Device();
  66. ~Device();
  67. void init();
  68. /// Shutdowns the engine freeing all the allocated resources
  69. void shutdown();
  70. /// Returns the number of command line arguments passed to
  71. /// the engine executable.
  72. int32_t argc() const { return m_argc; }
  73. /// Returns the string value of the command line arguments passed
  74. /// to the engine executable.
  75. /// The size of the returned array is given by Device::argc().
  76. const char** argv() const { return (const char**) m_argv; }
  77. /// Returns wheter the engine is running (i.e. it is actually
  78. /// doing work).
  79. bool is_running() const;
  80. /// Returns whether the engine is correctly initialized
  81. bool is_init() const;
  82. /// Returns wheter the engine is paused
  83. bool is_paused() const;
  84. /// Return the number of frames rendered from the first
  85. /// call to Device::start()
  86. uint64_t frame_count() const;
  87. /// Returns the time in seconds needed to render the last frame
  88. float last_delta_time() const;
  89. /// Returns the time in seconds since the first call to start().
  90. double time_since_start() const;
  91. /// Forces the engine to actually start doing work.
  92. void start();
  93. /// Forces the engine to stop all the work it is doing
  94. /// and normally terminates the program.
  95. void stop();
  96. /// Pauses the engine
  97. void pause();
  98. /// Unpauses the engine
  99. void unpause();
  100. virtual int32_t run(int argc, char** argv) = 0;
  101. /// Returns an array of video @a modes.
  102. /// Each DisplayMode has an id that can be passed to set_video_mode().
  103. virtual void display_modes(Array<DisplayMode>& modes) = 0;
  104. /// Sets the video mode @a id.
  105. /// @note See video_modes().
  106. virtual void set_display_mode(uint32_t id) = 0;
  107. /// Sets whether in fullscreen or not.
  108. virtual void set_fullscreen(bool full) = 0;
  109. /// Updates all the subsystems
  110. void frame();
  111. /// Updates the given @a world and renders it from the given @a camera.
  112. void update_world(World* world, float dt);
  113. /// Renders the given @a world from the point of view of the given @æ camera.
  114. void render_world(World* world, Camera* camera);
  115. WorldId create_world();
  116. void destroy_world(WorldId world);
  117. /// Returns the resource package with the given @a package_name name.
  118. ResourcePackage* create_resource_package(const char* name);
  119. /// Destroy a previously created resource @a package.
  120. /// @note
  121. /// To unload the resources loaded by the package, you have to call
  122. /// ResourcePackage::unload() first.
  123. void destroy_resource_package(ResourcePackage* package);
  124. void reload(const char* type, const char* name);
  125. Filesystem* filesystem();
  126. ResourceManager* resource_manager();
  127. LuaEnvironment* lua_environment();
  128. OsWindow* window();
  129. Renderer* renderer();
  130. Keyboard* keyboard();
  131. Mouse* mouse();
  132. Touch* touch();
  133. Accelerometer* accelerometer();
  134. ConsoleServer* console() { return m_console; }
  135. WorldManager* world_manager() { return m_world_manager; }
  136. protected:
  137. // Used to allocate all subsystems
  138. LinearAllocator m_allocator;
  139. int32_t m_argc;
  140. char** m_argv;
  141. // Preferred settings
  142. char m_source_dir[MAX_PATH_LENGTH];
  143. char m_bundle_dir[MAX_PATH_LENGTH];
  144. char m_boot_file[MAX_PATH_LENGTH];
  145. int32_t m_fileserver;
  146. uint16_t m_console_port;
  147. bool m_is_init : 1;
  148. bool m_is_running : 1;
  149. bool m_is_paused : 1;
  150. uint64_t m_frame_count;
  151. int64_t m_last_time;
  152. int64_t m_current_time;
  153. float m_last_delta_time;
  154. double m_time_since_start;
  155. // Public subsystems
  156. Filesystem* m_filesystem;
  157. OsWindow* m_window;
  158. Keyboard* m_keyboard;
  159. Mouse* m_mouse;
  160. Touch* m_touch;
  161. LuaEnvironment* m_lua_environment;
  162. Renderer* m_renderer;
  163. // Private subsystems
  164. BundleCompiler* m_bundle_compiler;
  165. ConsoleServer* m_console;
  166. ResourceManager* m_resource_manager;
  167. Bundle* m_resource_bundle;
  168. WorldManager* m_world_manager;
  169. private:
  170. // Disable copying
  171. Device(const Device&);
  172. Device& operator=(const Device&);
  173. };
  174. Device* device();
  175. void set_device(Device* device);
  176. } // namespace crown