9 _projectionMatrix(1.0f),
23 void init()
override {
25 _projectionMatrix = glm::ortho(0.0f, (
float)_screenWidth, (
float)_screenHeight, 0.0f);
28 void update()
override {
32 _cameraMatrix = glm::mat4(1.0f);
34 glm::vec3 scale(_scale, _scale, 1.0f);
35 _cameraMatrix = glm::scale(_cameraMatrix, scale);
38 glm::vec3 translate(-_position.x, -_position.y, 0.0f);
39 _cameraMatrix = glm::translate(_cameraMatrix, translate);
41 _cameraMatrix = _projectionMatrix * _viewMatrix * _cameraMatrix;
44 _cameraChange =
false;
48 glm::vec2 convertScreenToWorld(glm::vec2 screenCoords)
const override {
50 screenCoords -= glm::vec2(_screenWidth / 2, _screenHeight / 2);
52 screenCoords /= _scale;
53 screenCoords += glm::vec2(_screenWidth / 2, _screenHeight / 2);
55 screenCoords.x += _position.x;
56 screenCoords.y += _position.y;
63 void setPosition(
const glm::vec3 newPosition)
override {
64 _position = newPosition;
68 void setPosition_X(
const float newPosition)
override {
69 _position.x = newPosition;
73 void setPosition_Y(
const float newPosition)
override {
74 _position.y = newPosition;
78 void setPosition_Z(
const float newPosition)
override {
79 _position.z = newPosition;
83 void setScale(
float newScale)
override {
89 glm::vec3 getPosition()
const override {
93 float getScale()
const override {
97 glm::mat4 getCameraMatrix()
const override {
101 glm::ivec2 getCameraDimensions()
const override {
102 glm::vec2 cameraDimensions = { _screenWidth, _screenHeight };
103 return cameraDimensions;
106 SDL_FRect getCameraRect()
const override {
107 float cameraWidth = getCameraDimensions().x / getScale();
108 float cameraHeight = getCameraDimensions().y / getScale();
110 float cameraX = _position.x - cameraWidth / 2.0f + getCameraDimensions().x / 2;
111 float cameraY = _position.y - cameraHeight / 2.0f + getCameraDimensions().y / 2;
113 SDL_FRect cameraRect = { cameraX , cameraY , cameraWidth, cameraHeight };
117 void setCameraMatrix(glm::mat4 newMatrix) {
118 _cameraChange =
true;
121 bool isPointInCameraView(
const glm::vec4 point,
float margin)
123 glm::mat4 vpMatrix = _cameraMatrix;
125 glm::vec4 clipSpacePos = vpMatrix * point;
127 if (clipSpacePos.w != 0.0f) {
128 clipSpacePos.x /= clipSpacePos.w;
129 clipSpacePos.y /= clipSpacePos.w;
130 clipSpacePos.z /= clipSpacePos.w;
134 if (clipSpacePos.x < -1.0f - margin || clipSpacePos.x > 1.0f + margin)
return false;
135 if (clipSpacePos.y < -1.0f - margin || clipSpacePos.y > 1.0f + margin)
return false;
136 if (clipSpacePos.z < -margin || clipSpacePos.z > 1.0f + margin)
return false;
141 bool hasChanged()
override {
142 return _cameraChange;
145 void makeCameraDirty()
override {
146 _cameraChange =
true;
149 void refreshCamera()
override {
150 _cameraChange =
false;
154 int _screenWidth, _screenHeight;
159 glm::mat4 _projectionMatrix;
160 glm::mat4 _viewMatrix;
161 glm::mat4 _cameraMatrix;