Device.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #define MAX_SUBSYSTEMS_HEAP 1024 * 1024
  30. namespace crown
  31. {
  32. class Filesystem;
  33. class ResourceManager;
  34. class OsWindow;
  35. class Bundle;
  36. class Renderer;
  37. class DebugRenderer;
  38. class InputManager;
  39. class Keyboard;
  40. class Mouse;
  41. class Touch;
  42. class Accelerometer;
  43. class LuaEnvironment;
  44. class ConsoleServer;
  45. class BundleCompiler;
  46. class ResourcePackage;
  47. /// The Engine.
  48. /// It 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. /// Initializes the engine allowing to pass command line
  56. /// parameters to configure some parameters.
  57. bool init(int argc, char** argv);
  58. /// Shutdowns the engine freeing all the allocated resources
  59. void shutdown();
  60. /// Returns wheter the engine is running (i.e. it is actually
  61. /// doing work).
  62. bool is_running() const;
  63. /// Returns whether the engine is correctly initialized
  64. bool is_init() const;
  65. /// Returns wheter the engine is paused
  66. bool is_paused() const;
  67. /// Return the number of frames rendered from the first
  68. /// call to Device::start()
  69. uint64_t frame_count() const;
  70. /// Returns the time in milliseconds needed to render
  71. /// the last frame
  72. float last_delta_time() const;
  73. /// Forces the engine to actually start doing work.
  74. void start();
  75. /// Forces the engine to stop all the work it is doing
  76. /// and normally terminates the program.
  77. void stop();
  78. /// Pauses the engine
  79. void pause();
  80. /// Unpauses the engine
  81. void unpause();
  82. /// Updates all the subsystems
  83. void frame();
  84. /// Returns the resource package with the given @a package_name name.
  85. ResourcePackage* create_resource_package(const char* name);
  86. /// Destroy a previously created resource @a package.
  87. /// @note
  88. /// To unload the resources loaded by the package, you have to call
  89. /// ResourcePackage::unload() first.
  90. void destroy_resource_package(ResourcePackage* package);
  91. void compile(const char* bundle_dir, const char* source_dir, const char* resource);
  92. void reload(ResourceId name);
  93. Filesystem* filesystem();
  94. ResourceManager* resource_manager();
  95. InputManager* input_manager();
  96. LuaEnvironment* lua_environment();
  97. OsWindow* window();
  98. Renderer* renderer();
  99. DebugRenderer* debug_renderer();
  100. Keyboard* keyboard();
  101. Mouse* mouse();
  102. Touch* touch();
  103. Accelerometer* accelerometer();
  104. ConsoleServer* console_server();
  105. inline void init_renderer() { m_renderer_init_request = true; }
  106. private:
  107. void init();
  108. void parse_command_line(int argc, char** argv);
  109. void check_preferred_settings();
  110. void read_engine_settings();
  111. void print_help_message();
  112. private:
  113. // Used to allocate all subsystems
  114. uint8_t m_subsystems_heap[MAX_SUBSYSTEMS_HEAP];
  115. LinearAllocator m_allocator;
  116. // Preferred settings
  117. int32_t m_preferred_window_width;
  118. int32_t m_preferred_window_height;
  119. int32_t m_preferred_window_fullscreen;
  120. uint32_t m_parent_window_handle;
  121. char m_source_dir[MAX_PATH_LENGTH];
  122. char m_bundle_dir[MAX_PATH_LENGTH];
  123. char m_boot_file[MAX_PATH_LENGTH];
  124. int32_t m_compile;
  125. int32_t m_continue;
  126. int32_t m_quit_after_init;
  127. bool m_is_init : 1;
  128. bool m_is_running : 1;
  129. bool m_is_paused : 1;
  130. bool m_is_really_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. // Public subsystems
  136. Filesystem* m_filesystem;
  137. OsWindow* m_window;
  138. InputManager* m_input_manager;
  139. LuaEnvironment* m_lua_environment;
  140. Renderer* m_renderer;
  141. DebugRenderer* m_debug_renderer;
  142. // Private subsystems
  143. BundleCompiler* m_bundle_compiler;
  144. ResourceManager* m_resource_manager;
  145. Bundle* m_resource_bundle;
  146. // Debug subsystems
  147. ConsoleServer* m_console_server;
  148. bool m_renderer_init_request;
  149. private:
  150. // Disable copying
  151. Device(const Device&);
  152. Device& operator=(const Device&);
  153. friend class MainThread;
  154. };
  155. CE_EXPORT Device* device();
  156. } // namespace crown