-
Notifications
You must be signed in to change notification settings - Fork 1
/
Camera2D.cpp
69 lines (61 loc) · 1.77 KB
/
Camera2D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Created by lasagnaphil on 10/2/18.
//
#include "mat_utils.h"
#include "Camera2D.h"
#include "imgui.h"
#include "Shader.h"
#include "App.h"
Camera2D Camera2D::create(AppSettings* settings, vec2f pos) {
Camera2D cam;
cam.settings = settings;
cam.pos = pos;
return cam;
}
void Camera2D::addShader(Shader* shader) {
shaders.push(shader);
}
void Camera2D::update(float dt) {
auto inputMgr = InputManager::get();
if (inputMgr->isKeyPressed(SDL_SCANCODE_W)) {
pos.y += speed / zoom * dt;
}
else if (inputMgr->isKeyPressed(SDL_SCANCODE_S)) {
pos.y -= speed / zoom * dt;
}
if (inputMgr->isKeyPressed(SDL_SCANCODE_A)) {
pos.x -= speed / zoom * dt;
}
else if (inputMgr->isKeyPressed(SDL_SCANCODE_D)) {
pos.x += speed / zoom * dt;
}
if (inputMgr->isKeyEntered(SDL_SCANCODE_EQUALS)) {
zoom *= 2;
}
else if (inputMgr->isKeyEntered(SDL_SCANCODE_MINUS)) {
zoom /= 2;
}
FOREACH(shaders, shader, {
shader->use();
shader->setMatrix4("proj", getProjMatrix());
shader->setMatrix4("view", getViewMatrix());
})
}
mat4f Camera2D::getViewMatrix() const {
mat4f mat = aml::transMat(vec3f {-pos.x, -pos.y, 0});
mat = mat * aml::scaleMat(vec3f {zoom, zoom, 1});
return mat;
}
mat4f Camera2D::getProjMatrix() const {
float screenWidth = (float)settings->screenSize.x / pixelsPerMeter;
float screenHeight = (float)settings->screenSize.y / pixelsPerMeter;
return aml::ortho(-screenWidth/2, screenWidth/2,
-screenHeight/2, screenHeight/2,
-1.0f, 1.0f);
}
void Camera2D::drawUI() {
ImGui::Begin("Camera");
ImGui::Text("Position: %f %f", pos.x, pos.y);
ImGui::Text("Zoom: %f", zoom);
ImGui::End();
}