unit_render_cache.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include <QMatrix4x4>
  3. #include <cstdint>
  4. #include <string>
  5. #include <unordered_map>
  6. #include <vector>
  7. namespace Engine::Core {
  8. class Entity;
  9. class TransformComponent;
  10. class UnitComponent;
  11. class RenderableComponent;
  12. class MovementComponent;
  13. } // namespace Engine::Core
  14. namespace Render {
  15. struct CachedUnitData {
  16. std::uint32_t entity_id{0};
  17. Engine::Core::Entity *entity{nullptr};
  18. Engine::Core::TransformComponent *transform{nullptr};
  19. Engine::Core::UnitComponent *unit{nullptr};
  20. Engine::Core::RenderableComponent *renderable{nullptr};
  21. Engine::Core::MovementComponent *movement{nullptr};
  22. std::string renderer_key;
  23. QMatrix4x4 model_matrix;
  24. float distance_sq{0.0F};
  25. bool moving{false};
  26. bool in_frustum{true};
  27. bool fog_visible{true};
  28. bool has_attack{false};
  29. bool has_guard_mode{false};
  30. bool has_hold_mode{false};
  31. bool has_patrol{false};
  32. float last_pos_x{0.0F};
  33. float last_pos_y{0.0F};
  34. float last_pos_z{0.0F};
  35. float last_rot_x{0.0F};
  36. float last_rot_y{0.0F};
  37. float last_rot_z{0.0F};
  38. float last_scale_x{0.0F};
  39. float last_scale_y{0.0F};
  40. float last_scale_z{0.0F};
  41. bool model_matrix_valid{false};
  42. std::uint32_t last_seen_frame{0};
  43. };
  44. class UnitRenderCache {
  45. public:
  46. auto get_or_create(std::uint32_t entity_id, Engine::Core::Entity *entity,
  47. std::uint32_t frame) -> CachedUnitData &;
  48. void prune(std::uint32_t current_frame, std::uint32_t max_age = 120);
  49. void clear() { m_cache.clear(); }
  50. [[nodiscard]] auto size() const -> std::size_t { return m_cache.size(); }
  51. static auto update_model_matrix(CachedUnitData &data) -> bool;
  52. private:
  53. std::unordered_map<std::uint32_t, CachedUnitData> m_cache;
  54. };
  55. struct CachedModelMatrix {
  56. QMatrix4x4 matrix;
  57. float last_pos_x{0.0F};
  58. float last_pos_y{0.0F};
  59. float last_pos_z{0.0F};
  60. float last_rot_x{0.0F};
  61. float last_rot_y{0.0F};
  62. float last_rot_z{0.0F};
  63. float last_scale_x{0.0F};
  64. float last_scale_y{0.0F};
  65. float last_scale_z{0.0F};
  66. bool valid{false};
  67. std::uint32_t last_seen_frame{0};
  68. };
  69. class ModelMatrixCache {
  70. public:
  71. auto get_or_create(std::uint32_t entity_id,
  72. Engine::Core::TransformComponent *transform,
  73. std::uint32_t frame) -> const QMatrix4x4 &;
  74. void prune(std::uint32_t current_frame, std::uint32_t max_age = 120);
  75. void clear() { m_cache.clear(); }
  76. private:
  77. std::unordered_map<std::uint32_t, CachedModelMatrix> m_cache;
  78. };
  79. } // namespace Render