device.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "types.h"
  7. #include "config.h"
  8. #include "linear_allocator.h"
  9. #include "world_types.h"
  10. namespace crown
  11. {
  12. class Filesystem;
  13. struct LuaEnvironment;
  14. class ResourceManager;
  15. struct ResourcePackage;
  16. class World;
  17. class WorldManager;
  18. struct Camera;
  19. /// @defgroup Device Device
  20. /// Holds data for a display mode.
  21. ///
  22. /// @ingroup Device
  23. struct DisplayMode
  24. {
  25. uint32_t id;
  26. uint16_t width;
  27. uint16_t height;
  28. };
  29. /// This is the place where to look for accessing all of
  30. /// the engine subsystems and related stuff.
  31. ///
  32. /// @ingroup Device
  33. struct Device
  34. {
  35. Device(Filesystem& fs, StringId64 boot_package, StringId64 boot_script);
  36. void init();
  37. /// Shutdowns the engine freeing all the allocated resources
  38. void shutdown();
  39. /// Returns a string identifying what platform the engine is running on.
  40. const char* platform() const { return CROWN_PLATFORM_NAME; }
  41. /// Returns a string identifying what architecture the engine is running on.
  42. const char* architecture() const { return CROWN_ARCH_NAME; }
  43. /// Returns a string identifying the engine version.
  44. const char* version() const { return CROWN_VERSION_MAJOR "." CROWN_VERSION_MINOR "." CROWN_VERSION_MICRO; }
  45. /// Returns wheter the engine is running (i.e. it is advancing
  46. /// the simulation).
  47. bool is_running() const;
  48. /// Return the number of frames rendered from the first
  49. /// call to Device::start()
  50. uint64_t frame_count() const;
  51. /// Returns the time in seconds needed to render the last frame
  52. float last_delta_time() const;
  53. /// Returns the time in seconds since the first call to start().
  54. double time_since_start() const;
  55. /// Quits the application.
  56. void quit();
  57. /// Pauses the engine
  58. void pause();
  59. /// Unpauses the engine
  60. void unpause();
  61. void update_resolution(uint16_t width, uint16_t height)
  62. {
  63. _width = width;
  64. _height = height;
  65. }
  66. /// Returns the main window resolution.
  67. void resolution(uint16_t& width, uint16_t& height)
  68. {
  69. width = _width;
  70. height = _height;
  71. }
  72. /// Updates all the subsystems
  73. void update();
  74. /// Renders the given @a world from the point of view of the given @a camera.
  75. void render_world(World* world, Camera* camera);
  76. /// Creates a new world.
  77. WorldId create_world();
  78. /// Destroys the given @a world.
  79. void destroy_world(WorldId world);
  80. /// Returns the resource package with the given @a package_name name.
  81. ResourcePackage* create_resource_package(const char* name);
  82. ResourcePackage* create_resource_package(StringId64 id);
  83. /// Destroy a previously created resource @a package.
  84. /// @note
  85. /// To unload the resources loaded by the package, you have to call
  86. /// ResourcePackage::unload() first.
  87. void destroy_resource_package(ResourcePackage* package);
  88. void reload(const char* type, const char* name);
  89. ResourceManager* resource_manager();
  90. LuaEnvironment* lua_environment();
  91. WorldManager* world_manager() { return _world_manager; }
  92. private:
  93. // Used to allocate all subsystems
  94. LinearAllocator _allocator;
  95. uint16_t _width;
  96. uint16_t _height;
  97. bool _is_init : 1;
  98. bool _is_running : 1;
  99. bool _is_paused : 1;
  100. uint64_t _frame_count;
  101. int64_t _last_time;
  102. int64_t _current_time;
  103. float _last_delta_time;
  104. double _time_since_start;
  105. Filesystem& _fs;
  106. StringId64 _boot_package_id;
  107. StringId64 _boot_script_id;
  108. ResourcePackage* _boot_package;
  109. LuaEnvironment* _lua_environment;
  110. ResourceManager* _resource_manager;
  111. WorldManager* _world_manager;
  112. private:
  113. // Disable copying
  114. Device(const Device&);
  115. Device& operator=(const Device&);
  116. };
  117. namespace device_globals
  118. {
  119. void init(Filesystem& fs, StringId64 boot_package, StringId64 boot_script);
  120. void shutdown();
  121. } // namespace device_globals
  122. Device* device();
  123. } // namespace crown