device.h 3.6 KB

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