-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfml_header.h
262 lines (231 loc) · 9.83 KB
/
sfml_header.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
#pragma once
#include "utils.h"
/*
* TODO: write a custom SFML wrapper. When we port this to other platforms (e.g. Javascript),
* we use different API's for handling system events, so we don't want to completely rewrite parts of code that call SFML directly scattered throughout.
*/
#ifndef HAS_SFML
#include <emscripten/html5.h>
#include <emscripten.h>
#include <cstring>
#include <iostream>
inline void send_main_initialized_signal() {
emscripten_run_script("const event = new Event('main_initialized');window.dispatchEvent(event);");
}
namespace sf::Keyboard {
enum Key {
Unknown = -1, //!< Unhandled key
A = 0, //!< The A key
B, //!< The B key
C, //!< The C key
D, //!< The D key
E, //!< The E key
F, //!< The F key
G, //!< The G key
H, //!< The H key
I, //!< The I key
J, //!< The J key
K, //!< The K key
L, //!< The L key
M, //!< The M key
N, //!< The N key
O, //!< The O key
P, //!< The P key
Q, //!< The Q key
R, //!< The R key
S, //!< The S key
T, //!< The T key
U, //!< The U key
V, //!< The V key
W, //!< The W key
X, //!< The X key
Y, //!< The Y key
Z, //!< The Z key
Num0, //!< The 0 key
Num1, //!< The 1 key
Num2, //!< The 2 key
Num3, //!< The 3 key
Num4, //!< The 4 key
Num5, //!< The 5 key
Num6, //!< The 6 key
Num7, //!< The 7 key
Num8, //!< The 8 key
Num9, //!< The 9 key
Escape, //!< The Escape key
LControl, //!< The left Control key
LShift, //!< The left Shift key
LAlt, //!< The left Alt key
LSystem, //!< The left OS specific key: window (Windows and Linux), apple (MacOS X), ...
RControl, //!< The right Control key
RShift, //!< The right Shift key
RAlt, //!< The right Alt key
RSystem, //!< The right OS specific key: window (Windows and Linux), apple (MacOS X), ...
Menu, //!< The Menu key
LBracket, //!< The [ key
RBracket, //!< The ] key
Semicolon, //!< The ; key
Comma, //!< The , key
Period, //!< The . key
Quote, //!< The ' key
Slash, //!< The / key
Backslash, //!< The \ key
Tilde, //!< The ~ key
Equal, //!< The = key
Hyphen, //!< The - key (hyphen)
Space, //!< The Space key
Enter, //!< The Enter/Return keys
Backspace, //!< The Backspace key
Tab, //!< The Tabulation key
PageUp, //!< The Page up key
PageDown, //!< The Page down key
End, //!< The End key
Home, //!< The Home key
Insert, //!< The Insert key
Delete, //!< The Delete key
Add, //!< The + key
Subtract, //!< The - key (minus, usually from numpad)
Multiply, //!< The * key
Divide, //!< The / key
Left, //!< Left arrow
Right, //!< Right arrow
Up, //!< Up arrow
Down, //!< Down arrow
Numpad0, //!< The numpad 0 key
Numpad1, //!< The numpad 1 key
Numpad2, //!< The numpad 2 key
Numpad3, //!< The numpad 3 key
Numpad4, //!< The numpad 4 key
Numpad5, //!< The numpad 5 key
Numpad6, //!< The numpad 6 key
Numpad7, //!< The numpad 7 key
Numpad8, //!< The numpad 8 key
Numpad9, //!< The numpad 9 key
F1, //!< The F1 key
F2, //!< The F2 key
F3, //!< The F3 key
F4, //!< The F4 key
F5, //!< The F5 key
F6, //!< The F6 key
F7, //!< The F7 key
F8, //!< The F8 key
F9, //!< The F9 key
F10, //!< The F10 key
F11, //!< The F11 key
F12, //!< The F12 key
F13, //!< The F13 key
F14, //!< The F14 key
F15, //!< The F15 key
Pause, //!< The Pause key
KeyCount, //!< Keep last -- the total number of keyboard keys
// Deprecated values:
Dash = Hyphen, //!< \deprecated Use Hyphen instead
BackSpace = Backspace, //!< \deprecated Use Backspace instead
BackSlash = Backslash, //!< \deprecated Use Backslash instead
SemiColon = Semicolon, //!< \deprecated Use Semicolon instead
Return = Enter //!< \deprecated Use Enter instead
};
inline bool pressed[KeyCount] = {0};
inline bool isKeyPressed(Key key) {
return pressed[key];
}
inline EM_BOOL key_callback(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData) {
std::cout << "key: " << keyEvent->key << std::endl;
bool to_set;
if (eventType == EMSCRIPTEN_EVENT_KEYDOWN) {
to_set = true;
} else if (eventType == EMSCRIPTEN_EVENT_KEYUP) {
to_set = false;
}
if (eventType == EMSCRIPTEN_EVENT_KEYDOWN || eventType == EMSCRIPTEN_EVENT_KEYUP) {
if (!strcmp(keyEvent->key, "a")) pressed[A] = to_set;
if (!strcmp(keyEvent->key, "s")) pressed[S] = to_set;
if (!strcmp(keyEvent->key, "d")) pressed[D] = to_set;
if (!strcmp(keyEvent->key, "w")) pressed[W] = to_set;
if (!strcmp(keyEvent->key, "ArrowLeft")) pressed[Left] = to_set;
if (!strcmp(keyEvent->key, "ArrowRight")) pressed[Right] = to_set;
if (!strcmp(keyEvent->key, "ArrowDown")) pressed[Down] = to_set;
if (!strcmp(keyEvent->key, "ArrowUp")) pressed[Up] = to_set;
if (!strcmp(keyEvent->key, "ArrowUp")) pressed[Up] = to_set;
if (!strcmp(keyEvent->key, "PageUp")) pressed[PageUp] = to_set;
if (!strcmp(keyEvent->key, "PageDown")) pressed[PageDown] = to_set;
if (!strcmp(keyEvent->key, " ")) pressed[Space] = to_set;
if (!strcmp(keyEvent->key, "q")) pressed[Q] = to_set;
}
return false;
};
inline bool currently_dragging = false;
inline long movementX = 0, movementY = 0;
inline EM_BOOL mouse_callback(int eventType, const EmscriptenMouseEvent *event, void *userData) {
if (eventType == EMSCRIPTEN_EVENT_MOUSEDOWN) {
currently_dragging = true;
} else if (eventType == EMSCRIPTEN_EVENT_MOUSEUP) {
currently_dragging = false;
}
if (currently_dragging) {
movementX = event->movementX;
movementY = event->movementY;
// std::cout << "Mouse movement: " << movementX << " " << movementY << std::endl;
}
return false;
};
inline bool Dragged() {
return sf::Keyboard::currently_dragging;
}
}
inline void setup_callbacks() {
std::cout << "registering callbacks\n";
emscripten_set_keyup_callback("canvas", nullptr, false, sf::Keyboard::key_callback);
emscripten_set_keydown_callback("canvas", nullptr, false, sf::Keyboard::key_callback);
emscripten_set_mousedown_callback("canvas", nullptr, false, sf::Keyboard::mouse_callback);
emscripten_set_mouseup_callback("canvas", nullptr, false, sf::Keyboard::mouse_callback);
emscripten_set_mousemove_callback("canvas", nullptr, false, sf::Keyboard::mouse_callback);
}
inline void poll_input(Image &image, gmtl::Matrix44f &screen) {
// do nothing.
}
#endif
#ifdef HAS_SFML
#include <SFML/Graphics.hpp>
inline int window_width = 1000, window_height = 800;
inline void setup_callbacks() {
}
inline sf::RenderWindow& get_window() {
static sf::RenderWindow window (sf::VideoMode(window_width, window_height), "My window");
return window;
};
inline void poll_input(Image& image, gmtl::Matrix44f& screen) {
// TODO: refactor screen matrix to be part of the image class.
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
sf::RenderWindow& window = get_window();
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
{
window.close();
}
else if (event.type == sf::Event::KeyPressed)
{
// if (event.key.code == sf::Keyboard::Num1 && image.width < 2000)
// {
// // Increase the size of the image. This makes the picture clearer.
// // We limit width to around 2000 pixels because at that level too much memoy is used.
//
// // Clears the current image data and resizes it to specific width and height.
// image.resize(image.width * 1.2, image.height * 1.2);
//
// // Since we changed the width/height, the screen matrix (mapping normalized device coordinates to
// // pixels need to be changed as well.
// screen = create_screen_matrix(image.width, image.height);
// }
// else if (event.key.code == sf::Keyboard::Tilde && image.height > 10 && image.width > 10)
// {
// // Decrease size of the image
// image.resize(image.width / 1.2, image.height / 1.2);
// screen = create_screen_matrix(image.width, image.height);
// }
}
}
}
#endif