device.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #pragma once
  6. #include "config.h"
  7. #include "core/filesystem/types.h"
  8. #include "core/list.h"
  9. #include "core/memory/allocator.h"
  10. #include "core/memory/linear_allocator.h"
  11. #include "core/strings/string_id.h"
  12. #include "core/types.h"
  13. #include "device/boot_config.h"
  14. #include "device/console_server.h"
  15. #include "device/delta_time_filter.h"
  16. #include "device/device_options.h"
  17. #include "device/display.h"
  18. #include "device/input_types.h"
  19. #include "device/pipeline.h"
  20. #include "device/types.h"
  21. #include "device/window.h"
  22. #include "lua/types.h"
  23. #include "resource/types.h"
  24. #include "world/types.h"
  25. #include <atomic>
  26. /// @defgroup Device Device
  27. namespace crown
  28. {
  29. struct BgfxAllocator;
  30. struct BgfxCallback;
  31. /// This is the place where to look for accessing all of
  32. /// the engine subsystems and related stuff.
  33. ///
  34. /// @ingroup Device
  35. struct Device
  36. {
  37. LinearAllocator _allocator;
  38. const DeviceOptions &_options;
  39. BootConfig _boot_config;
  40. ConsoleServer *_console_server;
  41. Filesystem *_data_filesystem;
  42. ResourceLoader *_resource_loader;
  43. ResourceManager *_resource_manager;
  44. BgfxAllocator *_bgfx_allocator;
  45. BgfxCallback *_bgfx_callback;
  46. ShaderManager *_shader_manager;
  47. MaterialManager *_material_manager;
  48. InputManager *_input_manager;
  49. UnitManager *_unit_manager;
  50. LuaEnvironment *_lua_environment;
  51. Pipeline *_pipeline;
  52. Display *_display;
  53. Window *_window;
  54. ListNode _worlds;
  55. TimestepPolicy::Enum _timestep_policy;
  56. DeltaTimeFilter _delta_time_filter;
  57. u16 _width;
  58. u16 _height;
  59. u16 _prev_width;
  60. u16 _prev_height;
  61. s64 _last_time;
  62. bool _quit;
  63. bool _paused;
  64. std::atomic_int _needs_draw;
  65. ///
  66. bool process_events();
  67. ///
  68. Device(const DeviceOptions &opts, ConsoleServer &cs);
  69. ///
  70. Device(const Device &) = delete;
  71. ///
  72. Device &operator=(const Device &) = delete;
  73. /// Sets the timestep policy:
  74. /// * TimestepPolicy::VARIABLE: the timestep is the time it took for the previous frame to
  75. /// simulate. This is the default;
  76. /// * TimestepPolicy::SMOOTHED: the timestep is computed as an average of the previous delta
  77. /// times.
  78. void set_timestep_policy(TimestepPolicy::Enum policy);
  79. /// Sets the smoothed timestep parameters.
  80. /// @see DeltaTimeFilter::set_timestep_smoothing()
  81. void set_timestep_smoothing(u32 num_samples, u32 num_outliers, f32 average_cap);
  82. /// Simulate one frame.
  83. bool frame();
  84. /// Runs the engine.
  85. void run();
  86. /// Returns the number of command line parameters.
  87. int argc() const;
  88. /// Returns command line parameters.
  89. const char **argv() const;
  90. /// Quits the application.
  91. void quit();
  92. /// Pauses the engine.
  93. void pause();
  94. /// Unpauses the engine.
  95. void unpause();
  96. /// Returns the main window resolution.
  97. void resolution(u16 &width, u16 &height);
  98. /// Renders @a world using @a camera.
  99. void render(World &world, UnitId camera_unit);
  100. /// Creates a new world.
  101. World *create_world();
  102. /// Destroys the @a world.
  103. void destroy_world(World &world);
  104. /// Returns the resource package @a id.
  105. ResourcePackage *create_resource_package(StringId64 id);
  106. /// Destroys the resource package @a rp.
  107. /// @note
  108. /// Resources are not automatically unloaded.
  109. /// You have to call ResourcePackage::unload() before destroying a package.
  110. void destroy_resource_package(ResourcePackage &rp);
  111. /// Reloads all the resources listed in the @a json message.
  112. void refresh(const char *json);
  113. /// Captures a screenshot of the main window's backbuffer and saves it at @a path in PNG format.
  114. void screenshot(const char *path);
  115. };
  116. /// Runs the engine.
  117. void run(const DeviceOptions &opts);
  118. /// Returns the device.
  119. Device *device();
  120. } // namespace crown