From 3441dd266f671e43770986be8bf994dcad32ac72 Mon Sep 17 00:00:00 2001 From: Reuh Date: Tue, 18 Aug 2015 16:19:11 +0200 Subject: [PATCH] Added ctr.gfx.color lib; color.setDefault and color.getDefault working --- sdcard/ctruLua/main.lua | 5 +++++ source/color.c | 33 +++++++++++++++++++++++++++++++++ source/gfx.c | 17 +++++++++++++++-- 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 source/color.c diff --git a/sdcard/ctruLua/main.lua b/sdcard/ctruLua/main.lua index 3655b8a..a688ecf 100644 --- a/sdcard/ctruLua/main.lua +++ b/sdcard/ctruLua/main.lua @@ -15,8 +15,13 @@ while os.run() do if keys.held.down then y = y + 1 end gfx.startFrame() + + gfx.color.setDefault(0xFF0000FF) gfx.rectangle(x, y, 10, 10) + + gfx.color.setDefault(0x00FFFFFF) gfx.rectangle(240, 150, 120, 10) + gfx.endFrame() gfx.render() diff --git a/source/color.c b/source/color.c new file mode 100644 index 0000000..516ce04 --- /dev/null +++ b/source/color.c @@ -0,0 +1,33 @@ +#include + +#include +#include + +u32 color_default = RGBA8(255, 255, 255, 255); + +static int color_setDefault(lua_State *L) { + color_default = luaL_checkinteger(L, 1); + + return 0; +} + +static int color_getDefault(lua_State *L) { + lua_pushinteger(L, color_default); + + return 1; +} + +static const struct luaL_Reg color_lib[] = { + { "setDefault", color_setDefault }, + { "getDefault", color_getDefault }, + { NULL, NULL } +}; + +int luaopen_color_lib(lua_State *L) { + luaL_newlib(L, color_lib); + return 1; +} + +void load_color_lib(lua_State *L) { + luaL_requiref(L, "ctr.gfx.color", luaopen_color_lib, false); +} \ No newline at end of file diff --git a/source/gfx.c b/source/gfx.c index f29c4d8..f6dac1b 100644 --- a/source/gfx.c +++ b/source/gfx.c @@ -3,7 +3,9 @@ #include #include -u32 defaultColor = RGBA8(255, 255, 255, 255); +int load_color_lib(lua_State *L); + +u32 color_default; static int gfx_startFrame(lua_State *L) { sf2d_start_frame(GFX_TOP, GFX_LEFT); @@ -28,7 +30,7 @@ static int gfx_rectangle(lua_State *L) { int y = luaL_checkinteger(L, 2); int width = luaL_checkinteger(L, 3); int height = luaL_checkinteger(L, 4); - u32 color = luaL_optinteger(L, 5, defaultColor); + u32 color = luaL_optinteger(L, 5, color_default); sf2d_draw_rectangle(x, y, width, height, color); @@ -43,8 +45,19 @@ static const struct luaL_Reg gfx_lib[] = { { NULL, NULL } }; +struct { char *name; int (*load)(lua_State *L); } gfx_libs[] = { + { "color", load_color_lib }, + { NULL, NULL } +}; + int luaopen_gfx_lib(lua_State *L) { luaL_newlib(L, gfx_lib); + + for (int i = 0; gfx_libs[i].name; i++) { + gfx_libs[i].load(L); + lua_setfield(L, -2, gfx_libs[i].name); + } + return 1; }