-
Notifications
You must be signed in to change notification settings - Fork 5
/
pixie.h
337 lines (267 loc) · 8.29 KB
/
pixie.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#pragma once
#include <assert.h>
#include <stdint.h>
#include "core.h"
namespace Pixie
{
enum MouseButton
{
MouseButton_Left = 0,
MouseButton_Middle = 1,
MouseButton_Right = 2,
MouseButton_Num
};
enum Key
{
Key_Backspace = 0,
Key_Tab,
Key_Enter,
Key_Escape,
Key_Up,
Key_Down,
Key_Left,
Key_Right,
Key_Home,
Key_End,
Key_PageUp,
Key_PageDown,
Key_Delete,
Key_Insert,
Key_LeftShift,
Key_RightShift,
Key_LeftControl,
Key_RightControl,
Key_LeftAlt,
Key_RightAlt,
Key_F1,
Key_F2,
Key_F3,
Key_F4,
Key_F5,
Key_F6,
Key_F7,
Key_F8,
Key_F9,
Key_F10,
Key_F11,
Key_F12,
Key_ASCII_Start = 32,
// 32 to 127 are ASCII printable characters.
// Note: these are unshifted keys.
Key_ASCII_End = 127,
Key_Num
};
enum
{
MaxPlatformKeys = 256
};
class Window
{
public:
Window();
~Window();
// Open the Pixie window with the specified title bar, width, and height.
// If scale is greater than 1 the window will be rendered scale times larger
// and the buffer will be stretched to fit.
bool Open(const TCHAR* title, int width, int height, bool fullscreen, bool maintainAspectRatio = false, int scale = 1);
// Close the Pixie window.
void Close();
// Update the Pixie window. This will copy the backing buffer to the actual window.
bool Update();
// Returns true in the frame the mouse button went down.
bool HasMouseGoneDown(MouseButton button) const;
// Returns true in the frame the mouse button went up.
bool HasMouseGoneUp(MouseButton button) const;
// Returns true if the mouse button is currently down.
bool IsMouseDown(MouseButton button) const;
// Returns true in the frame the key specified went down.
bool HasKeyGoneDown(Key key) const;
// Returns true if any key went down in the frame.
bool HasAnyKeyGoneDown() const;
// Returns true in the frame the key specified went up.
bool HasKeyGoneUp(Key key) const;
// Returns true if the key specified is currently down.
bool IsKeyDown(Key key) const;
// Returns true if any key is currently down.
bool IsAnyKeyDown() const;
// Returns an array of ASCII input for the current frame.
const char* GetInputCharacters() const;
// Clears the ASCII input for the current frame.
void ClearInputCharacters();
// Returns the current mouse X position.
int GetMouseX() const;
// Returns the current mouse Y position.
int GetMouseY() const;
// Returns the time delta in seconds since the last time the window was updated.
float GetDelta() const;
// Returns the time in seconds since the window was opened.
float GetTime() const;
// Returns the backing buffer for the window.
uint32_t* GetPixels() const;
// Returns the width of the window.
uint32_t GetWidth() const;
// Returns the height of the window.
uint32_t GetHeight() const;
// Returns the scale of the window.
uint32_t GetScale() const;
// Key callback handler. Called on any key state change.
typedef void(*KeyCallback)(Key key, bool down);
void SetKeyCallback(KeyCallback callback);
// Used by the window procedure to update key and mouse state.
void SetMouseButtonDown(MouseButton button, bool down);
void SetKeyDown(int key, bool down);
void AddInputCharacter(char c);
private:
void PlatformInit();
bool PlatformOpen(const TCHAR* title, int width, int height);
bool PlatformUpdate();
void PlatformClose();
void UpdateMouse();
void UpdateKeyboard();
int m_mouseX;
int m_mouseY;
bool m_lastMouseButtonDown[MouseButton_Num];
bool m_mouseButtonDown[MouseButton_Num];
int m_keyMap[Key_Num];
bool m_lastKeyDown[MaxPlatformKeys];
bool m_keyDown[MaxPlatformKeys];
bool m_anyKeyDown;
char m_inputCharacters[16+1];
float m_delta;
uint32_t* m_pixels;
uint32_t m_width;
uint32_t m_height;
uint32_t m_windowWidth;
uint32_t m_windowHeight;
int m_scale;
bool m_fullscreen;
bool m_maintainAspectRatio;
float m_scalex, m_scaley;
void* m_window;
float m_time;
int64_t m_lastTime;
int64_t m_freq;
KeyCallback m_keyCallback;
};
inline int Window::GetMouseX() const
{
return m_mouseX;
}
inline int Window::GetMouseY() const
{
return m_mouseY;
}
inline float Window::GetDelta() const
{
return m_delta;
}
inline float Window::GetTime() const
{
return m_time;
}
inline uint32_t* Window::GetPixels() const
{
return m_pixels;
}
inline uint32_t Window::GetWidth() const
{
return m_width;
}
inline uint32_t Window::GetHeight() const
{
return m_height;
}
inline uint32_t Window::GetScale() const
{
return m_scale;
}
inline bool Window::HasMouseGoneDown(MouseButton button) const
{
return !m_lastMouseButtonDown[button] && m_mouseButtonDown[button];
}
inline bool Window::HasMouseGoneUp(MouseButton button) const
{
return m_lastMouseButtonDown[button] && !m_mouseButtonDown[button];
}
inline bool Window::IsMouseDown(MouseButton button) const
{
return m_mouseButtonDown[button];
}
inline bool Window::HasAnyKeyGoneDown() const
{
for (int i = 0; i < Key_Num; i++)
{
if (HasKeyGoneDown((Key)i))
return true;
}
return false;
}
inline bool Window::HasKeyGoneDown(Key key) const
{
int index = m_keyMap[key];
if (index == -1)
return false;
assert(index >= 0 && index < MaxPlatformKeys);
return !m_lastKeyDown[index] && m_keyDown[index];
}
inline bool Window::HasKeyGoneUp(Key key) const
{
int index = m_keyMap[key];
if (index == -1)
return false;
assert(index >= 0 && index < MaxPlatformKeys);
return m_lastKeyDown[index] && !m_keyDown[index];
}
inline bool Window::IsKeyDown(Key key) const
{
int index = m_keyMap[key];
if (index == -1)
return false;
assert(index >= 0 && index < MaxPlatformKeys);
return m_keyDown[index];
}
inline bool Window::IsAnyKeyDown() const
{
for (int i = 0; i < Key_Num; i++)
{
if (IsKeyDown((Key)i))
return true;
}
return false;
}
inline const char* Window::GetInputCharacters() const
{
return m_inputCharacters;
}
inline void Window::ClearInputCharacters()
{
m_inputCharacters[0] = 0;
}
inline void Window::SetMouseButtonDown(MouseButton button, bool down)
{
m_mouseButtonDown[button] = down;
}
inline void Window::SetKeyDown(int platformKey, bool down)
{
assert(platformKey >= 0 && platformKey < MaxPlatformKeys);
if (m_keyDown[platformKey] == down)
return;
m_keyDown[platformKey] = down;
if (m_keyCallback)
{
for (int i = 0; i < Key_Num; i++)
{
int key = m_keyMap[i];
if (key == platformKey)
{
m_keyCallback((Key)i, down);
return;
}
}
}
}
inline void Window::SetKeyCallback(KeyCallback callback)
{
m_keyCallback = callback;
}
}