summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2025-08-06 11:36:51 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2025-08-06 11:36:51 +0200
commit116124155ce9a36b50c0b606154547fffe549e49 (patch)
tree69bbf9f49fb9233222065f6542acc08701b2e706 /main.c
parent4c8e0f6f28ce3a5b6729ddccfeeb5445e9ec9605 (diff)
downloadbidi-116124155ce9a36b50c0b606154547fffe549e49.tar.gz
Added color stdlib module and compilation step for embedding lua stdlib
Diffstat (limited to 'main.c')
-rw-r--r--main.c39
1 files changed, 35 insertions, 4 deletions
diff --git a/main.c b/main.c
index 79bcfb9..7515e57 100644
--- a/main.c
+++ b/main.c
@@ -5,9 +5,25 @@
#include "lualib.h"
#include "lauxlib.h"
+#include "stdlib/color.h"
+
#define IN_FILE "test/main.lua"
#define DEBUG_LEVEL LOG_DEBUG
+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 l_open_window(lua_State *L) {
int width = luaL_checknumber(L, 1);
int height = luaL_checknumber(L, 2);
@@ -18,8 +34,9 @@ static int l_open_window(lua_State *L) {
return 0;
}
-static int l_window_should_close(lua_State *L) {
- lua_pushboolean(L, WindowShouldClose());
+// 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;
}
@@ -47,7 +64,14 @@ static int l_end_drawing(lua_State *L) {
}
static int l_clear_window(lua_State *L) {
- ClearBackground(BLACK);
+ 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;
}
@@ -64,13 +88,20 @@ int main(void) {
lua_register(L, "open_window", l_open_window);
lua_register(L, "close_window", l_close_window);
- lua_register(L, "window_should_close", l_window_should_close);
+ 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, "draw_fps", l_draw_fps);
lua_register(L, "clear_window", l_clear_window);
+ if (luaL_loadbuffer(L, color, color_len, "color") || lua_pcall(L, 0, 1, 0)) {
+ fprintf(stderr, "Error loading color.lua: %s\n", lua_tostring(L, -1));
+ lua_close(L);
+ return 1;
+ }
+ lua_setglobal(L, "color");
+
// TODO: This should probably use loadbuffer instead.
// https://www.lua.org/manual/5.4/manual.html#luaL_loadbuffer
if (luaL_loadfile(L, IN_FILE) || lua_pcall(L, 0, 0, 0)) {