-
Notifications
You must be signed in to change notification settings - Fork 0
/
emuGL.cpp
141 lines (118 loc) · 4.84 KB
/
emuGL.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdexcept>
#include <iostream>
#include <SDL2/SDL.h>
#include "emuGL.h"
#include "window.h"
namespace emuGL
{
void Shape::drawShape(SDL_Renderer* renderer) const
{
if (borderColor() != Colors::transparent)
{
for (uint i = 1; i < m_points.size(); i++)
SDL_RenderDrawLine(renderer, point(i - 1).x, point(i - 1).y, point(i).x, point(i).y);
}
}
void Shape::draw(const Window* win) const
{
// storing previous color
uint8_t r, g, b, a;
SDL_GetRenderDrawColor(win->m_renderer, &r, &g, &b, &a);
drawShape(win->m_renderer);
// restoring previoius color
SDL_SetRenderDrawColor(win->m_renderer, r, g, b, a);
}
const KeyInput* InputHandler::nextKeyInput()
{
SDL_Event sdlEvent;
bool haveAnEvent {};
do
{
haveAnEvent = SDL_PollEvent(&sdlEvent);
}
while (haveAnEvent && !isKeyboardEvent(&sdlEvent));
if (m_currKeyInput) delete m_currKeyInput;
if (haveAnEvent)
m_currKeyInput = keyInputFromSdlEvent(sdlEvent);
else
m_currKeyInput = nullptr;
return m_currKeyInput;
}
const KeyInput* InputHandler::waitForKeyPress()
{
SDL_Event pressedEvent;
SDL_Event upEvent;
while (
!SDL_PollEvent(&pressedEvent) ||
!(isKeyboardEvent(&pressedEvent) && pressedEvent.key.type == SDL_KEYDOWN)
);
KeyInput* pressedKeyInput = keyInputFromSdlEvent(pressedEvent);
while (
!SDL_PollEvent(&upEvent) ||
!(
isKeyboardEvent(&upEvent) && upEvent.key.type == SDL_KEYUP &&
keyInputFromSdlEvent(upEvent)->keyCode == pressedKeyInput->keyCode
)
);
if (m_currKeyInput) delete m_currKeyInput;
m_currKeyInput = keyInputFromSdlEvent(upEvent);
return m_currKeyInput;
}
KeyScanCode InputHandler::scanCodeFromSdlEvent(const SDL_Event& sdlEvent)
{
if (!isKeyboardEvent(&sdlEvent)) throw std::runtime_error("ERROR: Requested scan code for non keyboard event!");
switch (sdlEvent.key.keysym.scancode)
{
case SDL_SCANCODE_1: return KeyScanCode::k1;
case SDL_SCANCODE_2: return KeyScanCode::k2;
case SDL_SCANCODE_3: return KeyScanCode::k3;
case SDL_SCANCODE_4: return KeyScanCode::k4;
case SDL_SCANCODE_5: return KeyScanCode::k5;
case SDL_SCANCODE_6: return KeyScanCode::k6;
case SDL_SCANCODE_7: return KeyScanCode::k7;
case SDL_SCANCODE_8: return KeyScanCode::k8;
case SDL_SCANCODE_9: return KeyScanCode::k9;
case SDL_SCANCODE_0: return KeyScanCode::k0;
case SDL_SCANCODE_Q: return KeyScanCode::kQ;
case SDL_SCANCODE_W: return KeyScanCode::kW;
case SDL_SCANCODE_E: return KeyScanCode::kE;
case SDL_SCANCODE_R: return KeyScanCode::kR;
case SDL_SCANCODE_T: return KeyScanCode::kT;
case SDL_SCANCODE_Y: return KeyScanCode::kY;
case SDL_SCANCODE_U: return KeyScanCode::kU;
case SDL_SCANCODE_I: return KeyScanCode::kI;
case SDL_SCANCODE_O: return KeyScanCode::kO;
case SDL_SCANCODE_P: return KeyScanCode::kP;
case SDL_SCANCODE_A: return KeyScanCode::kA;
case SDL_SCANCODE_S: return KeyScanCode::kS;
case SDL_SCANCODE_D: return KeyScanCode::kD;
case SDL_SCANCODE_F: return KeyScanCode::kF;
case SDL_SCANCODE_G: return KeyScanCode::kG;
case SDL_SCANCODE_H: return KeyScanCode::kH;
case SDL_SCANCODE_J: return KeyScanCode::kJ;
case SDL_SCANCODE_K: return KeyScanCode::kK;
case SDL_SCANCODE_L: return KeyScanCode::kL;
case SDL_SCANCODE_Z: return KeyScanCode::kZ;
case SDL_SCANCODE_X: return KeyScanCode::kX;
case SDL_SCANCODE_C: return KeyScanCode::kC;
case SDL_SCANCODE_V: return KeyScanCode::kV;
case SDL_SCANCODE_B: return KeyScanCode::kB;
case SDL_SCANCODE_N: return KeyScanCode::kN;
case SDL_SCANCODE_M: return KeyScanCode::kM;
case SDL_SCANCODE_SPACE: return KeyScanCode::kSPACE;
case SDL_SCANCODE_RETURN:
case SDL_SCANCODE_KP_ENTER: return KeyScanCode::kENTER;
default:
std::cout << sdlEvent.key.keysym.sym << std::endl;
throw std::runtime_error("ERROR: Unimplemented key event occurred.");
break;
}
}
KeyInput* InputHandler::keyInputFromSdlEvent(const SDL_Event& sdlEvent)
{
return new KeyInput{
sdlEvent.key.type == SDL_KEYDOWN ? KeyEvent::Pressed : KeyEvent::Released,
scanCodeFromSdlEvent(sdlEvent)
};
}
}