This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.c
422 lines (356 loc) · 9.52 KB
/
graphics.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <GLFW/glfw3.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <soundpipe.h>
#include <sporth.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include "spigot.h"
#include "box.h"
#define WIDTH 193
#ifndef SCALING
#define SCALING 1
#endif
struct spigot_graphics {
int run;
pthread_t thread;
GLFWwindow *window;
int please_draw;
unsigned char *buf;
spigot_state *state;
int zoom;
int state_loaded;
int begin;
int _recompile;
int *recompile;
};
void spigot_color_rgb(spigot_color *clr, uint8_t r, uint8_t g, uint8_t b)
{
clr->r = r;
clr->g = g;
clr->b = b;
}
void spigot_color_rgb_hex(spigot_color *clr, long rgb)
{
clr->r = (rgb & 0xff0000) >> 16;
clr->g = (rgb & 0x00ff00) >> 8;
clr->b = (rgb & 0x0000ff);
}
/* NOTE: dimensions should be multiples of 8. if not, you better zero
* pad your bitmaps */
void spigot_draw_bitmap(spigot_graphics *gfx, spigot_color *clr,
int x_pos, int y_pos, int w, int h, const unsigned char *glyph)
{
int x_bytes;
int x, y, c;
unsigned char byte;
int pos;
int xp;
int base;
x_bytes = ((w - 1) >> 3) + 1;
base = y_pos * WIDTH * 3 + x_pos * 3;
for(y = 0; y < h; y++) {
xp = 0;
for(x = 0; x < x_bytes; x++) {
byte = glyph[y * x_bytes + x];
for(c = 7; c >= 0; c--) {
pos = base + y * WIDTH * 3 + xp * 3;
if((byte & (1 << c))) {
gfx->buf[pos] = clr->r;
gfx->buf[pos + 1] = clr->g;
gfx->buf[pos + 2] = clr->b;
}
xp++;
}
}
}
}
void spigot_draw_box(spigot_graphics *gfx, spigot_color *clr, int pos)
{
int x, y;
x = (pos % 12) * 16;
y = (pos / 12) * 16;
spigot_draw_bitmap(gfx, clr, x, y, 1, 17, spigot_box);
spigot_draw_bitmap(gfx, clr, x + 16, y, 1, 17, spigot_box);
spigot_draw_bitmap(gfx, clr, x, y + 16, 16, 1, spigot_box + 17);
spigot_draw_bitmap(gfx, clr, x, y, 16, 1, spigot_box + 17);
}
/* static void draw(spigot_pbrain *spb) */
static void draw(spigot_graphics *gfx)
{
spigot_state *state;
state = gfx->state;
state->draw(gfx, state->ud);
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glPixelZoom(gfx->zoom * SCALING, -gfx->zoom * SCALING);
glDrawPixels(193, 193, GL_RGB, GL_UNSIGNED_BYTE, gfx->buf);
}
static void init(void)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClearColor(0.0, 0.0, 0.0, 0.0);
}
static void errorcb(int error, const char* desc)
{
printf("GLFW error %d: %s\n", error, desc);
}
static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
{
spigot_graphics *gfx;
spigot_state *state;
gfx = glfwGetWindowUserPointer(window);
state = gfx->state;
if(action == GLFW_PRESS || action == GLFW_REPEAT) {
if(mods == GLFW_MOD_CONTROL && key == GLFW_KEY_R) {
spigot_recompile(gfx);
}
if(mods != GLFW_MOD_SHIFT) {
switch(key) {
case GLFW_KEY_SPACE:
state->toggle(state->ud);
gfx->please_draw = 1;
break;
case GLFW_KEY_H:
state->left(state->ud);
gfx->please_draw = 1;
break;
case GLFW_KEY_L:
state->right(state->ud);
gfx->please_draw = 1;
break;
case GLFW_KEY_J:
state->down(state->ud);
gfx->please_draw = 1;
break;
case GLFW_KEY_K:
state->up(state->ud);
gfx->please_draw = 1;
break;
case GLFW_KEY_Z:
state->reset(state->ud);
gfx->please_draw = 1;
break;
default:
break;
}
}
state->key(gfx, state->ud, key, scancode, action, mods);
}
}
void spigot_graphics_loop(spigot_graphics *spgt)
{
int w, h;
if (!glfwInit()) {
printf("Failed to init GLFW.");
pthread_exit(NULL);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
spgt->window = glfwCreateWindow(193 * spgt->zoom, 193 * spgt->zoom,
"spigot", NULL, NULL);
if (!spgt->window) {
glfwTerminate();
pthread_exit(NULL);
}
glfwSetKeyCallback(spgt->window, key);
glfwSetErrorCallback(errorcb);
glfwMakeContextCurrent(spgt->window);
glfwSwapInterval(0);
glfwSetTime(0);
init();
glfwSetWindowUserPointer(spgt->window, spgt);
#ifdef BUILD_SPORTH_PLUGIN
while(spgt->run)
#else
while(!glfwWindowShouldClose(spgt->window))
#endif
{
glfwPollEvents();
glfwGetWindowSize(spgt->window, &w, &h);
glViewport(0, 0, (GLsizei) w * SCALING, (GLsizei) h * SCALING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w * SCALING, h * SCALING, 0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
if(spgt->please_draw) {
spgt->please_draw = 0;
draw(spgt);
glfwSwapBuffers(spgt->window);
}
usleep(8000);
}
glfwTerminate();
fprintf(stderr, "Terminated...\n");
}
static void * run_loop(void *ud)
{
spigot_graphics *spgt = (spigot_graphics *)ud;
spigot_graphics_loop(spgt);
pthread_exit(0);
return NULL;
}
void spigot_start(spigot_graphics *spgt)
{
spgt->run = 1;
pthread_create(&spgt->thread, NULL, run_loop, spgt);
}
void spigot_stop(spigot_graphics *spgt)
{
if(spgt->run) {
spgt->run = 0;
pthread_join(spgt->thread, NULL);
}
}
void spigot_gfx_init(spigot_graphics *gfx)
{
int i;
spigot_color clr;
spigot_state *state;
spigot_color_rgb_hex(&clr, 0x000000);
state = gfx->state;
for(i = 0; i < 193 * 193 * 3; i+=3) {
gfx->buf[i] = clr.r;
gfx->buf[i+ 1] = clr.g;
gfx->buf[i + 2] = clr.b;
}
/* parse_code(gfx); */
state->gfx_init(gfx, state->ud);
}
spigot_graphics * spigot_gfx_new(int zoom)
{
spigot_graphics *gfx;
gfx = malloc(sizeof(spigot_graphics));
gfx->buf = malloc(193 * 193 * 3 * sizeof(unsigned char));
gfx->zoom = zoom;
gfx->state_loaded = 0;
gfx->begin = 0;
gfx->recompile = &gfx->_recompile;
return gfx;
}
void spigot_gfx_free(spigot_graphics *gfx)
{
free(gfx->buf);
free(gfx);
}
void spigot_gfx_step(spigot_graphics *gfx)
{
gfx->please_draw = 1;
}
void spigot_gfx_set_state(spigot_graphics *gfx, spigot_state *state)
{
gfx->state = state;
gfx->state_loaded = 1;
}
unsigned char * spigot_graphics_get_buf(spigot_graphics *gfx)
{
return gfx->buf;
}
void spigot_draw_hline(spigot_graphics *gfx,
spigot_color *clr, int pos, int start, int len)
{
int p;
int off;
unsigned char *buf;
buf = spigot_graphics_get_buf(gfx);
for(p = 0; p < len; p++) {
off = (p + start) * 3 + pos * 193 * 3;
buf[off] = clr->r;
buf[off + 1] = clr->g;
buf[off + 2] = clr->b;
}
}
void spigot_draw_vline(spigot_graphics *gfx,
spigot_color *clr, int pos, int start, int len)
{
int p;
unsigned int off;
unsigned char *buf;
buf = spigot_graphics_get_buf(gfx);
for(p = 0; p < len; p++) {
off = (p + start) * 3 * 193 + pos * 3;
buf[off] = clr->r;
buf[off + 1] = clr->g;
buf[off + 2] = clr->b;
}
}
void spigot_draw_glyph(spigot_graphics *gfx, spigot_color *clr,
int x_pos, int y_pos,
int w, int h,
int stride,
const unsigned char *glyph)
{
unsigned int glyph_pos;
unsigned int img_pos;
unsigned int x, y;
unsigned char *buf;
buf = spigot_graphics_get_buf(gfx);
for(y = 0; y < h; y++) {
for(x = 0; x < w; x++) {
glyph_pos = (y*stride) + x;
if(glyph[glyph_pos]) {
img_pos = ((y + y_pos)*193*3) + (x + x_pos)*3;
buf[img_pos] = clr->r;
buf[img_pos + 1] = clr->g;
buf[img_pos + 2] = clr->b;
}
}
}
}
void spigot_draw_fill(spigot_graphics *gfx, spigot_color *clr)
{
unsigned char *buf;
unsigned int pos;
unsigned int x, y;
buf = spigot_graphics_get_buf(gfx);
for(y = 0; y < 193; y++) {
for(x = 0; x < 193; x++) {
pos = y * 193 * 3 + x * 3;
buf[pos] = clr->r;
buf[pos + 1] = clr->g;
buf[pos + 2] = clr->b;
}
}
}
void spigot_draw_rect(spigot_graphics *gfx, spigot_color *clr,
int pos_x, int pos_y, int w, int h)
{
int x, y;
int pos;
unsigned char *buf;
buf = spigot_graphics_get_buf(gfx);
for(y = 0; y < h; y++) {
for(x = 0; x < w; x++) {
pos = (pos_y + y) * 193 * 3 + (pos_x + x) * 3;
buf[pos] = clr->r;
buf[pos + 1] = clr->g;
buf[pos + 2] = clr->b;
}
}
}
void spigot_start_why_dont_you(spigot_graphics *gfx)
{
gfx->begin = 1;
}
int spigot_is_it_happening(spigot_graphics *gfx)
{
return gfx->begin;
}
int spigot_loaded(spigot_graphics *gfx)
{
return gfx->state_loaded;
}
void spigot_set_zoom(spigot_graphics *gfx, int zoom)
{
gfx->zoom = zoom;
}
void spigot_set_recompile(spigot_graphics *gfx, int *recompile)
{
gfx->recompile = recompile;
}
void spigot_recompile(spigot_graphics *gfx)
{
*gfx->recompile = 1;
}