-
Notifications
You must be signed in to change notification settings - Fork 5
/
inputsystem.h
90 lines (70 loc) · 1.62 KB
/
inputsystem.h
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
#pragma once
class Input {
public:
struct key_t {
bool down;
bool pressed;
int tick;
int oldtick;
};
std::array< key_t, 256 > m_keys;
vec2_t m_mouse;
vec2_t m_delta;
int m_scroll;
std::string m_buffer;
public:
__forceinline void updateMouse() {
POINT p;
if (!GetCursorPos(&p))
return;
ScreenToClient(g_csgo.m_window, &p);
m_delta = m_mouse - vec2_t(
static_cast<float>(p.x),
static_cast<float>(p.y));
m_mouse = {
static_cast<float>(p.x),
static_cast<float>(p.y) };
}
__forceinline void update() {
// iterate all keys.
for (int i{}; i <= 254; ++i) {
key_t* key = &m_keys[i];
key->pressed = false;
if (key->down && key->tick > key->oldtick) {
key->oldtick = key->tick;
key->pressed = true;
}
}
}
// mouse within coords.
__forceinline bool IsCursorInBounds(int x, int y, int x2, int y2) const {
return m_mouse.x > x && m_mouse.y > y && m_mouse.x < x2&& m_mouse.y < y2;
}
// mouse within rectangle.
__forceinline bool IsCursorInRect(Rect area) const {
return IsCursorInBounds(area.x, area.y, area.x + area.w, area.y + area.h);
}
__forceinline void SetDown(int vk) {
key_t* key = &m_keys[vk];
key->down = true;
key->tick = g_winapi.GetTickCount();
}
__forceinline void SetUp(int vk) {
key_t* key = &m_keys[vk];
key->down = false;
}
// key is being held.
__forceinline bool GetKeyState(int vk) {
if (vk == -1)
return false;
return m_keys[vk].down;
}
// key was pressed.
__forceinline bool GetKeyPress(int vk) {
if (vk == -1)
return false;
key_t* key = &m_keys[vk];
return key->pressed;
}
};
extern Input g_input;