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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
require 'generate'
function write_output_lsdllib()
check_funcs = {
int = 'luaL_checkinteger(L, @n)',
Uint8 = 'luaL_checkinteger(L, @n)',
Uint16 = 'luaL_checkinteger(L, @n)',
Uint32 = 'luaL_checkinteger(L, @n)',
size_t = 'luaL_checkinteger(L, @n)',
SDL_GrabMode = 'luaL_checkinteger(L, @n)',
SDL_GLattr = 'luaL_checkinteger(L, @n)',
float = 'luaL_checknumber(L, @n)',
double = 'luaL_checknumber(L, @n)',
['SDL_Rect*'] = '&checkrect(L, @n)',
['const char*'] = 'luaL_checkstring(L, @n)',
['SDL_Surface*'] = '*((SDL_Surface **) luaL_checkudata(L, @n, "SDL_Surface"))',
['Mix_Music*'] = '*((Mix_Music **) luaL_checkudata(L, @n, "Mix_Music"))',
['Mix_Chunk*'] = '*((Mix_Chunk **) luaL_checkudata(L, @n, "Mix_Chunk"))',
['SDL_RWops*'] = 'checkrwops(L, @n)',
}
return_codes = {
int = 'lua_pushinteger(L, @b);',
Uint8 = 'lua_pushinteger(L, @b);',
Uint16 = 'lua_pushinteger(L, @b);',
Uint32 = 'lua_pushinteger(L, @b);',
SDL_GrabMode = 'lua_pushinteger(L, @b);',
SDL_GLattr = 'luaL_checkinteger(L, @n)',
float = 'lua_pushnumber(L, @b);',
double = 'lua_pushnumber(L, @b);',
['const char*'] = 'lua_pushstring(L, @b);',
void = '@v;',
}
Assert0 = '(void)(@v && luaL_error(L, "%s", SDL_GetError()));'
AssertTrue = '(void)(@v || luaL_error(L, "%s", SDL_GetError()));'
BoolPush = 'lua_pushboolean(L, @b);'
BoolCheck = 'checkboolean(L, @n)'
io.write([[
/* auto-generated by gengl.lua */
#include <string.h>
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include "glproto.h"
static GLboolean checkboolean(lua_State *L, int narg) {
]].. --[=[ TODO: this may not be right. can SDL funcs handle integers other than 1?
(this was made like this because SDL_EnableUNICODE, SDL_WM_GrabInput and others
use 0 and 1 (false and true) for setting states and -1 for querying.) ]=] [[
return lua_isnumber(L, narg) ? lua_tointeger(L, narg) : lua_toboolean(L, narg);
}
static int wrapped_seek(SDL_RWops *my, int offset, int whence) {
SDL_RWops *orig = (SDL_RWops *)my->hidden.mem.here;
if(!orig) return -1;
return SDL_RWseek(orig, offset, whence);
}
static int wrapped_read(SDL_RWops *my, void *ptr, int size, int maxnum) {
SDL_RWops *orig = (SDL_RWops *)my->hidden.mem.here;
if(!orig) return -1;
return SDL_RWread(orig, ptr, size, maxnum);
}
static int wrapped_write(SDL_RWops *my, const void *ptr, int size, int maxnum) {
SDL_RWops *orig = (SDL_RWops *)my->hidden.mem.here;
if(!orig) return -1;
return SDL_RWwrite(orig, ptr, size, maxnum);
}
static int wrapped_close(SDL_RWops *my) {
if(!my) return 0;
SDL_RWops *orig = (SDL_RWops *)my->hidden.mem.here;
if(!orig) return -1;
if(SDL_RWclose(orig) != 0) return -1;
my->hidden.mem.here = NULL; // forget wrapper->origRWops link
if(my->hidden.mem.base) // forget userdata->wrapper link
*(SDL_RWops **)my->hidden.mem.base = NULL;
my->hidden.mem.base = NULL; // forget wrapper->userdata link
SDL_FreeRW(my);
return 0;
}
SDL_RWops *checkrwops (lua_State *L, int narg) {
SDL_RWops **r = (SDL_RWops **) luaL_checkudata(L, narg, "SDL_RWops");
luaL_argcheck(L, *r, narg, "RWops already closed");
return *r;
}
void disown_rwops(SDL_RWops **handle) {
if(!*handle) return;
(*handle)->hidden.mem.base = NULL; // forget wrapper->userdata link
*handle = NULL; // forget userdata->wrapper link
}
static SDL_RWops **wrap_rwops(lua_State *L, SDL_RWops *orig) {
SDL_RWops *wrapper = SDL_AllocRW();
if(wrapper == NULL) luaL_error(L, "out of memory");
SDL_RWops **handle = (SDL_RWops **) lua_newuserdata(L, sizeof(SDL_RWops *));
*handle = wrapper; // make userdata->wrapper link
luaL_getmetatable(L, "SDL_RWops"); lua_setmetatable(L, -2);
wrapper->seek = wrapped_seek;
wrapper->read = wrapped_read;
wrapper->write = wrapped_write;
wrapper->close = wrapped_close;
wrapper->hidden.mem.base = (void *)handle; // make wrapper->origRWops link
wrapper->hidden.mem.here = (void *)orig; // make wrapper->userdata link
return handle;
}
SDL_Surface **wrap_surface (lua_State *L, SDL_Surface *surface) {
SDL_Surface **handle = (SDL_Surface **) lua_newuserdata(L, sizeof(SDL_Surface *));
*handle = surface;
luaL_getmetatable(L, "SDL_Surface"); lua_setmetatable(L, -2);
return handle;
}
Mix_Music **wrap_music (lua_State *L, Mix_Music *music) {
Mix_Music **handle = (Mix_Music **) lua_newuserdata(L, sizeof(SDL_Surface *));
*handle = music;
luaL_getmetatable(L, "Mix_Music"); lua_setmetatable(L, -2);
return handle;
}
Mix_Chunk **wrap_chunk (lua_State *L, Mix_Chunk *chunk) {
Mix_Chunk **handle = (Mix_Chunk **) lua_newuserdata(L, sizeof(Mix_Chunk *));
*handle = chunk;
luaL_getmetatable(L, "Mix_Chunk"); lua_setmetatable(L, -2);
return handle;
}
static int _newmem_close(SDL_RWops *rw) {
if(rw) {
if(rw->hidden.mem.base)
free(rw->hidden.mem.base);
rw->hidden.mem.base = NULL;
SDL_FreeRW(rw);
}
return 0;
}
SDL_RWops* wrapRWFromNewMem(size_t size) {
SDL_RWops *rw;
void *mem = malloc(size);
if(mem == NULL) return NULL;
memset(mem, 0, size);
rw = SDL_RWFromMem(mem, size);
if(rw == NULL) { free(mem); return NULL; }
rw->close = _newmem_close;
return rw;
}
SDL_Rect *checkrect(lua_State *L, int narg, SDL_Rect *r) {
luaL_checktype(L, narg, LUA_TTABLE);
lua_rawgeti(L, narg, 1); r->x = luaL_checkinteger(L, -1);
lua_rawgeti(L, narg, 2); r->y = luaL_checkinteger(L, -1);
lua_rawgeti(L, narg, 3); r->w = luaL_optinteger(L, -1, 0);
lua_rawgeti(L, narg, 4); r->h = luaL_optinteger(L, -1, 0);
lua_pop(L, 4);
return r;
}
#define evfield(name, val) (lua_pushinteger(L, event->val), lua_setfield(L, -2, #name))
void pushevent (lua_State *L, SDL_Event *event) {
lua_newtable(L);
evfield(type, type);
switch(event->type) {
case SDL_ACTIVEEVENT:
lua_pushboolean(L, event->active.gain); lua_setfield(L, -2, "gain");
evfield(state, active.state);
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
evfield(sym, key.keysym.sym);
evfield(mod, key.keysym.mod);
evfield(unicode, key.keysym.unicode);
break;
case SDL_MOUSEMOTION:
evfield(x, motion.x); evfield(y, motion.y);
evfield(xrel, motion.xrel); evfield(yrel, motion.yrel);
evfield(buttons, motion.state);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
evfield(button, button.button);
evfield(x, button.x); evfield(y, button.y);
break;
case SDL_VIDEORESIZE:
evfield(w, resize.w); evfield(h, resize.h);
break;
default:
/* the other event types don't have any values */
break;
}
}
static Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
/* this function is from the SDL docs available at www.libsdl.org */
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1: return *p;
case 2: return *(Uint16 *)p;
case 3: if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4: return *(Uint32 *)p;
default: return 0; /* shouldn't happen, but avoids warnings */
}
}
static int wrapSetVideoMode(int w, int h, int fullscreen) {
if(SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0)) == NULL)
return 0;
/* TODO:
* gl* functions don't work before SetVideoMode initializes GL context.
* however, they fail without any warning or error report. fix that.
*/
if(!glproto_connect()) return 0;
return 1;
}
static SDL_Surface* wrapCreateRGBSurface(int w, int h) {
return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, 0xFF0000, 0xFF00, 0xFF, 0xFF000000);
}
static int wrapFillRect(SDL_Surface *s, int r, int g, int b, SDL_Rect *rect) {
return SDL_FillRect(s, rect, SDL_MapRGB(s->format, r, g, b));
}
static int wrapglTexImage2D (SDL_Surface *s, int want_mipmap) {
/* NOTE: since glTexImage2D expect image data to be from bottom to top, I can't just supply surface->pixels.
* (Many people work around it by transforming the texture matrix, but I don't like that workaround.)
* Thus, I manually convert the data to the format OpenGL wants.
* (This also has the advantage that I can use any pixel formats I want, including BPP<3, because I use
* SDL's own SDL_GetRGB functions and don't access surface->pixels directly.)
*/
/* I found the basic idea for the code here:
* http://www.nabble.com/SDL_Surface---3E-OpenGL-texture-only-works-with-RGBA--to14212148.html
*/
int alpha = (s->format->BytesPerPixel == 4);
int texture_type = alpha ? GL_RGBA : GL_RGB;
int x, y;
int pos = 0;
int lock = SDL_MUSTLOCK(s);
GLubyte *data = (GLubyte *)malloc(s->w * s->h * (alpha ? 4 : 3));
if(data == NULL) { SDL_SetError("out of memory"); return -1; }
if((s->w & (s->w - 1)) != 0) {
SDL_SetError("surface's width isn't a power of two");
return -1;
}
if((s->h & (s->h - 1)) != 0) {
SDL_SetError("surface's height isn't a power of two");
return -1;
}
if(lock) SDL_LockSurface(s);
for(y = s->h - 1; y >= 0; y--) {
for(x = 0; x < s->w; x++) {
if(alpha) {
SDL_GetRGBA(getpixel(s, x, y), s->format, &data[pos+0], &data[pos+1], &data[pos+2], &data[pos+3]);
pos += 4;
} else {
SDL_GetRGB(getpixel(s, x, y), s->format, &data[pos+0], &data[pos+1], &data[pos+2]);
pos += 3;
}
}
}
if(lock) SDL_UnlockSurface(s);
glTexImage2D(GL_TEXTURE_2D, 0, texture_type, s->w, s->h, 0, texture_type, GL_UNSIGNED_BYTE, data);
if(want_mipmap)
gluBuild2DMipmaps(GL_TEXTURE_2D, texture_type, s->w, s->h, texture_type, GL_UNSIGNED_BYTE, data);
free((void *)data);
return 0;
}
static int is_memrwops(SDL_RWops *rw) {
static int (*mem_seek)(struct SDL_RWops *, int, int) = NULL;
if (mem_seek == NULL) {
SDL_RWops *memrw = SDL_RWFromConstMem(NULL, 0);
mem_seek = memrw->seek;
SDL_RWclose(memrw);
}
return (rw->seek == mem_seek);
}
static int rwops_baseptr(lua_State *L) {
SDL_RWops *rw = checkrwops(L, 1);
while(rw->close == wrapped_close) rw = (SDL_RWops *)rw->hidden.mem.here;
luaL_argcheck(L, is_memrwops(rw), 1, "expected memory buffer RWops");
lua_pushlightuserdata(L, rw->hidden.mem.base);
return 1;
}
static int rwops_hereptr(lua_State *L) {
SDL_RWops *rw = checkrwops(L, 1);
while(rw->close == wrapped_close) rw = (SDL_RWops *)rw->hidden.mem.here;
luaL_argcheck(L, is_memrwops(rw), 1, "expected memory buffer RWops");
lua_pushlightuserdata(L, rw->hidden.mem.here);
return 1;
}
static int rwops_stopptr(lua_State *L) {
SDL_RWops *rw = checkrwops(L, 1);
while(rw->close == wrapped_close) rw = (SDL_RWops *)rw->hidden.mem.here;
luaL_argcheck(L, is_memrwops(rw), 1, "expected memory buffer RWops");
lua_pushlightuserdata(L, rw->hidden.mem.stop);
return 1;
}
const void* check_const_ptr(lua_State *L, int narg) {
if(lua_isstring(L, narg))
return (const void *)lua_tolstring(L, narg, NULL);
luaL_argcheck(L, lua_type(L, narg) == LUA_TLIGHTUSERDATA, narg, "expected pointer");
return lua_touserdata(L, narg);
}
void *check_ptr(lua_State *L, int narg) {
luaL_argcheck(L, lua_type(L, narg) == LUA_TLIGHTUSERDATA, narg, "expected pointer");
return lua_touserdata(L, narg);
}
int rwops_disown_fn(lua_State *L) {
disown_rwops((SDL_RWops **) luaL_checkudata(L, 1, "SDL_RWops"));
return 0;
}
static size_t rw_maxlen(SDL_RWops *rw) {
size_t current = SDL_RWtell(rw);
size_t maxlen = SDL_RWseek(rw, 0, RW_SEEK_END) - current;
SDL_RWseek(rw, current, RW_SEEK_SET);
return maxlen;
}
static int rwops_read (lua_State *L) {
SDL_RWops *rw = checkrwops(L, 1);
size_t len, nread;
void *s;
len = luaL_optinteger(L, 2, rw_maxlen(rw));
s = malloc(len * sizeof(char));
if(s == NULL) luaL_error(L, "out of memory");
memset(s, 0, len * sizeof(char));
nread = SDL_RWread(rw, s, sizeof(char), len);
lua_pushinteger(L, nread);
if(nread != len) {
lua_pushnil(L);
lua_pushstring(L, SDL_GetError());
lua_pushlstring(L, (const char *)s, nread);
free(s);
return 3;
}
lua_pushlstring(L, (const char *)s, nread);
free(s);
return 1;
}
static int rwops_write (lua_State *L) {
SDL_RWops *rw = checkrwops(L, 1);
size_t len, nwrote;
const char *s;
if(lua_type(L, 2) == LUA_TLIGHTUSERDATA) {
s = (const char *)lua_touserdata(L, 2);
len = luaL_checkinteger(L, 3);
}
else {
s = luaL_checklstring(L, 2, &len);
len = luaL_optinteger(L, 3, len);
}
nwrote = SDL_RWwrite(rw, s, 1, len);
if(nwrote != len) {
lua_pushnil(L);
lua_pushstring(L, SDL_GetError());
lua_pushinteger(L, nwrote);
return 3;
}
lua_pushboolean(L, 1);
return 1;
}
static int rwops_seek(lua_State *L) {
static const int mode[] = {RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END};
static const char *const modenames[] = {"set", "cur", "end", NULL};
SDL_RWops *rw = checkrwops(L, 1);
int op = luaL_checkoption(L, 2, "cur", modenames);
int result = SDL_RWseek(rw, luaL_optlong(L, 3, 0), mode[op]);
if(result < 0) {
lua_pushnil(L);
lua_pushstring(L, SDL_GetError());
return 2;
}
lua_pushinteger(L, result);
return 1;
}
static int rwops_gc (lua_State *L) {
SDL_RWops **r = (SDL_RWops **) luaL_checkudata(L, 1, "SDL_RWops");
if(r && *r) {
SDL_RWclose(*r);
*r = NULL;
}
return 0;
}
]])
local b = {}; setmetatable(b, { __index = function (_, fn)
return function (...) io.write(Bind(fn, ...), "\n\n") end
end })
b.SDL_Init{}
b.SDL_Quit{}
b.SDL_GL_SwapBuffers{}
b.SDL_GetTicks{}
b.SDL_Delay{}
b.SDL_WM_GrabInput{}
b.SDL_WM_SetCaption{ [1] = { opt = 'NULL' }, [2] = { opt = 'NULL' } }
b.SDL_WM_SetIcon{ [2] = { check_func = 'NULL' } --[[ TODO make variable second argument) ]] }
b.SDL_WM_ToggleFullScreen{ return_code = AssertTrue, [1] = { check_func = 'SDL_GetVideoSurface()' } }
b.SDL_EnableUNICODE{ return_code = BoolPush, [1] = { check_func = BoolCheck } }
b.SDL_EnableKeyRepeat{ return_code = Assert0 }
b.SDL_WarpMouse{}
b.SDL_ShowCursor{ return_code = BoolPush, [1] = { check_func = BoolCheck } }
b.SDL_GetMouseState{ Returns=1 }
b.SDL_PollEvent{ [1] = { check_func = '&e' }, return_code = 'SDL_Event e;'..
'\nif(@b == 0)\n lua_pushnil(L);\nelse\n pushevent(L, &e);' }
b.SDL_WaitEvent{ [1] = { check_func = '&e' }, return_code = 'SDL_Event e;'..
'\n(void)(@b || luaL_error(L, "%s", SDL_GetError()));\npushevent(L, &e);' }
b.SDL_PumpEvents{}
b.wrapCreateRGBSurface{
prototype = { return_type = 'SDL_Surface*', argtypes = { 'int', 'int' } },
return_code = 'SDL_Surface *s = @b;\n(void)(s || luaL_error(L, "%s", SDL_GetError()));\nwrap_surface(L, s);' }
b.SDL_GL_SetAttribute{ return_code = Assert0 }
b.SDL_GL_GetAttribute{ return_code = Assert0, Returns=1 }
b.wrapSetVideoMode{ return_code = AssertTrue, prototype = { return_type = 'int', argtypes = { 'int', 'int', 'int' } },
[3] = { check_func = BoolCheck, opt = 0 } }
b.SDL_RWFromFile{ return_code =
'SDL_RWops *rw = @b;\n(void)(rw || luaL_error(L, "%s", SDL_GetError()));\nwrap_rwops(L, rw);' }
b.SDL_RWFromMem{ [1] = { check_func = 'check_ptr(L, @n)' }, return_code =
'SDL_RWops *rw = @b;\n(void)(rw || luaL_error(L, "%s", SDL_GetError()));\nwrap_rwops(L, rw);' }
b.wrapRWFromNewMem{ prototype = { return_type = 'SDL_RWops*', argtypes = { 'size_t' } }, return_code =
'SDL_RWops *rw = @b;\n(void)(rw || luaL_error(L, "out of memory"));\nwrap_rwops(L, rw);' }
b.Mix_OpenAudio{ return_code = Assert0 }
b.Mix_CloseAudio{}
b.Mix_HaltMusic{ return_code = Assert0 }
b.Mix_PauseMusic{}
b.Mix_ResumeMusic{}
b.Mix_RewindMusic{}
b.Mix_SetMusicPosition{ return_code = Assert0 }
b.Mix_PausedMusic{ return_code = BoolPush }
b.Mix_PlayingMusic{ return_code = BoolPush }
b.Mix_VolumeMusic{ [1] = { opt = '-1' } }
b.Mix_PlayChannelTimed{ [1] = { opt = -1 }, [3] = { opt = 0 }, [4] = { opt = -1 }, return_code =
'int r = @b;\n(void)(r == -1 && luaL_error(L, "%s", SDL_GetError()));\nlua_pushinteger(L, r);' }
b.Mix_FadeInChannelTimed{ [1] = { opt = -1 }, [3] = { opt = 0 }, [5] = { opt = -1 }, return_code =
'int r = @b;\n(void)(r == -1 && luaL_error(L, "%s", SDL_GetError()));\nlua_pushinteger(L, r);' }
b.Mix_AllocateChannels{ [1] = { opt = -1 } }
b.Mix_Volume{ [1] = { opt = -1 }, [2] = { opt = -1 } }
b.Mix_VolumeChunk{ [2] = { opt = -1 } }
b.Mix_FadeOutChannel{ [1] = { opt = -1 } }
b.Mix_FadingChannel{ return_code = return_codes.int } -- TODO: make sure enum Mix_Fading is available
b.Mix_Pause{}
b.Mix_Resume{}
b.Mix_Paused{ return_code = BoolPush }
b.Mix_Playing{ return_code = BoolPush }
b.Mix_HaltChannel{ return_code = '@v;' }
b.Mix_ExpireChannel{ [1] = { opt = -1 } }
b.Mix_ReserveChannels{}
b.Mix_GroupChannel{ [2] = { opt = -1 }, return_code = AssertTrue }
b.Mix_GroupChannels{ [3] = { opt = -1 }, return_code = AssertTrue }
b.Mix_GroupCount{ [1] = { opt = -1 } }
b.Mix_GroupAvailable{ [1] = { opt = -1 } }
b.Mix_GroupOldest{ [1] = { opt = -1 } }
b.Mix_GroupNewer{ [1] = { opt = -1 } }
b.Mix_FadeOutGroup{ [1] = { opt = -1 } }
b.Mix_HaltGroup{ return_code = '@v;' }
b.IMG_Load_RW{ [2] = { check_func = BoolCheck },
return_code = 'SDL_Surface *s = @b;\n(void)(s || luaL_error(L, "%s", SDL_GetError()));\nwrap_surface(L, s);' }
b.Mix_LoadMUS_RW{ return_code = 'Mix_Music *m = @b;\n(void)(m || luaL_error(L, "%s", SDL_GetError())); wrap_music(L, m);' }
b.Mix_LoadWAV_RW{ [2] = { check_func = BoolCheck },
return_code = 'Mix_Chunk *c = @b;\n(void)(c || luaL_error(L, "%s", SDL_GetError())); wrap_chunk(L, c);' }
b.wrapFillRect{ [5] = { opt = 'NULL', check_func = 'checkrect(L, @n, &r)' }, return_code = 'SDL_Rect r;\n'..Assert0,
prototype = { return_type = 'int', argtypes = { 'SDL_Surface*', 'int', 'int', 'int', 'SDL_Rect*' } } }
b.SDL_SetAlpha{}
b.SDL_w{ base_call = '(@a)->w', prototype = { return_type = 'int', argtypes = { 'SDL_Surface*' } } }
b.SDL_h{ base_call = '(@a)->h', prototype = { return_type = 'int', argtypes = { 'SDL_Surface*' } } }
b.SDL_BlitSurface{ return_code = 'SDL_Rect rect1, rect2;\n'..Assert0,
prototype = { return_type = 'int', argtypes = { 'SDL_Surface*', 'SDL_Rect*', 'SDL_Surface*', 'SDL_Rect*' } },
[2] = { opt = 'NULL', check_func = 'checkrect(L, @n, &rect1)' },
[4] = { opt = 'NULL', check_func = 'checkrect(L, @n, &rect2)' } }
b.wrapglTexImage2D{ return_code = Assert0, prototype = { return_type = 'int', argtypes = { 'SDL_Surface*', 'int' } }, [2] = { check_func = BoolCheck } --[[ TODO arg2 default=false (I think) ]] }
b.SDL_FreeSurface{ base_call = 'SDL_Surface *s = @a; if(s && (SDL_GetVideoSurface() != s)) SDL_FreeSurface(s)' }
b.Mix_FreeChunk{}
b.Mix_FadeInMusicPos{ return_code = Assert0, [2] = { opt = 0 }, [3] = { opt = 0 }, [4] = { opt='0.0' } }
b.Mix_FreeMusic{}
local nobind = { Mix_FreeMusic=true, Mix_FreeChunk=true, SDL_FreeSurface=true, IMG_Load_RW=true }
local function regitem(n,v) io.write(' { "', n, '", ', v, ' },\n') end
io.write[[
int luaopen_SDL(lua_State *L) {
int i;
const luaL_Reg reg_SDL[] = {
]]
for _,f in ipairs(bindlist) do if not nobind[f] then regitem(f:sub(5), 'b_'..f) end end
io.write[[
{ "IMG_Load_RW", b_IMG_Load_RW },
{ "PlayMusic", b_Mix_FadeInMusicPos },
{ "FadeInMusic", b_Mix_FadeInMusicPos },
{ "PlayChannel", b_Mix_PlayChannelTimed },
{ "FadeInChannel", b_Mix_FadeInChannelTimed },
{ NULL, NULL }
};
const luaL_Reg reg_RWops[] = {
{ "read", rwops_read },
{ "write", rwops_write },
{ "seek", rwops_seek },
{ "close", rwops_gc },
{ "disown", rwops_disown_fn },
{ "baseptr", rwops_baseptr },
{ "hereptr", rwops_hereptr },
{ "stopptr", rwops_stopptr },
{ "__gc", rwops_gc },
{ NULL, NULL }
};
struct {
const char *name;
lua_Integer value;
} const_sdl[] = {
]]
for c in Input:gmatch('\n#%s*define%s+SDL_[%w_]*%s+%d+[^\n]*') do
regitem(c:match('SDL_([%w_]*)'), c:match('SDL_[%w_]*%s*(.*)')) end
for c in Input:gmatch('\n#%s*define%s+KMOD_[%w_]*[^\n]*') do
regitem(c:match('KMOD_[%w_]*'), c:match('KMOD_[%w_]*%s*(.*)')) end
for c in Input:gmatch('\n#%s*define%s+AUDIO_[%w_]*[^\n]*') do
regitem(c:match('AUDIO_[%w_]*'), c:match('AUDIO_[%w_]*%s*(.*)')) end
for e in Input:gsub('\n#[^\n]*', ''):gmatch('%Wenum%s*[%w_]*%s*(%b{})') do
for c in e:gsub('^', ','):gmatch(',%s*([%w_]+)') do
regitem(c:gsub('^SDL_?', ''), c) end end
io.write[[
{ NULL, 0 }
};
void *quit_ud;
lua_settop(L, 0);
quit_ud = lua_newuserdata(L, 1);
lua_newtable(L);
lua_pushcfunction(L, b_SDL_Quit);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -2);
lua_setfield(L, LUA_REGISTRYINDEX, "SDL_Quit");
quit_ud = lua_newuserdata(L, 1);
lua_newtable(L);
lua_pushcfunction(L, b_Mix_CloseAudio);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -2);
lua_setfield(L, LUA_REGISTRYINDEX, "Mix_CloseAudio");
luaL_register(L, "SDL", reg_SDL);
for(i = 0; const_sdl[i].name; i++) {
lua_pushinteger(L, const_sdl[i].value);
lua_setfield(L, 1, const_sdl[i].name);
}
luaL_newmetatable(L, "SDL_Surface");
lua_pushvalue(L, 1); lua_setfield(L, -2, "__index"); /* metatable.__index = SDL */
lua_pushcfunction(L, b_SDL_FreeSurface); lua_setfield(L, -2, "__gc");
luaL_newmetatable(L, "Mix_Music");
lua_pushvalue(L, 1); lua_setfield(L, -2, "__index");
lua_pushcfunction(L, b_Mix_FreeMusic); lua_setfield(L, -2, "__gc");
luaL_newmetatable(L, "Mix_Chunk");
lua_pushvalue(L, 1); lua_setfield(L, -2, "__index");
lua_pushcfunction(L, b_Mix_FreeChunk); lua_setfield(L, -2, "__gc");
luaL_newmetatable(L, "SDL_RWops");
lua_pushvalue(L, -1); lua_setfield(L, -2, "__index");
luaL_register(L, NULL, reg_RWops);
lua_settop(L, 1);
return 1;
}
]]
end
local headers = { 'SDL.h', 'SDL_main.h', 'SDL_stdinc.h', 'SDL_active.h',
'SDL_audio.h', 'SDL_cdrom.h', 'SDL_cpuinfo.h', 'SDL_endian.h',
'SDL_error.h', 'SDL_events.h', 'SDL_joystick.h', 'SDL_keyboard.h',
'SDL_keysym.h', 'SDL_loadso.h', 'SDL_mouse.h', 'SDL_mutex.h',
'SDL_rwops.h', 'SDL_thread.h', 'SDL_timer.h', 'SDL_video.h',
'SDL_version.h', 'SDL_quit.h', 'SDL_image.h', 'SDL_mixer.h' }
local include_path = parse_include_path_args(arg)
include_path[#include_path+1] = '/usr/local/include'
include_path[#include_path+1] = '/usr/include'
local content = {}
for _,header in ipairs(headers) do
local locations = {}
for _,dir in ipairs(include_path) do
locations[#locations+1] = dir..'/'..header end
content[#content+1] = assert(try_file_locations(locations))
end
parse_headers(table.concat(content, '\n'))
write_output_lsdllib()
|