forked from antirez/load81
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload81.c
559 lines (487 loc) · 15.9 KB
/
load81.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/******************************************************************************
* Copyright (C) 2012 Salvatore Sanfilippo. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <ctype.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "framebuffer.h"
#include "editor.h"
#include "load81.h"
#define NOTUSED(V) ((void) V)
struct globalConfig l81;
/* =========================== Utility functions ============================ */
/* Return the UNIX time in microseconds */
long long ustime(void) {
struct timeval tv;
long long ust;
gettimeofday(&tv, NULL);
ust = ((long long)tv.tv_sec)*1000000;
ust += tv.tv_usec;
return ust;
}
/* Return the UNIX time in milliseconds */
long long mstime(void) {
return ustime()/1000;
}
/* ========================= Lua helper functions ========================== */
/* Set a Lua global to the specified number. */
void setNumber(char *name, lua_Number n) {
lua_pushnumber(l81.L,n);
lua_setglobal(l81.L,name);
}
/* Get a Lua global containing a number. */
lua_Number getNumber(char *name) {
lua_Number n;
lua_getglobal(l81.L,name);
n = lua_tonumber(l81.L,-1);
lua_pop(l81.L,1);
return n;
}
/* Set a Lua global table field to the value on the top of the Lua stack. */
void setTableField(char *name, char *field) {
lua_getglobal(l81.L,name); /* Stack: val table */
/* Create the table if needed */
if (lua_isnil(l81.L,-1)) {
lua_pop(l81.L,1); /* Stack: val */
lua_newtable(l81.L); /* Stack: val table */
lua_setglobal(l81.L,name); /* Stack: val */
lua_getglobal(l81.L,name); /* Stack: val table */
}
/* Set the field */
if (lua_istable(l81.L,-1)) {
lua_pushstring(l81.L,field); /* Stack: val table field */
lua_pushvalue(l81.L,-3); /* Stack: val table field val */
lua_settable(l81.L,-3); /* Stack: val table */
}
lua_pop(l81.L,2); /* Stack: (empty) */
}
void setTableFieldNumber(char *name, char *field, lua_Number n) {
lua_pushnumber(l81.L,n);
setTableField(name,field);
}
void setTableFieldString(char *name, char *field, char *s) {
lua_pushstring(l81.L,s);
setTableField(name,field);
}
/* Set the error string and the error line number. */
void programError(const char *e) {
int line = 0;
char *p;
if ((p = strchr(e,':')) != NULL)
line = atoi(p+1)-1;
editorSetError(e,line);
l81.luaerr = 1;
}
/* ============================= Lua bindings ============================== */
int fillBinding(lua_State *L) {
l81.r = lua_tonumber(L,-4);
l81.g = lua_tonumber(L,-3);
l81.b = lua_tonumber(L,-2);
l81.alpha = lua_tonumber(L,-1) * 255;
if (l81.r < 0) l81.r = 0;
if (l81.r > 255) l81.r = 255;
if (l81.g < 0) l81.g = 0;
if (l81.g > 255) l81.g = 255;
if (l81.b < 0) l81.b = 0;
if (l81.b > 255) l81.b = 255;
if (l81.alpha < 0) l81.alpha = 0;
if (l81.alpha > 255) l81.alpha = 255;
return 0;
}
int rectBinding(lua_State *L) {
int x,y,w,h;
x = lua_tonumber(L,-4);
y = lua_tonumber(L,-3);
w = lua_tonumber(L,-2);
h = lua_tonumber(L,-1);
drawBox(l81.fb,x,y,x+(w-1),y+(h-1),l81.r,l81.g,l81.b,l81.alpha);
return 0;
}
int ellipseBinding(lua_State *L) {
int x,y,rx,ry;
x = lua_tonumber(L,-4);
y = lua_tonumber(L,-3);
rx = lua_tonumber(L,-2);
ry = lua_tonumber(L,-1);
drawEllipse(l81.fb,x,y,rx,ry,l81.r,l81.g,l81.b,l81.alpha);
return 0;
}
int triangleBinding(lua_State *L) {
int x1,y1,x2,y2,x3,y3;
x1 = lua_tonumber(L,-6);
y1 = lua_tonumber(L,-5);
x2 = lua_tonumber(L,-4);
y2 = lua_tonumber(L,-3);
x3 = lua_tonumber(L,-2);
y3 = lua_tonumber(L,-1);
drawTriangle(l81.fb,x1,y1,x2,y2,x3,y3,l81.r,l81.g,l81.b,l81.alpha);
return 0;
}
int lineBinding(lua_State *L) {
int x1,y1,x2,y2;
x1 = lua_tonumber(L,-4);
y1 = lua_tonumber(L,-3);
x2 = lua_tonumber(L,-2);
y2 = lua_tonumber(L,-1);
drawLine(l81.fb,x1,y1,x2,y2,l81.r,l81.g,l81.b,l81.alpha);
return 0;
}
int textBinding(lua_State *L) {
int x,y;
const char *s;
size_t len;
x = lua_tonumber(L,-3);
y = lua_tonumber(L,-2);
s = lua_tolstring(L,-1,&len);
if (!s) return 0;
bfWriteString(l81.fb,x,y,s,len,l81.r,l81.g,l81.b,l81.alpha);
return 0;
}
int setFPSBinding(lua_State *L) {
l81.fps = lua_tonumber(L,-1);
if (l81.fps <= 0) l81.fps = 1;
SDL_setFramerate(&l81.fb->fps_mgr,l81.fps);
return 0;
}
int backgroundBinding(lua_State *L) {
int r,g,b;
r = lua_tonumber(L,-3);
g = lua_tonumber(L,-2);
b = lua_tonumber(L,-1);
fillBackground(l81.fb,r,g,b);
return 0;
}
int getpixelBinding(lua_State *L) {
Uint32 pixel;
int x, y;
unsigned char rgb[3];
x = lua_tonumber(L,-2);
y = l81.fb->height - 1 - lua_tonumber(L,-1);
if (x < 0 || x >= l81.fb->width || y < 0 || y >= l81.fb->height) {
pixel = 0;
} else {
SDL_Rect rect = {x,y,1,1};
SDL_RenderReadPixels(l81.fb->renderer,&rect,SDL_PIXELFORMAT_BGR888,
rgb,3*l81.fb->width);
}
/* Return the pixel as three values: r, g, b. */
lua_pushnumber(L,rgb[0]);
lua_pushnumber(L,rgb[1]);
lua_pushnumber(L,rgb[2]);
return 3;
}
int spriteBinding(lua_State *L) {
const char *filename;
int x, y, angle, antialiasing;
void *sprite;
filename = lua_tostring(L, 1);
x = lua_tonumber(L, 2);
y = lua_tonumber(L, 3);
angle = luaL_optnumber(L,4,0);
antialiasing = lua_toboolean(L,5);
sprite = spriteLoad(L,filename);
spriteBlit(l81.fb, sprite, x, y, angle, antialiasing);
return 1;
}
/* ========================== Events processing ============================= */
void setup(void) {
lua_getglobal(l81.L,"setup");
if (!lua_isnil(l81.L,-1)) {
if (lua_pcall(l81.L,0,0,0)) {
programError(lua_tostring(l81.L, -1));
}
} else {
lua_pop(l81.L,1);
}
}
void draw(void) {
lua_getglobal(l81.L,"draw");
if (!lua_isnil(l81.L,-1)) {
if (lua_pcall(l81.L,0,0,0)) {
programError(lua_tostring(l81.L, -1));
}
} else {
lua_pop(l81.L,1);
}
}
/* Update the keyboard.pressed and mouse.pressed Lua table. */
void updatePressedState(char *object, char *keyname, int pressed) {
lua_getglobal(l81.L,object); /* $keyboard */
lua_pushstring(l81.L,"pressed"); /* $keyboard, "pressed" */
lua_gettable(l81.L,-2); /* $keyboard, $pressed */
lua_pushstring(l81.L,keyname); /* $keyboard, $pressed, "keyname" */
if (pressed) {
lua_pushboolean(l81.L,1); /* $k, $pressed, "keyname", true */
} else {
lua_pushnil(l81.L); /* $k, $pressed, "keyname", nil */
}
lua_settable(l81.L,-3); /* $k, $pressed */
lua_pop(l81.L,2);
}
void keyboardEvent(SDL_KeyboardEvent *key, int down) {
char *keyname = (char*)SDL_GetKeyName(key->keysym.sym);
size_t keylen = strlen(keyname);
/* SDL2 key names are no longer lowercase by default, so we need
* to convert them. */
char buf[32];
if (keylen >= sizeof(buf)) keylen = sizeof(buf)-1;
for (size_t j = 0; j < keylen; j++) buf[j] = tolower(keyname[j]);
buf[keylen] = 0;
setTableFieldString("keyboard","state",down ? "down" : "up");
setTableFieldString("keyboard","key",buf);
updatePressedState("keyboard",buf,down);
}
void mouseMovedEvent(int x, int y, int xrel, int yrel) {
setTableFieldNumber("mouse","x",x);
setTableFieldNumber("mouse","y",l81.height-1-y);
setTableFieldNumber("mouse","xrel",xrel);
setTableFieldNumber("mouse","yrel",-yrel);
}
void mouseButtonEvent(int button, int pressed) {
char buttonname[32];
snprintf(buttonname,sizeof(buttonname),"%d",button);
updatePressedState("mouse",buttonname,pressed);
}
void resetEvents(void) {
setTableFieldString("keyboard","state","none");
setTableFieldString("keyboard","key","");
}
void showFPS(void) {
int elapsed_ms = mstime()-l81.start_ms;
char buf[64];
if (!elapsed_ms) return;
snprintf(buf,sizeof(buf),"FPS: %.2f",(float)(l81.epoch*1000)/elapsed_ms);
drawBox(l81.fb,0,0,100,20,0,0,0,255);
bfWriteString(l81.fb,0,0,buf,strlen(buf),128,128,128,255);
}
int processSdlEvents(void) {
SDL_Event event;
resetEvents();
while (SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_ESCAPE:
return 1;
break;
default:
keyboardEvent(&event.key,1);
break;
}
break;
case SDL_KEYUP:
keyboardEvent(&event.key,0);
break;
case SDL_MOUSEMOTION:
mouseMovedEvent(event.motion.x,event.motion.y,
event.motion.xrel,event.motion.yrel);
break;
case SDL_MOUSEBUTTONDOWN:
mouseButtonEvent(event.button.button,1);
break;
case SDL_MOUSEBUTTONUP:
mouseButtonEvent(event.button.button,0);
break;
case SDL_QUIT:
exit(0);
break;
}
/* If the next event to process is of type KEYUP or
* MOUSEBUTTONUP we want to stop processing here, so that
* a fast up/down event be noticed by Lua. */
if (SDL_PeepEvents(&event,1,SDL_PEEKEVENT,SDL_FIRSTEVENT,
SDL_LASTEVENT))
{
if (event.type == SDL_KEYUP ||
event.type == SDL_MOUSEBUTTONUP)
break; /* Go to lua before processing more. */
}
}
/* Call the setup function, only the first time. */
if (l81.epoch == 0) {
setup();
if (l81.luaerr) return l81.luaerr;
}
/* Call the draw function at every iteration. */
draw();
l81.epoch++;
/* Refresh the screen */
if (l81.opt_show_fps) showFPS();
presentFrameBuffer(l81.fb);
/* Stop execution on error */
return l81.luaerr;
}
/* =========================== Initialization ============================== */
void initConfig(void) {
l81.width = DEFAULT_WIDTH;
l81.height = DEFAULT_HEIGHT;
l81.fps = 30;
l81.r = 255;
l81.g = l81.b = 0;
l81.alpha = 255;
l81.L = NULL;
l81.luaerr = 0;
l81.opt_show_fps = 0;
l81.opt_full_screen = 0;
l81.filename = NULL;
srand(mstime());
}
/* Load the editor program into Lua. Returns 0 on success, 1 on error. */
int loadProgram(void) {
int buflen;
char *buf = editorRowsToString(&buflen);
if (luaL_loadbuffer(l81.L,buf,buflen,l81.filename)) {
programError(lua_tostring(l81.L, -1));
free(buf);
return 1;
}
free(buf);
if (lua_pcall(l81.L,0,0,0)) {
programError(lua_tostring(l81.L, -1));
return 1;
}
l81.luaerr = 0;
editorClearError();
return 0;
}
void initScreen(void) {
l81.fb = createFrameBuffer(l81.width,l81.height,
l81.opt_full_screen);
}
void resetProgram(void) {
char *initscript =
"keyboard={}; keyboard['pressed']={};"
"mouse={}; mouse['pressed']={};"
"sprites={}";
l81.epoch = 0;
if (l81.L) lua_close(l81.L);
l81.L = lua_open();
luaopen_base(l81.L);
luaopen_table(l81.L);
luaopen_string(l81.L);
luaopen_math(l81.L);
luaopen_debug(l81.L);
setNumber("WIDTH",l81.width);
setNumber("HEIGHT",l81.height);
luaL_loadbuffer(l81.L,initscript,strlen(initscript),"initscript");
lua_pcall(l81.L,0,0,0);
/* Make sure that mouse parameters make sense even before the first
* mouse event captured by SDL */
setTableFieldNumber("mouse","x",0);
setTableFieldNumber("mouse","y",0);
setTableFieldNumber("mouse","xrel",0);
setTableFieldNumber("mouse","yrel",0);
/* Register API */
lua_pushcfunction(l81.L,fillBinding);
lua_setglobal(l81.L,"fill");
lua_pushcfunction(l81.L,rectBinding);
lua_setglobal(l81.L,"rect");
lua_pushcfunction(l81.L,ellipseBinding);
lua_setglobal(l81.L,"ellipse");
lua_pushcfunction(l81.L,backgroundBinding);
lua_setglobal(l81.L,"background");
lua_pushcfunction(l81.L,triangleBinding);
lua_setglobal(l81.L,"triangle");
lua_pushcfunction(l81.L,lineBinding);
lua_setglobal(l81.L,"line");
lua_pushcfunction(l81.L,textBinding);
lua_setglobal(l81.L,"text");
lua_pushcfunction(l81.L,setFPSBinding);
lua_setglobal(l81.L,"setFPS");
lua_pushcfunction(l81.L,getpixelBinding);
lua_setglobal(l81.L,"getpixel");
lua_pushcfunction(l81.L,spriteBinding);
lua_setglobal(l81.L,"sprite");
initSpriteEngine(l81.L);
/* Start with a black screen */
fillBackground(l81.fb,0,0,0);
}
/* ================================= Main ================================== */
void showCliHelp(void) {
fprintf(stderr, "Usage: load81 [options] program.lua\n"
" --width <pixels> Set screen width\n"
" --height <pixels> Set screen height\n"
" --full Enable full screen mode\n"
" --fps Show frames per second\n"
" --help Show this help screen\n"
);
exit(1);
}
void parseOptions(int argc, char **argv) {
int j;
for (j = 1; j < argc; j++) {
char *arg = argv[j];
int lastarg = j == argc-1;
if (!strcasecmp(arg,"--fps")) {
l81.opt_show_fps = 1;
} else if (!strcasecmp(arg,"--full")) {
l81.opt_full_screen = 1;
} else if (!strcasecmp(arg,"--width") && !lastarg) {
l81.width = atoi(argv[++j]);
} else if (!strcasecmp(arg,"--height") && !lastarg) {
l81.height = atoi(argv[++j]);
} else if (!strcasecmp(arg,"--help")) {
showCliHelp();
} else {
if (l81.filename == NULL && arg[0] != '-') {
l81.filename = arg;
} else {
fprintf(stderr,
"Unrecognized option or missing argument: %s\n\n", arg);
showCliHelp();
}
}
}
if (l81.filename == NULL) {
fprintf(stderr,"No Lua program filename specified.\n\n");
showCliHelp();
}
}
int main(int argc, char **argv) {
NOTUSED(argc);
NOTUSED(argv);
initConfig();
parseOptions(argc,argv);
initScreen();
initEditor(l81.fb,30,30,30,30);
editorOpen(l81.filename);
while(1) {
resetProgram();
loadProgram();
if (!l81.luaerr) {
SDL_setFramerate(&l81.fb->fps_mgr,l81.fps);
l81.start_ms = mstime();
while(!processSdlEvents());
if (editorFileWasModified()) editorSave(l81.filename);
}
editorRun();
}
return 0;
}