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
|
#include <stdio.h>
#include <stdarg.h>
#include <getopt.h>
#include <stdlib.h>
#include <time.h>
#include "raylib.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "stdlib/json.h"
#include "stdlib/color.h"
#include "stdlib/button.h"
#include "stdlib/tilemap.h"
#include "fonts/dejavusans_mono_bold.h"
#define VERSION "x.x"
#define DEBUG_LEVEL LOG_DEBUG
#define FONT_IMPORT_SIZE 30
#define UID_LENGTH 36
typedef struct {
char uid[UID_LENGTH + 1];
const char* filepath;
} ExternalImage;
typedef struct {
} ExternalAudio;
typedef struct {
Font font;
int font_size;
Camera2D camera;
ExternalImage *images;
ExternalAudio *audio;
} Context;
// Setting up global context.
Context ctx = {0};
static void generate_uid(char *uid) {
const char *chars = "0123456789abcdef";
for (size_t i = 0; i < UID_LENGTH; i++) {
uid[i] = chars[rand() % 16];
}
uid[UID_LENGTH + 1] = '\0';
}
static int lua_getfield_int(lua_State *L, int index, const char *key) {
lua_getfield(L, index, key);
int val = (int)luaL_checknumber(L, -1);
lua_pop(L, 1);
return val;
}
static int lua_getfield_int_opt(lua_State *L, int index, const char *key, int def) {
lua_getfield(L, index, key);
int val = lua_isnil(L, -1) ? def : (int)luaL_checknumber(L, -1);
lua_pop(L, 1);
return val;
}
static int load_embedded_module(lua_State *L, const char *module_code, size_t code_len, const char *module_name) {
if (luaL_loadbuffer(L, module_code, code_len, module_name) || lua_pcall(L, 0, 1, 0)) {
fprintf(stderr, "Error loading %s: %s\n", module_name, lua_tostring(L, -1));
return 0;
}
lua_setglobal(L, module_name);
return 1;
}
static int l_open_window(lua_State *L) {
int width = luaL_checknumber(L, 1);
int height = luaL_checknumber(L, 2);
const char *title = luaL_checkstring(L, 3);
SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
InitWindow(width, height, title);
TraceLog(LOG_DEBUG, "l_open_window");
ctx.font_size = FONT_IMPORT_SIZE;
ctx.font = LoadFontFromMemory(".ttf", dejavusans_mono_bold, dejavusans_mono_bold_len, ctx.font_size, NULL, 0);
SetTextureFilter(ctx.font.texture, TEXTURE_FILTER_TRILINEAR);
if (!IsFontValid(ctx.font)) {
printf("font not valid\n");
}
return 0;
}
// TODO: This function name is still a bit sus. Revisit the name later.
static int l_window_running(lua_State *L) {
lua_pushboolean(L, !WindowShouldClose());
return 1;
}
static int l_set_fps(lua_State *L) {
int fps = luaL_checknumber(L, 1);
SetTargetFPS(fps);
TraceLog(LOG_DEBUG, "l_set_fps");
return 0;
}
static int l_get_dt(lua_State *L) {
lua_pushnumber(L, GetFrameTime());
return 1;
}
static int l_get_fps(lua_State *L) {
lua_pushnumber(L, GetFPS());
return 1;
}
static int l_close_window(lua_State *L) {
CloseWindow();
TraceLog(LOG_DEBUG, "l_close_window");
return 0;
}
static int l_begin_drawing(lua_State *L) {
BeginDrawing();
return 0;
}
static int l_end_drawing(lua_State *L) {
EndDrawing();
return 0;
}
static int l_clear_window(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
Color color = {
.r = (unsigned char)lua_getfield_int(L, 1, "r"),
.g = (unsigned char)lua_getfield_int(L, 1, "g"),
.b = (unsigned char)lua_getfield_int(L, 1, "b"),
.a = (unsigned char)lua_getfield_int_opt(L, 1, "a", 255)
};
ClearBackground(color);
return 0;
}
static int l_draw_info(lua_State *L) {
float delta = GetFrameTime();
int fps = GetFPS();
double runtime = GetTime();
int height = GetScreenHeight();
DrawTextEx(ctx.font, TextFormat("fps: %d", fps), (Vector2){ 20, height - 80 }, 20, 0, RAYWHITE);
DrawTextEx(ctx.font, TextFormat("run: %f", runtime), (Vector2){ 20, height - 60 }, 20, 0, RAYWHITE);
DrawTextEx(ctx.font, TextFormat("dt: %f", delta), (Vector2){ 20, height - 40 }, 20, 0, RAYWHITE);
return 0;
}
static int l_draw_rect(lua_State *L) {
int x = luaL_checknumber(L, 1);
int y = luaL_checknumber(L, 2);
int width = luaL_checknumber(L, 3);
int height = luaL_checknumber(L, 4);
luaL_checktype(L, 5, LUA_TTABLE);
Color color = {
.r = (unsigned char)lua_getfield_int(L, 5, "r"),
.g = (unsigned char)lua_getfield_int(L, 5, "g"),
.b = (unsigned char)lua_getfield_int(L, 5, "b"),
.a = (unsigned char)lua_getfield_int_opt(L, 5, "a", 255)
};
DrawRectangle(x, y, width, height, color);
return 0;
}
static int l_draw_text(lua_State *L) {
const char *text = luaL_checkstring(L, 1);
int x = luaL_checknumber(L, 2);
int y = luaL_checknumber(L, 3);
int size = luaL_checknumber(L, 4);
luaL_checktype(L, 5, LUA_TTABLE);
Color color = {
.r = (unsigned char)lua_getfield_int(L, 5, "r"),
.g = (unsigned char)lua_getfield_int(L, 5, "g"),
.b = (unsigned char)lua_getfield_int(L, 5, "b"),
.a = (unsigned char)lua_getfield_int_opt(L, 5, "a", 255)
};
DrawTextEx(ctx.font, text, (Vector2){ x, y }, size, 0, color);
return 0;
}
static int l_draw_pixel(lua_State *L) {
int x = luaL_checknumber(L, 1);
int y = luaL_checknumber(L, 2);
luaL_checktype(L, 3, LUA_TTABLE);
Color color = {
.r = (unsigned char)lua_getfield_int(L, 3, "r"),
.g = (unsigned char)lua_getfield_int(L, 3, "g"),
.b = (unsigned char)lua_getfield_int(L, 3, "b"),
.a = (unsigned char)lua_getfield_int_opt(L, 3, "a", 255)
};
DrawPixel(x, y, color);
}
static int l_draw_line(lua_State *L) {
int x1 = luaL_checknumber(L, 1);
int y1 = luaL_checknumber(L, 2);
int x2 = luaL_checknumber(L, 3);
int y2 = luaL_checknumber(L, 4);
luaL_checktype(L, 5, LUA_TTABLE);
Color color = {
.r = (unsigned char)lua_getfield_int(L, 5, "r"),
.g = (unsigned char)lua_getfield_int(L, 5, "g"),
.b = (unsigned char)lua_getfield_int(L, 5, "b"),
.a = (unsigned char)lua_getfield_int_opt(L, 5, "a", 255)
};
DrawLine(x1, y1, x2, y2, color);
return 0;
}
static int l_draw_circle(lua_State *L) {
int center_x = luaL_checknumber(L, 1);
int center_y = luaL_checknumber(L, 2);
int radius = luaL_checknumber(L, 3);
luaL_checktype(L, 4, LUA_TTABLE);
Color color = {
.r = (unsigned char)lua_getfield_int(L, 4, "r"),
.g = (unsigned char)lua_getfield_int(L, 4, "g"),
.b = (unsigned char)lua_getfield_int(L, 4, "b"),
.a = (unsigned char)lua_getfield_int_opt(L, 4, "a", 255)
};
DrawCircle(center_x, center_y, radius, color);
return 0;
}
static int l_draw_ellipse(lua_State *L) {
int center_x = luaL_checknumber(L, 1);
int center_y = luaL_checknumber(L, 2);
int radius_h = luaL_checknumber(L, 3);
int radius_v = luaL_checknumber(L, 4);
luaL_checktype(L, 5, LUA_TTABLE);
Color color = {
.r = (unsigned char)lua_getfield_int(L, 5, "r"),
.g = (unsigned char)lua_getfield_int(L, 5, "g"),
.b = (unsigned char)lua_getfield_int(L, 5, "b"),
.a = (unsigned char)lua_getfield_int_opt(L, 5, "a", 255)
};
DrawEllipse(center_x, center_y, radius_h, radius_v, color);
return 0;
}
static int l_draw_triangle(lua_State *L) {
int x1 = luaL_checknumber(L, 1);
int y1 = luaL_checknumber(L, 2);
int x2 = luaL_checknumber(L, 3);
int y2 = luaL_checknumber(L, 4);
int x3 = luaL_checknumber(L, 5);
int y3 = luaL_checknumber(L, 6);
luaL_checktype(L, 7, LUA_TTABLE);
Color color = {
.r = (unsigned char)lua_getfield_int(L, 7, "r"),
.g = (unsigned char)lua_getfield_int(L, 7, "g"),
.b = (unsigned char)lua_getfield_int(L, 7, "b"),
.a = (unsigned char)lua_getfield_int_opt(L, 7, "a", 255)
};
// NOTE: Raylib orders vertices in counter-clockwise order. We order them
// in clockwise order instead to make this a bit easier to use.
Vector2 p1 = { x1, y1 };
Vector2 p2 = { x3, y3 };
Vector2 p3 = { x2, y2 };
DrawTriangle(p1, p2, p3, color);
return 0;
}
static int l_button_pressed(lua_State *L) {
int button = luaL_checknumber(L, 1);
lua_pushboolean(L, IsKeyDown(button));
return 1;
}
static int l_load_image(lua_State *L) {
return 0;
}
static int l_load_audio(lua_State *L) {
return 0;
}
static int l_move_camera(lua_State *L) {
return 0;
}
static void help(const char *argv0) {
printf("Usage: %s [options]\n"
"\nAvailable options:\n"
" -r,--run=file.lua run input file\n"
" -b,--bundle bundles this folder\n"
" -d,--debug prints debug information\n"
" -h,--help this help\n"
" -v,--version show version\n",
argv0);
}
static void version(const char *argv0) {
printf("%s version %s\n", argv0, VERSION);
}
int main(int argc, char *argv[]) {
srand(time(NULL));
TraceLogLevel debug_level = LOG_WARNING;
const char *run_file = NULL;
const char short_options[] = "r:dbhv";
const struct option long_options[] = {
{ "run", 1, NULL, 'r' },
{ "debug", 0, NULL, 'd' },
{ "bundle", 0, NULL, 'b' },
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ 0 },
};
int opt;
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 'r':
run_file = optarg;
break;
case 'b':
printf("TODO: Bundler\n");
return 0;
case 'd':
debug_level = LOG_DEBUG;
break;
case 'h':
help(argv[0]);
return 0;
case 'v':
version(argv[0]);
return 0;
default:
fprintf(stdout, "Missing options. Check help.\n");
return 0;
}
}
if (run_file) {
SetTraceLogLevel(debug_level);
lua_State *L = luaL_newstate();
luaL_openlibs(L);
// Loading embeded modules into Lua state.
if (!load_embedded_module(L, json, json_len, "json")) return 1;
if (!load_embedded_module(L, color, color_len, "color")) return 1;
if (!load_embedded_module(L, button, button_len, "button")) return 1;
if (!load_embedded_module(L, tilemap, tilemap_len, "tilemap")) return 1;
// Registring Raylib mappings.
lua_register(L, "open_window", l_open_window);
lua_register(L, "close_window", l_close_window);
lua_register(L, "window_running", l_window_running);
lua_register(L, "begin_drawing", l_begin_drawing);
lua_register(L, "end_drawing", l_end_drawing);
lua_register(L, "set_fps", l_set_fps);
lua_register(L, "get_fps", l_get_fps);
lua_register(L, "get_dt", l_get_dt);
lua_register(L, "clear_window", l_clear_window);
lua_register(L, "draw_info", l_draw_info);
lua_register(L, "draw_rect", l_draw_rect);
lua_register(L, "draw_text", l_draw_text);
lua_register(L, "draw_pixel", l_draw_pixel);
lua_register(L, "draw_line", l_draw_line);
lua_register(L, "draw_circle", l_draw_circle);
lua_register(L, "draw_ellipse", l_draw_ellipse);
lua_register(L, "draw_triangle", l_draw_triangle);
lua_register(L, "load_image", l_load_image);
lua_register(L, "load_audio", l_load_audio);
lua_register(L, "move_camera", l_move_camera);
lua_register(L, "button_pressed", l_button_pressed);
// Interpreting and running input file Lua script.
if (luaL_loadfile(L, run_file) || lua_pcall(L, 0, 0, 0)) {
fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
return 1;
}
UnloadFont(ctx.font);
lua_close(L);
}
return 0;
}
|