forked from brackeen/glfm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
227 lines (199 loc) · 7.24 KB
/
main.c
File metadata and controls
227 lines (199 loc) · 7.24 KB
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
// Example app that draws a triangle. The triangle can be moved via touch or keyboard arrow keys.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "glfm.h"
#define FILE_COMPAT_ANDROID_ACTIVITY glfmAndroidGetActivity()
#include "file_compat.h"
typedef struct {
GLuint program;
GLuint vertexBuffer;
double lastTouchX;
double lastTouchY;
double offsetX;
double offsetY;
bool needsRedraw;
} ExampleApp;
static void onFrame(GLFMDisplay *display);
static void onSurfaceCreated(GLFMDisplay *display, int width, int height);
static void onSurfaceRefresh(GLFMDisplay *display);
static void onSurfaceDestroyed(GLFMDisplay *display);
static bool onTouch(GLFMDisplay *display, int touch, GLFMTouchPhase phase, double x, double y);
static bool onKey(GLFMDisplay *display, GLFMKey keyCode, GLFMKeyAction action, int modifiers);
// Main entry point
void glfmMain(GLFMDisplay *display) {
ExampleApp *app = calloc(1, sizeof(ExampleApp));
glfmSetDisplayConfig(display,
GLFMRenderingAPIOpenGLES2,
GLFMColorFormatRGBA8888,
GLFMDepthFormatNone,
GLFMStencilFormatNone,
GLFMMultisampleNone);
glfmSetUserData(display, app);
glfmSetSurfaceCreatedFunc(display, onSurfaceCreated);
glfmSetSurfaceRefreshFunc(display, onSurfaceRefresh);
glfmSetSurfaceDestroyedFunc(display, onSurfaceDestroyed);
glfmSetRenderFunc(display, onFrame);
glfmSetTouchFunc(display, onTouch);
glfmSetKeyFunc(display, onKey);
}
static bool onTouch(GLFMDisplay *display, int touch, GLFMTouchPhase phase, double x, double y) {
if (phase == GLFMTouchPhaseHover) {
return false;
}
ExampleApp *app = glfmGetUserData(display);
app->needsRedraw = true;
if (phase != GLFMTouchPhaseBegan) {
int width, height;
glfmGetDisplaySize(display, &width, &height);
app->offsetX += 2 * (x - app->lastTouchX) / width;
app->offsetY -= 2 * (y - app->lastTouchY) / height;
}
app->lastTouchX = x;
app->lastTouchY = y;
return true;
}
static bool onKey(GLFMDisplay *display, GLFMKey keyCode, GLFMKeyAction action, int modifiers) {
bool handled = false;
if (action == GLFMKeyActionPressed) {
ExampleApp *app = glfmGetUserData(display);
app->needsRedraw = true;
switch (keyCode) {
case GLFMKeyLeft:
app->offsetX -= 0.1f;
handled = true;
break;
case GLFMKeyRight:
app->offsetX += 0.1f;
handled = true;
break;
case GLFMKeyUp:
app->offsetY += 0.1f;
handled = true;
break;
case GLFMKeyDown:
app->offsetY -= 0.1f;
handled = true;
break;
default:
break;
}
}
return handled;
}
static void onSurfaceCreated(GLFMDisplay *display, int width, int height) {
GLFMRenderingAPI api = glfmGetRenderingAPI(display);
printf("Hello from GLFM! Using OpenGL %s\n",
api == GLFMRenderingAPIOpenGLES32 ? "ES 3.2" :
api == GLFMRenderingAPIOpenGLES31 ? "ES 3.1" :
api == GLFMRenderingAPIOpenGLES3 ? "ES 3.0" : "ES 2.0");
}
static void onSurfaceRefresh(GLFMDisplay *display) {
ExampleApp *app = glfmGetUserData(display);
app->needsRedraw = true;
}
static void onSurfaceDestroyed(GLFMDisplay *display) {
// When the surface is destroyed, all existing GL resources are no longer valid.
ExampleApp *app = glfmGetUserData(display);
app->program = 0;
app->vertexBuffer = 0;
}
static GLuint compileShader(GLenum type, const char *shaderName) {
char fullPath[PATH_MAX];
fc_resdir(fullPath, sizeof(fullPath));
strncat(fullPath, shaderName, sizeof(fullPath) - strlen(fullPath) - 1);
// Get shader string
char *shaderString = NULL;
FILE *shaderFile = fopen(fullPath, "rb");
if (shaderFile) {
fseek(shaderFile, 0, SEEK_END);
long length = ftell(shaderFile);
fseek(shaderFile, 0, SEEK_SET);
shaderString = malloc(length + 1);
if (shaderString) {
fread(shaderString, length, 1, shaderFile);
shaderString[length] = 0;
}
fclose(shaderFile);
}
if (!shaderString) {
printf("Couldn't read file: %s\n", fullPath);
return 0;
}
// Compile
const char *constShaderString = shaderString;
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &constShaderString, NULL);
glCompileShader(shader);
free(shaderString);
// Check compile status
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == 0) {
printf("Couldn't compile shader: %s\n", shaderName);
GLint logLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
GLchar *log = malloc(logLength);
glGetShaderInfoLog(shader, logLength, &logLength, log);
if (log[0] != 0) {
printf("Shader log: %s\n", log);
}
free(log);
}
glDeleteShader(shader);
shader = 0;
}
return shader;
}
static void draw(ExampleApp *app, int width, int height) {
// Create shader
if (app->program == 0) {
GLuint vertShader = compileShader(GL_VERTEX_SHADER, "simple.vert");
GLuint fragShader = compileShader(GL_FRAGMENT_SHADER, "simple.frag");
if (vertShader == 0 || fragShader == 0) {
return;
}
app->program = glCreateProgram();
glAttachShader(app->program, vertShader);
glAttachShader(app->program, fragShader);
glBindAttribLocation(app->program, 0, "a_position");
glBindAttribLocation(app->program, 1, "a_color");
glLinkProgram(app->program);
glDeleteShader(vertShader);
glDeleteShader(fragShader);
}
// Draw background
glViewport(0, 0, width, height);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw triangle
glUseProgram(app->program);
if (app->vertexBuffer == 0) {
glGenBuffers(1, &app->vertexBuffer);
}
glBindBuffer(GL_ARRAY_BUFFER, app->vertexBuffer);
const size_t stride = sizeof(GLfloat) * 6;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void *)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void *)(sizeof(GLfloat) * 3));
const GLfloat vertices[] = {
// x,y,z, r,g,b
app->offsetX + 0.0f, app->offsetY + 0.5f, 0.0, 1.0, 0.0, 0.0,
app->offsetX - 0.5f, app->offsetY - 0.5f, 0.0, 0.0, 1.0, 0.0,
app->offsetX + 0.5f, app->offsetY - 0.5f, 0.0, 0.0, 0.0, 1.0,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
static void onFrame(GLFMDisplay *display) {
ExampleApp *app = glfmGetUserData(display);
if (app->needsRedraw) {
app->needsRedraw = false;
int width, height;
glfmGetDisplaySize(display, &width, &height);
draw(app, width, height);
glfmSwapBuffers(display);
}
}