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
|
#define AL_LOG_SECTION "glfm"
#include <al/log.h>
#include <nnwt/common.h>
#include "window_glfm.h"
#include "common.h"
#include "keys.h"
f64 stl_scale_scroll(f64 v, f64 factor)
{
return v / factor;
}
static struct stl_glfm_global global = { 0 };
void stl_set_output_discovered_callback(void (*output_discovered)(void *, struct stl_monitor *), void *userdata)
{
(void)output_discovered;
(void)userdata;
}
bool stl_global_init(bool skip_graphics)
{
(void)skip_graphics;
global.did_main = false;
return false;
}
s32 stl_global_get_poll_fd(void)
{
al_assert_and_return(-1);
}
void stl_global_process_events(void)
{
al_assert(false);
}
void stl_global_close(void)
{
}
static void onSurfaceCreated(GLFMDisplay *display, s32 width, s32 height)
{
(void)width;
(void)height;
if (!global.did_main) {
stl_platform_main(0, NULL);
global.did_main = true;
}
GLFMRenderingAPI api = glfmGetRenderingAPI(display);
log_info("Created GLES context of version %s.",
api == GLFMRenderingAPIOpenGLES32 ? "3.2" :
api == GLFMRenderingAPIOpenGLES31 ? "3.1" :
api == GLFMRenderingAPIOpenGLES3 ? "3.0" : "2.0");
}
static void onDraw(GLFMDisplay *display)
{
struct stl_window_glfm *glfm = (struct stl_window_glfm *)glfmGetUserData(display);
stl_window_send_render_event(&glfm->w);
}
static void onSurfaceDestroyed(GLFMDisplay *display)
{
struct stl_window_glfm *glfm = (struct stl_window_glfm *)glfmGetUserData(display);
stl_window_send_should_close_event(&glfm->w);
}
static void swap_dimensions_and_send_resize(struct stl_window_glfm *glfm)
{
SWAP(glfm->w.width, glfm->w.height);
stl_window_send_resize_event(&glfm->w, glfm->w.width, glfm->w.height);
}
static void onOrientationChanged(GLFMDisplay *display, GLFMInterfaceOrientation orientation)
{
struct stl_window_glfm *glfm = (struct stl_window_glfm *)glfmGetUserData(display);
if (orientation & (GLFMInterfaceOrientationPortrait | GLFMInterfaceOrientationPortraitUpsideDown)) {
if (!(global.orientation & (GLFMInterfaceOrientationPortrait | GLFMInterfaceOrientationPortraitUpsideDown))) {
swap_dimensions_and_send_resize(glfm);
}
} else if (orientation & (GLFMInterfaceOrientationLandscapeLeft | GLFMInterfaceOrientationLandscapeRight)) {
if (!(global.orientation & (GLFMInterfaceOrientationLandscapeLeft | GLFMInterfaceOrientationLandscapeRight))) {
swap_dimensions_and_send_resize(glfm);
}
}
global.orientation = orientation;
}
static u8 glfm_to_stela_button_action[] = {
[GLFMKeyActionPressed] = STELA_BUTTON_PRESSED,
[GLFMKeyActionReleased] = STELA_BUTTON_RELEASED
};
static bool onKey(GLFMDisplay *display, GLFMKeyCode key, GLFMKeyAction action, s32 modifiers)
{
struct stl_window_glfm *glfm = (struct stl_window_glfm *)glfmGetUserData(display);
(void)modifiers;
u8 mapped_action = glfm_to_stela_button_action[action];
switch (key) {
case GLFMKeyCodeVolumeUp:
return stl_window_send_key_immediate_event(&glfm->w, mapped_action, STELA_KEY_RIGHT);
case GLFMKeyCodeVolumeDown:
return stl_window_send_key_immediate_event(&glfm->w, mapped_action, STELA_KEY_LEFT);
case GLFMKeyCodeNavigationBack:
default:
break;
}
return true;
}
static u8 glfm_to_stela_touch[] = {
[GLFMTouchPhaseHover] = STELA_TOUCH_HOVER,
[GLFMTouchPhaseBegan] = STELA_TOUCH_BEGAN,
[GLFMTouchPhaseMoved] = STELA_TOUCH_MOVED,
[GLFMTouchPhaseEnded] = STELA_TOUCH_ENDED,
[GLFMTouchPhaseCancelled] = STELA_TOUCH_CANCELLED
};
static bool onTouch(GLFMDisplay *display, s32 touch, GLFMTouchPhase phase, f64 x, f64 y)
{
struct stl_window_glfm *glfm = (struct stl_window_glfm *)glfmGetUserData(display);
stl_window_send_touch_event(&glfm->w, touch, glfm_to_stela_touch[phase], x, y);
return true;
}
void glfmMain(GLFMDisplay *display)
{
nn_common_init("glfmMain");
stl_global_init(false);
global.display = display;
#define STELA_INCLUDE_DEPTH_BUFFER
glfmSetDisplayConfig(display,
GLFMRenderingAPIOpenGLES32,
GLFMColorFormatRGBA8888,
#ifdef STELA_INCLUDE_DEPTH_BUFFER
GLFMDepthFormat24,
GLFMStencilFormat8,
#else
GLFMDepthFormatNone,
GLFMStencilFormatNone,
#endif
GLFMMultisampleNone);
global.orientation = glfmGetInterfaceOrientation(display);
glfmSetDisplayChrome(display, GLFMUserInterfaceChromeNone);
glfmSetMultitouchEnabled(display, true);
glfmSetSurfaceCreatedFunc(display, onSurfaceCreated);
glfmSetRenderFunc(display, onDraw);
glfmSetSurfaceDestroyedFunc(display, onSurfaceDestroyed);
glfmSetOrientationChangedFunc(display, onOrientationChanged);
glfmSetKeyFunc(display, onKey);
glfmSetTouchFunc(display, onTouch);
}
static bool window_glfm_create_window(struct stl_window *window, u32 width, u32 height, u32 flags,
const char *monitor, const char *name, const char *app_id)
{
struct stl_window_glfm *glfm = (struct stl_window_glfm *)window;
(void)width;
(void)height;
(void)monitor;
(void)name;
(void)app_id;
glfmSetUserData(global.display, glfm);
s32 display_width, display_height;
glfmGetDisplaySize(global.display, &display_width, &display_height);
al_assert(display_width > 0 && display_height > 0);
stl_window_created(&glfm->w, (u32)display_width, (u32)display_height, flags);
#ifdef STELA_USE_EGL
glfm->w.egl_display = (EGLDisplay)glfmGetEglDisplay(global.display);
glfm->w.egl_context = (EGLContext)glfmGetEglContext(global.display);
#endif
return true;
}
static void window_glfm_resize_internal(struct stl_window *window, u32 width, u32 height)
{
(void)window;
(void)width;
(void)height;
}
static void window_glfm_resize(struct stl_window *window, u32 width, u32 height)
{
(void)window;
(void)width;
(void)height;
}
static void window_glfm_toggle_fullscreen(struct stl_window *window)
{
(void)window;
}
#ifdef STELA_POLL_INLINE
static bool window_glfm_poll(struct stl_window *window, bool block)
{
(void)window;
(void)block;
return true;
}
#ifdef STELA_PAUSE
static void window_glfm_wake(struct stl_window *window)
{
(void)window;
}
#endif
#endif
static void window_glfm_process_events(struct stl_window *window)
{
(void)window;
}
#ifdef STELA_PAUSE
static bool window_glfm_should_refresh(struct stl_window *window)
{
(void)window;
return true;
}
#endif
static void window_glfm_free(struct stl_window **window)
{
(void)window;
}
#if defined STELA_API_OPENGL
static bool window_glfm_gl_loader_load(void *window)
{
(void)window;
return true;
}
static bool window_glfm_gl_make_current(void *window)
{
(void)window;
return true;
}
static void window_glfm_gl_release_current(void *window)
{
(void)window;
}
static void window_glfm_gl_swap_buffers(void *window)
{
struct stl_window_glfm *glfm = (struct stl_window_glfm *)window;
(void)glfm;
glfmSwapBuffers(global.display);
}
#endif
struct stl_window *stl_window_create(void)
{
log_info("Wrapping GLFM window.");
struct stl_window_glfm *glfm = al_alloc_object(struct stl_window_glfm);
stl_window_init(&glfm->w);
glfm->w.fullscreen = false;
glfm->w.create_window = window_glfm_create_window;
glfm->w.resize_internal = window_glfm_resize_internal;
glfm->w.resize = window_glfm_resize;
glfm->w.toggle_fullscreen = window_glfm_toggle_fullscreen;
#ifdef STELA_POLL_INLINE
glfm->w.poll = window_glfm_poll;
#ifdef STELA_PAUSE
glfm->w.wake = window_glfm_wake;
#endif
#endif
glfm->w.process_events = window_glfm_process_events;
#ifdef STELA_PAUSE
glfm->w.should_refresh = window_glfm_should_refresh;
#endif
glfm->w.free = window_glfm_free;
#if defined STELA_API_OPENGL
glfm->w.get_gl_proc_address = glfmGetProcAddress;
glfm->w.gl_loader_load = window_glfm_gl_loader_load;
glfm->w.gl_make_current = window_glfm_gl_make_current;
glfm->w.gl_release_current = window_glfm_gl_release_current;
glfm->w.gl_swap_buffers = window_glfm_gl_swap_buffers;
#endif
return (struct stl_window *)glfm;
}
|