]> git.lizzy.rs Git - dragonblocks3d.git/blob - src/dragonblocks/input_handler.cpp
Multithreading
[dragonblocks3d.git] / src / dragonblocks / input_handler.cpp
1 #include "camera.hpp" 
2 #include "input_handler.hpp" 
3 #include "window.hpp" 
4
5 using namespace std;
6 using namespace glm;
7 using namespace dragonblocks;
8
9 void InputHandler::processInput(double dtime)
10 {
11         processMouseInput(dtime);
12         processKeyInput(dtime);
13 }
14
15 void InputHandler::processMouseInput(double dtime)
16 {
17         vec2 cursor_delta = vec2(mouse_sensitivity * dtime) * (vec2)window->getCursorDelta();
18         onMouseMove(dtime, cursor_delta.x, cursor_delta.y);
19 }
20
21 void InputHandler::processKeyInput(double dtime)
22 {
23         set<int> keysDown;
24         for (auto it = listened_keys.begin(); it != listened_keys.end(); it++) {
25                 int key = *it;
26                 if (window->wasKeyDown(key)) {
27                         keysDown.insert(key);
28                 }
29         }
30         onKeyPress(dtime, keysDown);
31 }
32
33 void InputHandler::onMouseMove(double dtime, double x, double y)
34 {
35         yaw += x;
36         pitch -= y;
37         pitch = clamp(pitch, -89.0, 89.0);
38         camera->update(yaw, pitch);
39 }
40
41 void InputHandler::onKeyPress(double dtime, set<int> keys)
42 {
43         vec3 vel = vec3(speed * dtime);
44         vec3 front = camera->front(), right = camera->right(), up = camera->up();
45         if (! pitch_move) {
46                 front = normalize(vec3(front.x, 0, front.z));
47                 up = normalize(vec3(0, up.y, 0));
48         }
49         if (keys.find(GLFW_KEY_W) != keys.end()) {
50                 camera->pos += vel * front;
51         } else if (keys.find(GLFW_KEY_S) != keys.end()) {
52                 camera->pos -= vel * front;
53         }
54         if (keys.find(GLFW_KEY_D) != keys.end()) {
55                 camera->pos += vel * right;
56         } else if (keys.find(GLFW_KEY_A) != keys.end()) {
57                 camera->pos -= vel * right;
58         }
59         if (keys.find(GLFW_KEY_SPACE) != keys.end()) {
60                 camera->pos += vel * up;
61         } else if (keys.find(GLFW_KEY_LEFT_SHIFT) != keys.end()) {
62                 camera->pos -= vel * up;
63         }       
64 }
65
66 void InputHandler::listenFor(int key)
67 {
68         listened_keys.insert(key);
69 }
70
71 void InputHandler::dontListenFor(int key)
72 {
73         listened_keys.erase(key);
74 }
75
76 InputHandler::InputHandler(Camera *c, Window *w) : camera(c), window(w)
77 {
78         listenFor(GLFW_KEY_W);
79         listenFor(GLFW_KEY_A);
80         listenFor(GLFW_KEY_S);
81         listenFor(GLFW_KEY_D);
82         listenFor(GLFW_KEY_SPACE);
83         listenFor(GLFW_KEY_LEFT_SHIFT);
84 }
85