forked from brackeen/glfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pattern.c
195 lines (171 loc) · 6.88 KB
/
test_pattern.c
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
// Draws a test pattern to check if framebuffer is scaled correctly.
// Tap to modify interface chrome (navigation bar, status bar, etc)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "glfm.h"
#include "test_pattern_renderer.h"
#include "file_compat.h"
#define FILE_COMPAT_ANDROID_ACTIVITY glfmGetAndroidActivity(display)
typedef struct {
Renderer *renderer;
Texture texture;
bool textureNeedsUpdate;
bool needsRedraw;
} TestPatternApp;
static Texture createTestPatternTexture(GLFMDisplay *display, uint32_t width, uint32_t height) {
double top, right, bottom, left;
glfmGetDisplayChromeInsets(display, &top, &right, &bottom, &left);
static const uint32_t borderColor = 0xff0000ff;
static const uint32_t insetColor = 0xffff3322;
TestPatternApp *app = glfmGetUserData(display);
Texture texture = 0;
uint32_t *data = malloc(width * height * sizeof(uint32_t));
if (data) {
uint32_t *out = data;
for (uint32_t y = 0; y < height; y++) {
*out++ = borderColor;
if (y == 0 || y == height - 1) {
for (uint32_t x = 1; x < width - 1; x++) {
*out++ = borderColor;
}
} else if (y < bottom || y >= height - top) {
for (uint32_t x = 1; x < width - 1; x++) {
*out++ = insetColor;
}
} else {
uint32_t x = 1;
while (x < left) {
*out++ = insetColor;
x++;
}
while (x < width - right - 1) {
*out++ = ((x & 1U) == (y & 1U)) ? 0xff000000 : 0xffffffff;
x++;
}
while (x < width - 1) {
*out++ = insetColor;
x++;
}
}
*out++ = borderColor;
}
texture = app->renderer->textureUpload(app->renderer, width, height, (uint8_t *)data);
free(data);
}
if (texture != 0) {
printf("Created test pattern %ix%i with insets %i, %i, %i, %i\n", width, height,
(int)top, (int)right, (int)bottom, (int)left);
}
return texture;
}
static void onSurfaceCreated(GLFMDisplay *display, int width, int height) {
TestPatternApp *app = glfmGetUserData(display);
#if defined(__APPLE__)
if (glfmGetRenderingAPI(display) == GLFMRenderingAPIMetal) {
app->renderer = createRendererMetal(display);
printf("Hello from Metal!\n");
}
#endif
if (!app->renderer) {
app->renderer = createRendererGLES2(display);
printf("Hello from GLES2!\n");
}
app->textureNeedsUpdate = true;
app->needsRedraw = true;
}
static void onSurfaceResized(GLFMDisplay *display, int width, int height) {
TestPatternApp *app = glfmGetUserData(display);
app->textureNeedsUpdate = true;
}
static void onSurfaceRefresh(GLFMDisplay *display) {
TestPatternApp *app = glfmGetUserData(display);
app->needsRedraw = true;
}
static void onOrientationChange(GLFMDisplay *display, GLFMInterfaceOrientation orientation) {
TestPatternApp *app = glfmGetUserData(display);
app->textureNeedsUpdate = true;
}
static void onInsetsChange(GLFMDisplay *display, double top, double right, double bottom, double left) {
TestPatternApp *app = glfmGetUserData(display);
app->textureNeedsUpdate = true;
}
static void onSurfaceDestroyed(GLFMDisplay *display) {
// When the surface is destroyed, all existing GL resources are no longer valid.
TestPatternApp *app = glfmGetUserData(display);
app->renderer->textureDestroy(app->renderer, app->texture);
app->renderer->destroy(app->renderer);
app->renderer = NULL;
}
static bool onTouch(GLFMDisplay *display, int touch, GLFMTouchPhase phase, double x, double y) {
if (phase == GLFMTouchPhaseBegan) {
char *chromeString = NULL;
GLFMUserInterfaceChrome chrome = glfmGetDisplayChrome(display);
switch (chrome) {
case GLFMUserInterfaceChromeNavigation:
chromeString = "Navigation+StatusBar";
glfmSetDisplayChrome(display, GLFMUserInterfaceChromeNavigationAndStatusBar);
break;
case GLFMUserInterfaceChromeNavigationAndStatusBar:
chromeString = "None";
glfmSetDisplayChrome(display, GLFMUserInterfaceChromeNone);
break;
case GLFMUserInterfaceChromeNone: default:
chromeString = "Navigation";
glfmSetDisplayChrome(display, GLFMUserInterfaceChromeNavigation);
break;
}
glfmPerformHapticFeedback(display, GLFMHapticFeedbackLight);
printf("Chrome set to: %s\n", chromeString);
return true;
} else {
return false;
}
}
static void onDraw(GLFMDisplay *display) {
TestPatternApp *app = glfmGetUserData(display);
if (!app->textureNeedsUpdate && !app->needsRedraw) {
return;
}
int width, height;
glfmGetDisplaySize(display, &width, &height);
if (app->textureNeedsUpdate && app->texture != NULL_TEXTURE) {
app->renderer->textureDestroy(app->renderer, app->texture);
app->texture = NULL_TEXTURE;
}
if (app->texture == NULL_TEXTURE) {
app->texture = createTestPatternTexture(display, width, height);
app->textureNeedsUpdate = false;
}
app->renderer->drawFrameStart(app->renderer, width, height);
const Vertex vertices[4] = {
{ .position = { -1, -1 }, .texCoord = { 0, 0 } },
{ .position = { 1, -1 }, .texCoord = { 1, 0 } },
{ .position = { -1, 1 }, .texCoord = { 0, 1 } },
{ .position = { 1, 1 }, .texCoord = { 1, 1 } },
};
app->renderer->drawQuad(app->renderer, app->texture, &vertices);
app->renderer->drawFrameEnd(app->renderer);
glfmSwapBuffers(display);
app->needsRedraw = false;
}
void glfmMain(GLFMDisplay *display) {
TestPatternApp *app = calloc(1, sizeof(TestPatternApp));
GLFMRenderingAPI renderingAPI = glfmIsMetalSupported(display) ? GLFMRenderingAPIMetal : GLFMRenderingAPIOpenGLES2;
glfmSetDisplayConfig(display,
renderingAPI,
GLFMColorFormatRGBA8888,
GLFMDepthFormatNone,
GLFMStencilFormatNone,
GLFMMultisampleNone);
glfmSetUserData(display, app);
glfmSetDisplayChrome(display, GLFMUserInterfaceChromeNavigationAndStatusBar);
glfmSetTouchFunc(display, onTouch);
glfmSetSurfaceCreatedFunc(display, onSurfaceCreated);
glfmSetSurfaceResizedFunc(display, onSurfaceResized);
glfmSetSurfaceRefreshFunc(display, onSurfaceRefresh);
glfmSetSurfaceDestroyedFunc(display, onSurfaceDestroyed);
glfmSetOrientationChangedFunc(display, onOrientationChange);
glfmSetDisplayChromeInsetsChangedFunc(display, onInsetsChange);
glfmSetRenderFunc(display, onDraw);
}