-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglut.c
262 lines (205 loc) · 5.5 KB
/
glut.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
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
#include <GL/glew.h>
#ifdef FREE
#include <GL/freeglut.h>
#else
#include <GL/glut.h>
#endif
#include <math.h>
#include <setjmp.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gl2.h"
#include "glut.h"
#include "conf.h"
// TODO Undepend.
#include "dem.h"
#include "ext.h"
#include "io.h"
#include "msg.h"
#include "sig.h"
#include "tle.h"
/// This is necessary since GLUT does not support passing in closures.
static struct bmm_glut *bmm_glut;
void bmm_glut_opts_def(struct bmm_glut_opts *const opts) {
opts->vpath = "bmm-glut.vs.glsl";
opts->fpath = "bmm-glut.fs.glsl";
}
void bmm_glut_def(struct bmm_glut *const glut,
struct bmm_glut_opts const *const opts) {
glut->opts = *opts;
glut->prog = NULL;
glut->vshader = 0;
glut->fshader = 0;
glut->program = 0;
}
static enum bmm_io_read msg_read(void *buf, size_t const n,
__attribute__ ((__unused__)) void *const ptr) {
return bmm_io_readin(buf, n);
}
static bool bmm_glut_open(struct bmm_glut *const glut) {
return true;
}
static bool bmm_glut_close(struct bmm_glut *const glut) {
return true;
}
enum bmm_io_read bmm_glut_step(struct bmm_glut *const glut) {
struct bmm_msg_spec spec;
switch (bmm_msg_spec_read(&spec, msg_read, NULL)) {
case BMM_IO_READ_ERROR:
return BMM_IO_READ_ERROR;
case BMM_IO_READ_EOF:
return BMM_IO_READ_EOF;
}
if (spec.endy != bmm_endy_get()) {
BMM_TLE_EXTS(BMM_TLE_NUM_UNIMPL, "Unsupported endianness");
return BMM_IO_READ_ERROR;
}
if (spec.tag != BMM_MSG_TAG_SP) {
BMM_TLE_EXTS(BMM_TLE_NUM_UNIMPL, "Unsupported tag");
return BMM_IO_READ_ERROR;
}
enum bmm_msg_num num;
switch (bmm_msg_num_read(&num, msg_read, NULL)) {
case BMM_IO_READ_EOF:
BMM_TLE_EXTS(BMM_TLE_NUM_IO, "Unexpected end");
case BMM_IO_READ_ERROR:
return BMM_IO_READ_ERROR;
}
switch (num) {
case BMM_MSG_NUM_PARTS:
{
size_t npart;
switch (msg_read(&npart, sizeof npart, NULL)) {
case BMM_IO_READ_EOF:
BMM_TLE_EXTS(BMM_TLE_NUM_IO, "Unexpected end");
case BMM_IO_READ_ERROR:
return BMM_IO_READ_ERROR;
}
double parts[BMM_MPART];
switch (msg_read(parts, sizeof parts, NULL)) {
case BMM_IO_READ_EOF:
BMM_TLE_EXTS(BMM_TLE_NUM_IO, "Unexpected end");
case BMM_IO_READ_ERROR:
return BMM_IO_READ_ERROR;
}
}
break;
default:
dynamic_assert(false, "Unsupported message number");
}
return BMM_IO_READ_SUCCESS;
}
static bool bmm_glut_run_(struct bmm_glut *const glut) {
for ever {
int signum;
if (bmm_sig_use(&signum))
switch (signum) {
case SIGINT:
case SIGQUIT:
case SIGTERM:
case SIGPIPE:
BMM_TLE_EXTS(BMM_TLE_NUM_ASYNC, "Interrupted");
return false;
}
switch (bmm_glut_step(glut)) {
case BMM_IO_READ_ERROR:
return false;
case BMM_IO_READ_EOF:
return true;
}
}
}
static void ifunc(void) {
}
static void render(void) {
}
static void reshape(int const w, int const h) {
}
static void bmm_glut_exit(void) {
if (bmm_glut->program != 0)
glDeleteProgram(bmm_glut->program);
if (bmm_glut->fshader != 0)
glDeleteShader(bmm_glut->fshader);
if (bmm_glut->vshader != 0)
glDeleteShader(bmm_glut->vshader);
free(bmm_glut->prog);
}
static void kfunc(unsigned char const key, int const x, int const y) {
switch (key) {
case '\x1b':
case 'q':
#ifdef FREE
glutLeaveMainLoop();
#else
bmm_glut_exit();
longjmp(bmm_glut->env, 1);
#endif
}
}
static void sfunc(int const key, int const x, int const y) {
}
static void mfunc(int const button, int const state, int const x, int const y) {
// The button parameter is one of GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON.
// The state parameter is either GLUT_UP or GLUT_DOWN.
}
#ifdef FREE
static void mwfunc(int const wheel, int const dir, int const x, int const y) {
// There is the wheel number, direction is +/- 1, and x and y are the mouse coordinates.
}
#endif
bool bmm_glut_run(struct bmm_glut *const glut) {
int const sigs[] = {SIGINT, SIGQUIT, SIGTERM, SIGPIPE};
if (bmm_sig_register(sigs, nmembof(sigs)) != SIZE_MAX) {
BMM_TLE_STDS();
return false;
}
bmm_glut = glut;
char const *const prog = bmm_tle_prog();
size_t const size = strlen(prog) + 1;
bmm_glut->prog = malloc(size);
if (bmm_glut->prog == NULL) {
BMM_TLE_STDS();
return false;
}
(void) memcpy(bmm_glut->prog, prog, size);
int argc = 1;
char *argv[] = {bmm_glut->prog, NULL};
glutInit(&argc, argv);
#ifdef FREE
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
#endif
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
int const window = glutCreateWindow(BMM_SHORT);
glutSetWindow(window);
glutDisplayFunc(render);
glutIdleFunc(ifunc);
glutReshapeFunc(reshape);
glewInit();
if (!GLEW_VERSION_3_3) {
BMM_TLE_EXTS(BMM_TLE_NUM_GL, "Failed to initialize OpenGL 3.3");
return false;
}
glutKeyboardFunc(kfunc);
glutSpecialFunc(sfunc);
glutMouseFunc(mfunc);
#ifdef FREE
glutMouseWheelFunc(mwfunc);
#endif
if (setjmp(bmm_glut->env) == 0)
glutMainLoop();
#ifdef FREE
bmm_glut_exit();
#endif
return true;
}
bool bmm_glut_run_with(struct bmm_glut_opts const *const opts) {
struct bmm_glut glut;
bmm_glut_def(&glut, opts);
return bmm_glut_run(&glut);
}