BARE2D
Camera2D.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <glm/glm.hpp>
4 
5 namespace BARE2D {
6 
7  /**
8  * @class CameraState
9  * @brief Holds some basic information that the camera holds in a state.
10  */
11  struct CameraState {
12  glm::vec2 focus = glm::vec2(0.0f);
13  glm::vec2 scale = glm::vec2(1.0f);
14  };
15 
16  /**
17  * @class Camera2D
18  * @brief The basic camera for a 2D world. There are two spaces - the Worldspace and the Viewspace.
19  * The Worldspace is standard for all cameras, it is the 'gameworld' space.
20  * The Viewspace is the subspace of the worldspace. It is what the camera captures.
21  * Scale is zoom factor. NOT resolution scalar.
22  */
23  class Camera2D {
24  public:
25  Camera2D();
26  ~Camera2D();
27 
28  /**
29  * @brief Initializes the camera
30  * @param screenWidth The width of the screen or FBO the camera needs to transform things within. The 'visual field'
31  * @param screenHeight The height of the screen or FBO the camera needs to transform things within. The 'visual field'
32  */
33  void init(float screenWidth, float screenHeight);
34 
35  /**
36  * @brief Updates the matrix if necessary
37  */
38  void update();
39 
40  /**
41  * @brief Converts a coordinate from Worldspace to Viewspace
42  * @param pos A point in Viewspace
43  * @return The converted point in Worldspace.
44  */
45  glm::vec2 getWorldspaceCoord(glm::vec2 pos) const;
46 
47  /**
48  * @brief Converts a coordinate from Worldspace to Viewspace
49  * @param pos A point in Worldspace
50  * @return The converted point in Viewspace.
51  */
52  glm::vec2 getViewspaceCoord(glm::vec2 pos) const;
53 
54  /**
55  * @return The Worldspace size from a Viewspace size
56  */
57  glm::vec2 getWorldspaceSize(glm::vec2 size) const;
58 
59  /**
60  * @return The Viewspace size from a Worldspace size
61  */
62  glm::vec2 getViewspaceSize(glm::vec2 size) const;
63 
64  /**
65  * @return The Worldspace rectangle (dest/rect) from a Viewspace rectangle (dest/rect)
66  */
67  glm::vec4 getWorldspaceRect(glm::vec4 destRect) const;
68 
69  /**
70  * @return The Viewspace rectangle (dest/rect) from a Worldspace rectangle (dest/rect)
71  */
72  glm::vec4 getViewspaceRect(glm::vec4 destRect) const;
73 
74  /**
75  * @brief Sets the center of the camera to newFocus
76  * @param newFocus The position to set the center of the camera to
77  */
78  void setFocus(glm::vec2 newFocus);
79 
80  /**
81  * @brief Moves the center of the camera to focus + deltaPos
82  * @param deltaPos The change to move by
83  */
84  void offsetFocus(glm::vec2 deltaPos);
85 
86  /**
87  * @brief Sets the scale of the camera
88  * @param newScaleX The scale to set ot
89  * @param newScaleY The scale to set ot
90  */
91  void setScale(float newScaleX, float newScaleY);
92 
93  /**
94  * @brief Adds to the zoom factor of the camera.
95  * @param deltaScaleX The x direction
96  * @param deltaScaleY The y direction
97  */
98  void offsetScale(float deltaScaleX, float deltaScaleY);
99 
100  /**
101  * @return The POV size
102  */
103  glm::vec2 getViewspaceResolution() const;
104 
105  /**
106  * @return The camera focus
107  */
108  glm::vec2 getFocus() const;
109 
110  /**
111  * @return The camera's bottom-left position.
112  */
113  glm::vec2 getStatePosition() const;
114 
115  /**
116  * @return The zoom levels
117  */
118  glm::vec2 getScale() const;
119 
120  /**
121  * @return The camera matrix.
122  */
123  const glm::mat4& getCameraMatrix() const;
124 
125  bool isRectInViewspace(glm::vec4& destRect) const;
126 
127  /**
128  * @return The camera's current state.
129  */
131  /**
132  * @return The camera's last state before modification of the current state.
133  */
135 
136  private:
137  // The size of the POV.
138  glm::vec2 m_resolution;
139  // This holds the state of the camera - the position as well as scale.
141  // Holds the last state - only changes after being modified
143 
144  // Some matrices for math purposes.
146  // If we change something simple, we need to update the complicated stuff.
147  bool m_matrixNeedsUpdate = false;
148 
149  };
150 
151 }
BARE2D::Camera2D::init
void init(float screenWidth, float screenHeight)
Initializes the camera.
Definition: Camera2D.cpp:17
BARE2D
Definition: App.cpp:13
BARE2D::Camera2D::offsetScale
void offsetScale(float deltaScaleX, float deltaScaleY)
Adds to the zoom factor of the camera.
Definition: Camera2D.cpp:148
BARE2D::Camera2D::isRectInViewspace
bool isRectInViewspace(glm::vec4 &destRect) const
Definition: Camera2D.cpp:173
BARE2D::Camera2D::m_orthographicMatrix
glm::mat4 m_orthographicMatrix
Definition: Camera2D.hpp:145
BARE2D::Camera2D::getScale
glm::vec2 getScale() const
Definition: Camera2D.cpp:165
BARE2D::Camera2D::m_matrixNeedsUpdate
bool m_matrixNeedsUpdate
Definition: Camera2D.hpp:147
BARE2D::Camera2D::m_cameraMatrix
glm::mat4 m_cameraMatrix
Definition: Camera2D.hpp:145
BARE2D::Camera2D::getCameraMatrix
const glm::mat4 & getCameraMatrix() const
Definition: Camera2D.cpp:169
BARE2D::Camera2D::getViewspaceCoord
glm::vec2 getViewspaceCoord(glm::vec2 pos) const
Converts a coordinate from Worldspace to Viewspace.
Definition: Camera2D.cpp:74
BARE2D::Camera2D::~Camera2D
~Camera2D()
Definition: Camera2D.cpp:14
BARE2D::Camera2D::setFocus
void setFocus(glm::vec2 newFocus)
Sets the center of the camera to newFocus.
Definition: Camera2D.cpp:123
BARE2D::Camera2D::m_lastState
CameraState m_lastState
Definition: Camera2D.hpp:142
BARE2D::Camera2D::getWorldspaceCoord
glm::vec2 getWorldspaceCoord(glm::vec2 pos) const
Converts a coordinate from Worldspace to Viewspace.
Definition: Camera2D.cpp:62
BARE2D::Camera2D::Camera2D
Camera2D()
Definition: Camera2D.cpp:9
BARE2D::Camera2D::getStatePosition
glm::vec2 getStatePosition() const
Definition: Camera2D.cpp:185
BARE2D::Camera2D::getViewspaceSize
glm::vec2 getViewspaceSize(glm::vec2 size) const
Definition: Camera2D.cpp:95
BARE2D::CameraState
Holds some basic information that the camera holds in a state.
Definition: Camera2D.hpp:11
BARE2D::Camera2D::getViewspaceRect
glm::vec4 getViewspaceRect(glm::vec4 destRect) const
Definition: Camera2D.cpp:113
BARE2D::Camera2D::offsetFocus
void offsetFocus(glm::vec2 deltaPos)
Moves the center of the camera to focus + deltaPos.
Definition: Camera2D.cpp:131
BARE2D::Camera2D::update
void update()
Updates the matrix if necessary.
Definition: Camera2D.cpp:35
BARE2D::Camera2D::m_state
CameraState m_state
Definition: Camera2D.hpp:140
BARE2D::Camera2D::getWorldspaceRect
glm::vec4 getWorldspaceRect(glm::vec4 destRect) const
Definition: Camera2D.cpp:103
BARE2D::Camera2D::getViewspaceResolution
glm::vec2 getViewspaceResolution() const
Definition: Camera2D.cpp:157
BARE2D::Camera2D::getLastState
CameraState getLastState()
Definition: Camera2D.cpp:181
BARE2D::Camera2D
The basic camera for a 2D world. There are two spaces - the Worldspace and the Viewspace....
Definition: Camera2D.hpp:23
BARE2D::Camera2D::setScale
void setScale(float newScaleX, float newScaleY)
Sets the scale of the camera.
Definition: Camera2D.cpp:139
BARE2D::CameraState::focus
glm::vec2 focus
Definition: Camera2D.hpp:12
BARE2D::Camera2D::getState
CameraState getState()
Definition: Camera2D.cpp:177
BARE2D::CameraState::scale
glm::vec2 scale
Definition: Camera2D.hpp:13
BARE2D::Camera2D::m_resolution
glm::vec2 m_resolution
Definition: Camera2D.hpp:138
BARE2D::Camera2D::getFocus
glm::vec2 getFocus() const
Definition: Camera2D.cpp:161
BARE2D::Camera2D::getWorldspaceSize
glm::vec2 getWorldspaceSize(glm::vec2 size) const
Definition: Camera2D.cpp:87