-
Notifications
You must be signed in to change notification settings - Fork 1
/
xx2d_engine_web.h
213 lines (184 loc) · 9.3 KB
/
xx2d_engine_web.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
#pragma once
#include <xx2d_base3.h>
namespace xx {
// Derived need inherit from gDesign
template<typename Derived>
struct Engine : EngineBase3 {
void EngineInit() {
// EM_BOOL OnKeyXXXXXXXXXXX(EmscriptenKeyboardEvent const& e) { return EM_TRUE; }
if constexpr (Has_OnKeyPress<Derived>) {
emscripten_set_keypress_callback("body", this, true, [](int, const EmscriptenKeyboardEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnKeyPress(*e);
});
}
if constexpr (Has_OnKeyDown<Derived>) {
emscripten_set_keydown_callback("body", this, true, [](int, const EmscriptenKeyboardEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnKeyDown(*e);
});
}
if constexpr (Has_OnKeyUp<Derived>) {
emscripten_set_keyup_callback("body", this, true, [](int, const EmscriptenKeyboardEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnKeyUp(*e);
});
}
// EM_BOOL OnMouseXXXXXXXXXXX(EmscriptenMouseEvent const& e) { return EM_TRUE; }
emscripten_set_mousedown_callback("canvas", this, true, [](int, const EmscriptenMouseEvent* e, void* ud)->EM_BOOL {
if constexpr (Has_OnMouseDown<Derived>) {
return ((Derived*)ud)->OnMouseDown(*e);
} else {
return ((Derived*)ud)->OnMouseDown_(*e);
}
});
emscripten_set_mouseup_callback("canvas", this, true, [](int, const EmscriptenMouseEvent* e, void* ud)->EM_BOOL {
if constexpr (Has_OnMouseUp<Derived>) {
return ((Derived*)ud)->OnMouseUp(*e);
} else {
return ((Derived*)ud)->OnMouseUp_(*e);
}
});
if constexpr (Has_OnClick<Derived>) {
emscripten_set_click_callback("canvas", this, true, [](int, const EmscriptenMouseEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnClick(*e);
});
}
if constexpr (Has_OnDblClick<Derived>) {
emscripten_set_dblclick_callback("canvas", this, true, [](int, const EmscriptenMouseEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnDblClick(*e);
});
}
emscripten_set_mousemove_callback("canvas", this, true, [](int, const EmscriptenMouseEvent* e, void* ud)->EM_BOOL {
if constexpr (Has_OnMouseMove<Derived>) {
return ((Derived*)ud)->OnMouseMove(*e);
} else {
return ((Derived*)ud)->OnMouseMove_(*e);
}
});
if constexpr (Has_OnMouseEnter<Derived>) {
emscripten_set_mouseenter_callback("canvas", this, true, [](int, const EmscriptenMouseEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnMouseEnter(*e);
});
}
if constexpr (Has_OnMouseLeave<Derived>) {
emscripten_set_mouseleave_callback("canvas", this, true, [](int, const EmscriptenMouseEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnMouseLeave(*e);
});
}
if constexpr (Has_OnMouseOver<Derived>) {
emscripten_set_mouseover_callback("canvas", this, true, [](int, const EmscriptenMouseEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnMouseOver(*e);
});
}
if constexpr (Has_OnMouseOut<Derived>) {
emscripten_set_mouseout_callback("canvas", this, true, [](int, const EmscriptenMouseEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnMouseOut(*e);
});
}
// EM_BOOL OnTouchXXXXXXXXXXX(EmscriptenTouchEvent const& e) { return EM_TRUE; }
if constexpr (Has_OnTouchStart<Derived>) {
emscripten_set_touchstart_callback("canvas", this, true, [](int, const EmscriptenTouchEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnTouchStart(*e);
});
}
if constexpr (Has_OnTouchMove<Derived>) {
emscripten_set_touchmove_callback("canvas", this, true, [](int, const EmscriptenTouchEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnTouchMove(*e);
});
}
if constexpr (Has_OnTouchEnd<Derived>) {
emscripten_set_touchend_callback("canvas", this, true, [](int, const EmscriptenTouchEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnTouchEnd(*e);
});
}
if constexpr (Has_OnTouchCancel<Derived>) {
emscripten_set_touchcancel_callback("canvas", this, true, [](int, const EmscriptenTouchEvent* e, void* ud)->EM_BOOL {
return ((Derived*)ud)->OnTouchCancel(*e);
});
}
// todo:
// emscripten_set_wheel_callback(target, userData, useCapture, callback)
// emscripten_set_resize_callback(target, userData, useCapture, callback)
// emscripten_set_scroll_callback(target, userData, useCapture, callback)
// emscripten_set_blur_callback(target, userData, useCapture, callback)
// emscripten_set_focus_callback(target, userData, useCapture, callback)
// emscripten_set_focusin_callback(target, userData, useCapture, callback)
// emscripten_set_focusout_callback(target, userData, useCapture, callback)
// emscripten_set_deviceorientation_callback(userData, useCapture, callback)
// emscripten_set_devicemotion_callback(userData, useCapture, callback)
// emscripten_set_orientationchange_callback(userData, useCapture, callback)
// emscripten_set_fullscreenchange_callback(target, userData, useCapture, callback)
// emscripten_set_pointerlockchange_callback(target, userData, useCapture, callback)
// emscripten_set_pointerlockerror_callback(target, userData, useCapture, callback)
// emscripten_set_visibilitychange_callback(userData, useCapture, callback)
// emscripten_set_gamepadconnected_callback(userData, useCapture, callback)
// emscripten_set_gamepaddisconnected_callback(userData, useCapture, callback)
// emscripten_set_batterychargingchange_callback(userData, callback)
// emscripten_set_batterylevelchange_callback(userData, callback)
// emscripten_set_beforeunload_callback(userData, callback)
if constexpr (Has_Init<Derived>) {
((Derived*)this)->Init();
}
framePerSeconds = ((Derived*)this)->fps;
this->SetWindowSize(((Derived*)this)->width, ((Derived*)this)->height);
mouseEventHandlers.Init(128, 128, (int)this->windowSize.x * 2, (int)this->windowSize.y * 2);
GLInit();
// todo: remap event handler
GLInit2();
if constexpr (Has_AfterInit<Derived>) {
((Derived*)this)->AfterInit();
}
tasks.Add([this]()->Task<> {
ctcDefault.Init();
co_return;
});
if constexpr (Has_MainTask<Derived>) {
tasks.Add(((Derived*)this)->MainTask());
}
if constexpr (Has_DrawTask<Derived>) {
drawTask = ((Derived*)this)->DrawTask();
}
}
/*
int main() {
gLooper.EngineInit();
emscripten_request_animation_frame_loop([](double ms, void*)->EM_BOOL {
return ???????????????.JsLoopCallback(ms);
}, nullptr);
}
*/
EM_BOOL JsLoopCallback(double ms) {
(void)ms;
auto s = NowSteadyEpochSeconds();
delta = s - nowSecs;
nowSecs = s;
GLUpdateBegin();
if (delta > ((Derived*)this)->maxFrameDelay) {
delta = ((Derived*)this)->maxFrameDelay;
}
if constexpr (Has_BeforeUpdate<Derived>) {
((Derived*)this)->BeforeUpdate();
}
timePool += delta;
while (timePool >= ((Derived*)this)->frameDelay) {
timePool -= ((Derived*)this)->frameDelay;
time += ((Derived*)this)->frameDelay;
++frameNumber;
fpsViewer.Update();
if constexpr (Has_Update<Derived>) {
((Derived*)this)->Update();
}
tasks();
}
if constexpr (Has_Draw<Derived>) {
((Derived*)this)->Draw();
}
if constexpr (Has_DrawTask<Derived>) {
drawTask();
}
if (showFps) {
fpsViewer.Draw(ctcDefault);
}
GLUpdateEnd();
return running;
}
};
}