Chira Engine
A customizable MIT-licensed game engine.
Viewport.h
1 #pragma once
2 
3 #include <render/backend/RenderBackend.h>
4 #include "Scene.h"
5 
6 namespace chira {
7 
8 class Viewport {
9 public:
10  explicit Viewport(glm::vec2i size_, ColorRGB backgroundColor_ = {}, bool linearFiltering_ = true);
11 
12  Scene* addScene();
13 
14  Scene* addScene(const std::string& name);
15 
16  Scene* addScene(uuids::uuid uuid);
17 
18  Scene* addScene(uuids::uuid uuid, const std::string& name);
19 
20  [[nodiscard]] Scene* getScene(uuids::uuid sceneID);
21 
22  [[nodiscard]] const auto& getScenes() const {
23  return this->scenes;
24  }
25 
26  [[nodiscard]] bool hasScene(uuids::uuid sceneID);
27 
28  void removeScene(uuids::uuid sceneID);
29 
30  void removeAllScenes();
31 
32  void update();
33 
34  void render();
35 
36  [[nodiscard]] CameraComponent* getCamera() const {
37  for (const auto& [uuid, scene] : this->scenes) {
38  if (auto* camera = scene->getCamera()) {
39  return camera;
40  }
41  }
42  return nullptr;
43  }
44 
45  [[nodiscard]] ColorRGB getBackgroundColor() const {
46  return this->backgroundColor;
47  }
48 
49  void setBackgroundColor(ColorRGB color) {
50  this->backgroundColor = color;
51  }
52 
53  [[nodiscard]] glm::vec2i getSize() const {
54  return this->size;
55  }
56 
57  void setSize(glm::vec2i size_) {
58  this->size = size_;
59  this->recreateFrameBuffer();
60  }
61 
62  [[nodiscard]] bool isLinearFiltered() const {
63  return this->linearFiltering;
64  }
65 
66  void setLinearFiltering(bool enable) {
67  this->linearFiltering = enable;
68  this->recreateFrameBuffer();
69  }
70 
71  [[nodiscard]] Renderer::FrameBufferHandle* getRawHandle() {
72  return &this->frameBufferHandle;
73  }
74 
75 private:
76  void recreateFrameBuffer();
77 
78 private:
79  std::unordered_map<uuids::uuid, std::unique_ptr<Scene>> scenes;
80  Renderer::FrameBufferHandle frameBufferHandle;
81  glm::vec2i size;
82  ColorRGB backgroundColor;
83  bool linearFiltering;
84 };
85 
86 } // namespace chira