-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.c
More file actions
52 lines (42 loc) · 1.7 KB
/
Camera.c
File metadata and controls
52 lines (42 loc) · 1.7 KB
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
#include "Camera.h"
#include "debugmalloc.h"
static const float minZoom = 0.1f;
static const float maxZoom = 1;
void camera_zoom(Camera *camera, float zoomLevels, Point zoomPos) {
float multiplier = powf(0.9f, -zoomLevels);
if (camera->zoom * multiplier < minZoom)
multiplier = minZoom / camera->zoom;
else if (camera->zoom * multiplier > maxZoom)
multiplier = maxZoom / camera->zoom;
camera->zoom *= multiplier;
camera->position.x -= (zoomPos.x - zoomPos.x * multiplier) / camera->zoom;
camera->position.y -= (zoomPos.y - zoomPos.y * multiplier) / camera->zoom;
}
void camera_move(Camera *camera, Vec movement) {
camera->position.x -= movement.x / camera->zoom;
camera->position.y -= movement.y / camera->zoom;
}
Point camera_screen_to_view(Camera *camera, Point p) {
return (Point){(p.x) / camera->zoom + camera->position.x,
(p.y) / camera->zoom + camera->position.y};
}
void camera_update(Camera *camera, Input *input, SDL_Renderer *renderer) {
if (input_get_mouse_button(input, SDL_BUTTON_MIDDLE).isHeld)
camera_move(camera, (Vec) {(float) input_get_mouse_delta_x(input),
(float) input_get_mouse_delta_y(input)});
SDL_Cursor *cursor = SDL_GetCursor();
if (input_get_mouse_button(input, SDL_BUTTON_MIDDLE).isPressed) {
cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
SDL_SetCursor(cursor);
SDL_FreeCursor(cursor);
SDL_CaptureMouse(true);
}
if (input_get_mouse_button(input, SDL_BUTTON_MIDDLE).isReleased) {
cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
SDL_SetCursor(cursor);
SDL_FreeCursor(cursor);
SDL_CaptureMouse(false);
}
camera_zoom(camera, (float) input_get_mouse_wheel_y(input),
input_get_mouse_pos(input));
}