-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputHandler.cpp
More file actions
98 lines (85 loc) · 2.08 KB
/
InputHandler.cpp
File metadata and controls
98 lines (85 loc) · 2.08 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
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
94
95
96
97
98
#include <SDL2/SDL.h>
#include <iostream>
#include "InputHandler.h"
#include "../utils/Position.h"
using namespace std;
constexpr const SDL_Scancode InputHandler::numberScancodes[10] =
{
SDL_SCANCODE_0,
SDL_SCANCODE_1,
SDL_SCANCODE_2,
SDL_SCANCODE_3,
SDL_SCANCODE_4,
SDL_SCANCODE_5,
SDL_SCANCODE_6,
SDL_SCANCODE_7,
SDL_SCANCODE_8,
SDL_SCANCODE_9,
};
HandlerError *InputHandler::tick()
{
for (int keyID = 0; keyID < SDL_NUM_SCANCODES; ++keyID)
{
keysJustPressed[keyID] = (keysPressed[keyID] && !keysPressedLastFrame[keyID]);
keysPressedLastFrame[keyID] = keysPressed[keyID];
}
mouseButtonsPressed = SDL_GetMouseState(&mousePosition.first, &mousePosition.second);
leftClickLastFrame = leftClickThisFrame;
rightClickLastFrame = rightClickThisFrame;
leftClickThisFrame = false;
rightClickThisFrame = false;
if ((mouseButtonsPressed & SDL_BUTTON_LMASK) != 0)
{
leftClickThisFrame = true;
}
if ((mouseButtonsPressed & SDL_BUTTON_RMASK) != 0)
{
rightClickThisFrame = true;
}
return nullptr;
}
bool InputHandler::initalize()
{
leftClickLastFrame = false;
rightClickLastFrame = false;
keysPressed = (const bool *)SDL_GetKeyboardState(0);
return true;
}
const bool InputHandler::keyPressed(SDL_Scancode keyID)
{
return keysPressed[keyID];
}
const bool InputHandler::keyJustPressed(SDL_Scancode keyID)
{
return keysJustPressed[keyID];
}
const bool InputHandler::isLMBPress()
{
return leftClickThisFrame;
}
const bool InputHandler::isRMBPress()
{
return rightClickThisFrame;
}
const bool InputHandler::isLMBOneClick()
{
return (!leftClickLastFrame && leftClickThisFrame);
}
const bool InputHandler::isRMBOneClick()
{
return (!rightClickLastFrame && rightClickThisFrame);
}
const unsigned int InputHandler::getMouseX()
{
return mousePosition.first;
}
const unsigned int InputHandler::getMouseY()
{
return mousePosition.second;
}
InputHandler::InputHandler(): Handler(hp_OnInput)
{
debugName = "Input Handler";
}
InputHandler::~InputHandler()
{}