-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
93 lines (79 loc) · 1.39 KB
/
Camera.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "Camera.h"
#include <cmath>
Camera::Camera()
{
camera.w = width;
camera.h = height;
camera.x = 0;
camera.y = 0;
x_offset = width / 2;
y_offset = height / 2;
}
Camera::Camera(int width, int height) {
this->width = width;
this->height = height;
camera.w = width;
camera.h = height;
camera.x = 0;
camera.y = 0;
x_offset = width / 2;
y_offset = height / 2;
}
void Camera::update()
{
float lerpX = std::lerp(camera.x, destX, .3);
float lerpY = std::lerp(camera.y, destY, .3);
if (std::abs(destX - lerpX) < .1) {
camera.x = destX;
}
else {
camera.x = lerpX;
}
if (std::abs(destY - lerpY) < .1) {
camera.y = destY;
}
else {
camera.y = lerpY;
}
}
void Camera::setDestination(int x, int y)
{
destX = x;
destY = y;
}
int Camera::get_x_offset() {
return camera.x - x_offset;
}
int Camera::get_y_offset() {
return camera.y - y_offset;
}
void Camera::setPos(int x, int y) {
camera.x = x;
camera.y = y;
}
bool Camera::in_camera_view(SDL_Rect* test)
{
SDL_Rect current_cam_rect;
current_cam_rect.x = get_x_offset();
current_cam_rect.y = get_y_offset();
current_cam_rect.w = width;
current_cam_rect.h = height;
if (SDL_HasIntersection(¤t_cam_rect, test)) {
return true;
}
return false;
}
int Camera::get_x() {
return camera.x;
}
int Camera::get_y() {
return camera.y;
}
int Camera::get_width()
{
return width;
}
int Camera::get_height()
{
return height;
}