device.h 3.5 KB

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