NasNas
An intuitive and beginner friendly 2D game framework for C++
Camera.hpp
1 
6 #pragma once
7 
8 #include <memory>
9 #include <optional>
10 
11 #include <SFML/Graphics/RenderTexture.hpp>
12 #include <SFML/Graphics/Sprite.hpp>
13 #include <SFML/Graphics/Transformable.hpp>
14 #include <SFML/Graphics/View.hpp>
15 
16 #include <NasNas/core/data/Rect.hpp>
17 #include <NasNas/core/data/ShaderHolder.hpp>
18 
19 namespace ns {
20  class Scene;
21 
22  class Camera : public sf::View, public ShaderHolder {
23  friend class App;
24  public:
31  Camera(std::string name, int render_order);
32 
33  Camera(const Camera&) = delete;
34  Camera(Camera&&) = default;
35  Camera& operator=(const Camera&) = delete;
36  Camera& operator=(Camera&&) = default;
37 
46  void reset(int x, int y, int w, int h);
47 
54  void reset(const sf::Vector2i& position, const sf::Vector2i& size);
55 
61  void reset(const ns::IntRect& rect) ;
62 
78  void resetViewport(float x, float y, float w, float h);
79 
86  void resetViewport(const sf::Vector2f& position, const sf::Vector2f& size);
87 
93  void resetViewport(const sf::FloatRect& rect);
94 
95  auto getViewport() const -> const ns::FloatRect& ;
96 
102  auto hasScene() -> bool;
103 
109  void lookAt(Scene& scene);
110 
116  auto isVisible() const -> bool;
122  void setVisible(bool value);
123 
129  void follow(sf::Transformable& transformable);
130 
131  void unfollow();
132 
140  auto getRenderOrder() const -> int;
141 
147  auto getFramesDelay() const -> unsigned int;
148 
157  void setFramesDelay(unsigned int value);
158 
164  void setLimitsRect(const ns::IntRect& rectangle);
165 
171  auto getPosition() const -> sf::Vector2f;
172 
173  auto getFolloweePosition() const -> const sf::Vector2f&;
174 
180  auto getLeft() const -> float;
181 
187  void setLeft(float value);
188 
194  auto getRight() const -> float;
195 
201  void setRight(float value);
202 
208  auto getTop() const -> float;
209 
215  void setTop(float value);
216 
222  auto getBottom() const -> float;
223 
229  void setBottom(float value);
230 
236  auto getGlobalBounds() const -> ns::FloatRect;
237 
241  void update();
242 
243  private:
244  std::string m_name;
245  int m_render_order;
246  bool m_visible;
247  Scene* m_scene;
248 
249  std::optional<sf::Transformable*> m_followee;
250  unsigned int m_frames_delay;
251 
252  ns::IntRect m_base_view;
253  ns::FloatRect m_base_viewport;
254  ns::IntRect m_limits = {0, 0, 0, 0};
255 
256  std::unique_ptr<sf::RenderTexture> m_render_texture;
257  sf::Sprite m_sprite;
258 
263  void render(sf::RenderTarget& target);
264 
265  auto getSprite() const -> const sf::Sprite&;
266 
267  using sf::View::reset;
268  using sf::View::setViewport;
269 
270  };
271 
272 }
auto getRenderOrder() const -> int
Get render order of the Camera.
void setVisible(bool value)
Show or hide the Camera content.
void lookAt(Scene &scene)
Tell the Camera to look at a Scene.
Definition: App.hpp:39
auto getBottom() const -> float
Get Camera bottom position in world&#39;s coordinates.
Camera(std::string name, int render_order)
Construct a Camera object.
auto getRight() const -> float
Get Camera right position in world&#39;s coordinates.
void setLeft(float value)
Set Camera left position in world&#39;s coordinates.
void update()
Updates Camera position if following an entity.
void follow(sf::Transformable &transformable)
Follow an entity, Camera position will be updated accordingly automatically.
auto getFramesDelay() const -> unsigned int
auto getGlobalBounds() const -> ns::FloatRect
Get camera global bounds.
auto hasScene() -> bool
Is the Camera looking at a Scene ?
void setLimitsRect(const ns::IntRect &rectangle)
Sets Camera move limits.
auto getLeft() const -> float
Get Camera left position in world&#39;s coordinates.
void reset(int x, int y, int w, int h)
Reset Camera size and position.
auto getPosition() const -> sf::Vector2f
Get Camera position in world&#39;s coordinates.
void setFramesDelay(unsigned int value)
Set the frame delay.
void resetViewport(float x, float y, float w, float h)
Reset Camera viewport.
auto getTop() const -> float
Get Camera top position in world&#39;s coordinates.
void setBottom(float value)
Set Camera bottom position in world&#39;s coordinates.
auto isVisible() const -> bool
Is the Camera visible ?
void setTop(float value)
Set Camera top position in world&#39;s coordinates.
void setRight(float value)
Set Camera right position in world&#39;s coordinates.