Device.h 5.5 KB

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