-
Notifications
You must be signed in to change notification settings - Fork 0
/
emuGL.h
159 lines (118 loc) · 3.78 KB
/
emuGL.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef EMUGL_H
#define EMUGL_H
#include <string>
#include <vector>
#include <stdexcept>
#include <SDL2/SDL.h>
#include "color.h"
namespace emuGL
{
struct Point
{
Point() : x{}, y{} {}
Point(int x, int y) : x(x), y(y) {}
Point(const Point& other) : x(other.x), y(other.y) {}
Point operator=(const Point& other) { return {other}; }
int x;
int y;
};
class Window;
class Shape
{
friend class Window;
public:
void add(Point p) { m_points.push_back(p); }
void setBorderColor(const Color& color) { m_borderColor = color; }
const Color& borderColor() const { return m_borderColor; }
void setFillColor(const Color& color) { m_fillColor = color; }
const Color& fillColor() const { return m_fillColor; }
Shape(const Shape&) = delete;
Shape operator=(const Shape&) = delete;
Point point(int i) const { return m_points.at(i); }
protected:
Shape() {};
Shape(std::initializer_list<Point> points) { for (const Point& p : points) add(p); }
virtual void drawShape(SDL_Renderer* renderer) const;
private:
std::vector<Point> m_points;
Color m_borderColor {Colors::black};
Color m_fillColor {Colors::transparent};
void draw(const Window* win) const;
};
class Rect : public Shape
{
public:
Rect(int width, int height, Point pos)
: m_w(width),
m_h(height),
Shape{pos}
{ m_rect = new SDL_Rect{point(0).x, point(0).y, m_w, m_h}; }
~Rect() { delete m_rect; }
protected:
void drawShape(SDL_Renderer* renderer) const
{
if (borderColor() != Colors::transparent)
{
SDL_SetRenderDrawColor(renderer,
borderColor().red(), borderColor().green(),
borderColor().blue(), borderColor().alpha());
SDL_RenderDrawRect(renderer, m_rect);
}
if (fillColor() != Colors::transparent)
{
SDL_SetRenderDrawColor(renderer,
fillColor().red(), fillColor().green(),
fillColor().blue(), fillColor().alpha());
SDL_RenderFillRect(renderer, m_rect);
}
}
private:
int m_w;
int m_h;
SDL_Rect* m_rect {};
};
enum class KeyScanCode
{
k0, k1, k2, k3, k4, k5, k6, k7, k8, k9, // top 1-9, 0 keys
kQ, kW, kE, kR, kT, kY, kU, kI, kO, kP,
kA, kS, kD, kF, kG, kH, kJ, kK, kL,
kZ, kX, kC, kV, kB, kN, kM,
kSPACE,
kENTER
};
enum class KeyEvent { Pressed, Released };
struct KeyInput
{
KeyEvent type;
KeyScanCode keyCode;
};
class InputHandler
{
public:
bool quitIntent() const
{
SDL_PumpEvents();
return SDL_HasEvent(SDL_QUIT) == SDL_TRUE;
}
/**
* @returns nullptr if no key input in queue, a ptr to KeyInput type otherwise
*/
const KeyInput* nextKeyInput(); // TODO: change ptr return to object return
const KeyInput* waitForKeyPress(); // TODO: change ptr return to object return
~InputHandler()
{
// delete m_sdlEvent;
delete m_currKeyInput;
}
private:
// SDL_Event m_sdlEvent;
KeyInput* m_currKeyInput {};
bool isKeyboardEvent(const SDL_Event* event)
{
return SDL_KEYDOWN == event->type || event->type == SDL_KEYUP;
}
KeyScanCode scanCodeFromSdlEvent(const SDL_Event& sdlEvent);
KeyInput* keyInputFromSdlEvent(const SDL_Event& sdlEvent);
};
}
#endif /* EMUGL_H */