Device.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /// The Engine.
  46. /// It is the place where to look for accessing all of
  47. /// the engine subsystems and related stuff.
  48. class CE_EXPORT Device
  49. {
  50. public:
  51. Device();
  52. ~Device();
  53. /// Initializes the engine allowing to pass command line
  54. /// parameters to configure some parameters.
  55. bool init(int argc, char** argv);
  56. /// Shutdowns the engine freeing all the allocated resources
  57. void shutdown();
  58. /// Returns wheter the engine is running (i.e. it is actually
  59. /// doing work).
  60. bool is_running() const;
  61. /// Returns whether the engine is correctly initialized
  62. bool is_init() const;
  63. /// Return the number of frames rendered from the first
  64. /// call to Device::start()
  65. uint64_t frame_count() const;
  66. /// Returns the time in milliseconds needed to render
  67. /// the last frame
  68. float last_delta_time() const;
  69. /// Forces the engine to actually start doing work.
  70. void start();
  71. /// Forces the engine to stop all the work it is doing
  72. /// and normally terminates the program.
  73. void stop();
  74. /// Updates all the subsystems
  75. void frame();
  76. /// Loads a resource and returns its unique identifier.
  77. ResourceId load(const char* name);
  78. void unload(ResourceId name);
  79. void reload(ResourceId name);
  80. bool is_loaded(ResourceId name);
  81. const void* data(ResourceId name);
  82. Filesystem* filesystem();
  83. ResourceManager* resource_manager();
  84. InputManager* input_manager();
  85. LuaEnvironment* lua_environment();
  86. OsWindow* window();
  87. Renderer* renderer();
  88. DebugRenderer* debug_renderer();
  89. Keyboard* keyboard();
  90. Mouse* mouse();
  91. Touch* touch();
  92. Accelerometer* accelerometer();
  93. ConsoleServer* console_server();
  94. private:
  95. void create_filesystem();
  96. void create_resource_manager();
  97. void create_input_manager();
  98. void create_lua_environment();
  99. void create_window();
  100. void create_renderer();
  101. void create_debug_renderer();
  102. void create_console_server();
  103. void parse_command_line(int argc, char** argv);
  104. void check_preferred_settings();
  105. void read_engine_settings();
  106. void print_help_message();
  107. private:
  108. // Used to allocate all subsystems
  109. uint8_t m_subsystems_heap[MAX_SUBSYSTEMS_HEAP];
  110. LinearAllocator m_allocator;
  111. // Preferred settings from command line
  112. int32_t m_preferred_window_width;
  113. int32_t m_preferred_window_height;
  114. int32_t m_preferred_window_fullscreen;
  115. int32_t m_preferred_mode;
  116. char m_preferred_root_path[MAX_PATH_LENGTH];
  117. int32_t m_quit_after_init;
  118. bool m_is_init : 1;
  119. bool m_is_running : 1;
  120. uint64_t m_frame_count;
  121. uint64_t m_last_time;
  122. uint64_t m_current_time;
  123. float m_last_delta_time;
  124. // Public subsystems
  125. Filesystem* m_filesystem;
  126. OsWindow* m_window;
  127. InputManager* m_input_manager;
  128. LuaEnvironment* m_lua_environment;
  129. Renderer* m_renderer;
  130. DebugRenderer* m_debug_renderer;
  131. // Private subsystems
  132. ResourceManager* m_resource_manager;
  133. Bundle* m_resource_bundle;
  134. // Debug subsystems
  135. ConsoleServer* m_console_server;
  136. private:
  137. enum
  138. {
  139. MODE_RELEASE,
  140. MODE_DEVELOPMENT
  141. };
  142. // Disable copying
  143. Device(const Device&);
  144. Device& operator=(const Device&);
  145. };
  146. CE_EXPORT Device* device();
  147. } // namespace crown