Device.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. namespace crown
  30. {
  31. class Filesystem;
  32. class ResourceManager;
  33. class OsWindow;
  34. class Bundle;
  35. class Renderer;
  36. class DebugRenderer;
  37. class Keyboard;
  38. class Mouse;
  39. class Touch;
  40. class Accelerometer;
  41. class LuaEnvironment;
  42. class SoundRenderer;
  43. class ConsoleServer;
  44. class BundleCompiler;
  45. class ResourcePackage;
  46. /// The Engine.
  47. /// It is the place where to look for accessing all of
  48. /// the engine subsystems and related stuff.
  49. class CE_EXPORT Device
  50. {
  51. public:
  52. Device();
  53. ~Device();
  54. void init();
  55. /// Shutdowns the engine freeing all the allocated resources
  56. void shutdown();
  57. /// Returns wheter the engine is running (i.e. it is actually
  58. /// doing work).
  59. bool is_running() const;
  60. /// Returns whether the engine is correctly initialized
  61. bool is_init() const;
  62. /// Returns wheter the engine is paused
  63. bool is_paused() const;
  64. /// Return the number of frames rendered from the first
  65. /// call to Device::start()
  66. uint64_t frame_count() const;
  67. /// Returns the time in seconds needed to render the last frame
  68. float last_delta_time() const;
  69. /// Returns the time in seconds since the first call to start().
  70. double time_since_start() const;
  71. /// Forces the engine to actually start doing work.
  72. void start();
  73. /// Forces the engine to stop all the work it is doing
  74. /// and normally terminates the program.
  75. void stop();
  76. /// Pauses the engine
  77. void pause();
  78. /// Unpauses the engine
  79. void unpause();
  80. virtual int32_t run(int argc, char** argv) = 0;
  81. /// Updates all the subsystems
  82. void frame();
  83. /// Returns the resource package with the given @a package_name name.
  84. ResourcePackage* create_resource_package(const char* name);
  85. /// Destroy a previously created resource @a package.
  86. /// @note
  87. /// To unload the resources loaded by the package, you have to call
  88. /// ResourcePackage::unload() first.
  89. void destroy_resource_package(ResourcePackage* package);
  90. void compile(const char* bundle_dir, const char* source_dir, const char* resource);
  91. void reload(ResourceId name);
  92. Filesystem* filesystem();
  93. ResourceManager* resource_manager();
  94. LuaEnvironment* lua_environment();
  95. OsWindow* window();
  96. Renderer* renderer();
  97. DebugRenderer* debug_renderer();
  98. SoundRenderer* sound_renderer();
  99. Keyboard* keyboard();
  100. Mouse* mouse();
  101. Touch* touch();
  102. Accelerometer* accelerometer();
  103. ConsoleServer* console_server();
  104. private:
  105. void read_engine_settings();
  106. protected:
  107. // Used to allocate all subsystems
  108. LinearAllocator m_allocator;
  109. // Preferred settings
  110. char m_source_dir[MAX_PATH_LENGTH];
  111. char m_bundle_dir[MAX_PATH_LENGTH];
  112. char m_boot_file[MAX_PATH_LENGTH];
  113. bool m_is_init : 1;
  114. bool m_is_running : 1;
  115. bool m_is_paused : 1;
  116. bool m_is_really_paused :1;
  117. uint64_t m_frame_count;
  118. uint64_t m_last_time;
  119. uint64_t m_current_time;
  120. float m_last_delta_time;
  121. double m_time_since_start;
  122. // Public subsystems
  123. Filesystem* m_filesystem;
  124. OsWindow* m_window;
  125. Keyboard* m_keyboard;
  126. Mouse* m_mouse;
  127. Touch* m_touch;
  128. LuaEnvironment* m_lua_environment;
  129. Renderer* m_renderer;
  130. DebugRenderer* m_debug_renderer;
  131. SoundRenderer* m_sound_renderer;
  132. // Private subsystems
  133. BundleCompiler* m_bundle_compiler;
  134. ResourceManager* m_resource_manager;
  135. Bundle* m_resource_bundle;
  136. bool m_renderer_init_request;
  137. private:
  138. // Disable copying
  139. Device(const Device&);
  140. Device& operator=(const Device&);
  141. };
  142. CE_EXPORT Device* device();
  143. CE_EXPORT void set_device(Device* device);
  144. } // namespace crown