forked from brackeen/glfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglfm.h
369 lines (294 loc) · 12.7 KB
/
glfm.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
GLFM
https://github.com/brackeen/glfm
Copyright (c) 2014-2017 David Brackeen
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software. Permission is granted to anyone to use this software
for any purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be appreciated
but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef GLFM_H
#define GLFM_H
#define GLFM_VERSION_MAJOR 0
#define GLFM_VERSION_MINOR 9
#define GLFM_VERSION_REVISION 0
// One of these will be defined:
// GLFM_PLATFORM_IOS
// GLFM_PLATFORM_TVOS
// GLFM_PLATFORM_ANDROID
// GLFM_PLATFORM_EMSCRIPTEN
#if defined(__ANDROID__)
#define GLFM_PLATFORM_ANDROID
#elif defined(__EMSCRIPTEN__)
#define GLFM_PLATFORM_EMSCRIPTEN
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_IOS
#define GLFM_PLATFORM_IOS
#elif TARGET_OS_TV
#define GLFM_PLATFORM_TVOS
#else
#error Unknown Apple platform
#endif
#else
#error Unknown platform
#endif
// OpenGL ES includes
#if defined(GLFM_INCLUDE_ES32)
#if defined(GLFM_PLATFORM_IOS) || defined(GLFM_PLATFORM_TVOS)
#error No OpenGL ES 3.2 support in iOS
#elif defined(GLFM_PLATFORM_EMSCRIPTEN)
#error No OpenGL ES 3.2 support in WebGL
#else
#include <GLES3/gl32.h>
#include <GLES3/gl3ext.h>
#endif
#elif defined(GLFM_INCLUDE_ES31)
#if defined(GLFM_PLATFORM_IOS) || defined(GLFM_PLATFORM_TVOS)
#error No OpenGL ES 3.1 support in iOS
#elif defined(GLFM_PLATFORM_EMSCRIPTEN)
#error No OpenGL ES 3.1 support in WebGL
#else
#include <GLES3/gl31.h>
#include <GLES3/gl3ext.h>
#endif
#elif defined(GLFM_INCLUDE_ES3)
#if defined(GLFM_PLATFORM_IOS) || defined(GLFM_PLATFORM_TVOS)
#include <OpenGLES/ES3/gl.h>
#include <OpenGLES/ES3/glext.h>
#elif defined(GLFM_PLATFORM_EMSCRIPTEN)
#include <GLES3/gl3.h>
#include <GLES3/gl2ext.h>
#else
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#endif
#elif !defined(GLFM_INCLUDE_NONE)
#if defined(GLFM_PLATFORM_IOS) || defined(GLFM_PLATFORM_TVOS)
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#else
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#endif
#endif
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
// MARK: Enums
typedef enum {
GLFMRenderingAPIOpenGLES2,
GLFMRenderingAPIOpenGLES3,
GLFMRenderingAPIOpenGLES31,
GLFMRenderingAPIOpenGLES32,
} GLFMRenderingAPI;
typedef enum {
GLFMColorFormatRGBA8888,
GLFMColorFormatRGB565,
} GLFMColorFormat;
typedef enum {
GLFMDepthFormatNone,
GLFMDepthFormat16,
GLFMDepthFormat24,
} GLFMDepthFormat;
typedef enum {
GLFMStencilFormatNone,
GLFMStencilFormat8,
} GLFMStencilFormat;
typedef enum {
GLFMMultisampleNone,
GLFMMultisample4X,
} GLFMMultisample;
/// GLFMUserInterfaceChrome defines whether system UI chrome (status bar, navigation bar) is shown.
/// This value is ignored on Emscripten.
/// GLFMUserInterfaceChromeNavigation (default)
/// - Android: Show the navigation bar
/// - iOS: Show the home indicator on iPhone X
/// GLFMUserInterfaceChromeNavigationAndStatusBar:
/// - Android: Show the navigation bar and status bar
/// - iOS: Show status bar, and show the home indicator on iPhone X
/// GLFMUserInterfaceChromeFullscreen
/// - Android 2.3: Fullscreen
/// - Android 4.0 - 4.3: Navigation bar dimmed
/// - Android 4.4: Fullscreen immersive mode
/// - iOS: Fullscreen
typedef enum {
GLFMUserInterfaceChromeNavigation,
GLFMUserInterfaceChromeNavigationAndStatusBar,
GLFMUserInterfaceChromeFullscreen,
} GLFMUserInterfaceChrome;
typedef enum {
GLFMUserInterfaceOrientationAny,
GLFMUserInterfaceOrientationPortrait,
GLFMUserInterfaceOrientationLandscape,
} GLFMUserInterfaceOrientation;
typedef enum {
GLFMTouchPhaseHover,
GLFMTouchPhaseBegan,
GLFMTouchPhaseMoved,
GLFMTouchPhaseEnded,
GLFMTouchPhaseCancelled,
} GLFMTouchPhase;
typedef enum {
GLFMMouseCursorAuto,
GLFMMouseCursorNone,
GLFMMouseCursorDefault,
GLFMMouseCursorPointer,
GLFMMouseCursorCrosshair,
GLFMMouseCursorText
} GLFMMouseCursor;
typedef enum {
GLFMKeyBackspace = 0x08,
GLFMKeyTab = 0x09,
GLFMKeyEnter = 0x0d,
GLFMKeyEscape = 0x1b,
GLFMKeySpace = 0x20,
GLFMKeyLeft = 0x25,
GLFMKeyUp = 0x26,
GLFMKeyRight = 0x27,
GLFMKeyDown = 0x28,
GLFMKeyNavBack = 0x1000,
GLFMKeyNavMenu = 0x1001,
GLFMKeyNavSelect = 0x1002,
GLFMKeyPlayPause = 0x2000,
} GLFMKey;
typedef enum {
GLFMKeyModifierShift = (1 << 0),
GLFMKeyModifierCtrl = (1 << 1),
GLFMKeyModifierAlt = (1 << 2),
GLFMKeyModifierMeta = (1 << 3),
} GLFMKeyModifier;
typedef enum {
GLFMKeyActionPressed,
GLFMKeyActionRepeated,
GLFMKeyActionReleased,
} GLFMKeyAction;
// MARK: Structs and function pointers
typedef struct GLFMDisplay GLFMDisplay;
/// Function pointer returned from glfmGetProcAddress
typedef void (*GLFMProc)(void);
/// Main loop callback function. The frame time is in seconds, and is not related to wall time.
typedef void (*GLFMMainLoopFunc)(GLFMDisplay *display, double frameTime);
/// Callback function for mouse or touch events. The (x,y) values are in pixels.
/// The function should return true if the event was handled, and false otherwise.
typedef bool (*GLFMTouchFunc)(GLFMDisplay *display, int touch, GLFMTouchPhase phase,
double x, double y);
/// Callback function for key events.
/// The function should return true if the event was handled, and false otherwise.
typedef bool (*GLFMKeyFunc)(GLFMDisplay *display, GLFMKey keyCode, GLFMKeyAction action,
int modifiers);
/// Callback function for character input events.
typedef void (*GLFMCharFunc)(GLFMDisplay *display, const char *utf8, int modifiers);
/// Callback function for keyboard visibility, in pixels.
typedef void (*GLFMKeyboardVisibilityChangedFunc)(GLFMDisplay *display, bool visible,
double x, double y, double width, double height);
/// Callback when the surface could not be created.
typedef void (*GLFMSurfaceErrorFunc)(GLFMDisplay *display, const char *message);
/// Callback function when the OpenGL surface is created
typedef void (*GLFMSurfaceCreatedFunc)(GLFMDisplay *display, int width, int height);
/// Callback function when the OpenGL surface is resized (or rotated).
typedef void (*GLFMSurfaceResizedFunc)(GLFMDisplay *display, int width, int height);
/// Callback function when the OpenGL surface is destroyed.
typedef void (*GLFMSurfaceDestroyedFunc)(GLFMDisplay *display);
/// Callback function when the system recieves a low memory warning.
typedef void (*GLFMMemoryWarningFunc)(GLFMDisplay *display);
typedef void (*GLFMAppFocusFunc)(GLFMDisplay *display, bool focused);
// MARK: Functions
/// Main entry point for the app, where the display can be initialized and the GLFMMainLoopFunc
/// can be set.
extern void glfmMain(GLFMDisplay *display);
/// Init the display condifuration. Should only be called in glfmMain.
/// If the device does not support the preferred rendering API, the next available rendering API is
/// chosen (OpenGL ES 3.0 if OpenGL ES 3.1 is not available, and OpenGL ES 2.0 if OpenGL ES 3.0 is
/// not available). Call glfmGetRenderingAPI in the GLFMSurfaceCreatedFunc to see which rendering
/// API was chosen.
void glfmSetDisplayConfig(GLFMDisplay *display,
GLFMRenderingAPI preferredAPI,
GLFMColorFormat colorFormat,
GLFMDepthFormat depthFormat,
GLFMStencilFormat stencilFormat,
GLFMMultisample multisample);
void glfmSetUserData(GLFMDisplay *display, void *userData);
void *glfmGetUserData(GLFMDisplay *display);
/// Sets the allowed user interface orientations
void glfmSetUserInterfaceOrientation(GLFMDisplay *display,
GLFMUserInterfaceOrientation allowedOrientations);
/// Returns the allowed user interface orientations
GLFMUserInterfaceOrientation glfmGetUserInterfaceOrientation(GLFMDisplay *display);
/// Sets whether multitouch input is enabled. By default, multitouch is disabled.
void glfmSetMultitouchEnabled(GLFMDisplay *display, bool multitouchEnabled);
/// Gets whether multitouch input is enabled. By default, multitouch is disabled.
bool glfmGetMultitouchEnabled(GLFMDisplay *display);
/// Gets the display size, in pixels.
void glfmGetDisplaySize(GLFMDisplay *display, int *width, int *height);
/// Gets the display scale. On Apple devices, the value will be 1.0 for non-retina displays and 2.0
/// for retina.
double glfmGetDisplayScale(GLFMDisplay *display);
/// Gets the chrome insets, in pixels (AKA "safe area insets" in iOS). This is the space taken
/// on the outer edges of the display by status bars, navigation bars, and other UI elements.
void glfmGetDisplayChromeInsets(GLFMDisplay *display, double *top, double *right, double *bottom,
double *left);
/// Gets the user interface chrome.
GLFMUserInterfaceChrome glfmGetDisplayChrome(GLFMDisplay *display);
/// Sets the user interface chrome.
/// On Emscripten, to switch to fullscreen, this function must be called from an user-generated event handler.
void glfmSetDisplayChrome(GLFMDisplay *display, GLFMUserInterfaceChrome uiChrome);
/// Gets the rendering API of the display. The return value is not valid until the surface is
/// created. Defaults to GLFMRenderingAPIOpenGLES2.
GLFMRenderingAPI glfmGetRenderingAPI(GLFMDisplay *display);
/// Gets whether the display has touch capabilities.
bool glfmHasTouch(GLFMDisplay *display);
/// Sets the mouse cursor (only on platforms with a mouse)
void glfmSetMouseCursor(GLFMDisplay *display, GLFMMouseCursor mouseCursor);
/// Checks if a named OpenGL extension is supported
bool glfmExtensionSupported(const char *extension);
/// Gets the address of the specified function.
GLFMProc glfmGetProcAddress(const char *functionName);
/// Sets the function to call before each frame is displayed.
void glfmSetMainLoopFunc(GLFMDisplay *display, GLFMMainLoopFunc mainLoopFunc);
/// Sets the function to call when a mouse or touch event occurs.
void glfmSetTouchFunc(GLFMDisplay *display, GLFMTouchFunc touchFunc);
/// Sets the function to call when a key event occurs.
/// Note, on iOS, only pressed events are sent (no repeated or released events) and with no
/// modifiers.
void glfmSetKeyFunc(GLFMDisplay *display, GLFMKeyFunc keyFunc);
/// Sets the function to call when character input events occur.
void glfmSetCharFunc(GLFMDisplay *display, GLFMCharFunc charFunc);
/// Sets the function to call when the surface could not be created.
/// For example, the browser does not support WebGL.
void glfmSetSurfaceErrorFunc(GLFMDisplay *display, GLFMSurfaceErrorFunc surfaceErrorFunc);
/// Sets the function to call when the surface was created.
void glfmSetSurfaceCreatedFunc(GLFMDisplay *display, GLFMSurfaceCreatedFunc surfaceCreatedFunc);
/// Sets the function to call when the surface was resized (or rotated).
void glfmSetSurfaceResizedFunc(GLFMDisplay *display, GLFMSurfaceResizedFunc surfaceResizedFunc);
/// Sets the function to call when the surface was destroyed. For example, OpenGL context loss.
/// All OpenGL resources should be deleted in this call.
void glfmSetSurfaceDestroyedFunc(GLFMDisplay *display,
GLFMSurfaceDestroyedFunc surfaceDestroyedFunc);
void glfmSetMemoryWarningFunc(GLFMDisplay *display, GLFMMemoryWarningFunc lowMemoryFunc);
void glfmSetAppFocusFunc(GLFMDisplay *display, GLFMAppFocusFunc focusFunc);
/// Requests to show or hide the onscreen virtual keyboard. On Emscripten, this function does
/// nothing.
void glfmSetKeyboardVisible(GLFMDisplay *display, bool visible);
/// Returns true if the virtual keyboard is currently visible.
bool glfmIsKeyboardVisible(GLFMDisplay *display);
/// Sets the function to call when the virtual keyboard changes visibility or changes bounds.
void glfmSetKeyboardVisibilityChangedFunc(GLFMDisplay *display,
GLFMKeyboardVisibilityChangedFunc visibilityChangedFunc);
#if defined(GLFM_PLATFORM_ANDROID)
#include <android/native_activity.h>
ANativeActivity *glfmAndroidGetActivity(void);
#endif // GLFM_PLATFORM_ANDROID
#ifdef __cplusplus
}
#endif
#endif