From 42d42c9ba3b741d167eaa2196c686962559686f1 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sat, 31 Oct 2020 22:43:41 -0500 Subject: add basic framebuffer operations --- src/honey.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) (limited to 'src/honey.c') diff --git a/src/honey.c b/src/honey.c index 1328a5f..ce1a38c 100644 --- a/src/honey.c +++ b/src/honey.c @@ -53,6 +53,48 @@ bool honey_parse_options(honey_options* options, int argc, char** argv) return true; } +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +static int honey_lua_clear_color(lua_State* L) +{ + float* color_array; + bool color, depth, stencil; + honey_lua_parse_arguments(L, 4, + HONEY_USERDATA, &color_array, + HONEY_BOOLEAN, &color, + HONEY_BOOLEAN, &depth, + HONEY_BOOLEAN, &stencil); + float r = color_array[0]; + float g = color_array[1]; + float b = color_array[2]; + float a = color_array[3]; + + int clear_flags = 0; + if (color) + clear_flags = clear_flags | GL_COLOR_BUFFER_BIT; + if (depth) + clear_flags = clear_flags | GL_DEPTH_BUFFER_BIT; + if (stencil) + clear_flags = clear_flags | GL_STENCIL_BUFFER_BIT; + + glClearColor(r, g, b, a); + glClear(clear_flags); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_lua_enable_depth_test(lua_State* L) +{ + bool enable; + honey_lua_parse_arguments(L, 1, HONEY_BOOLEAN, &enable); + if (enable) + glEnable(GL_DEPTH_TEST); + else + glDisable(GL_DEPTH_TEST); + return 0; +} + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ bool honey_setup(lua_State** L) @@ -88,6 +130,15 @@ bool honey_setup(lua_State** L) lua_pushcfunction(*L, honey_exit); lua_setfield(*L, -2, "exit"); + lua_pushcfunction(*L, honey_set_framebuffer); + lua_setfield(*L, -2, "set_framebuffer"); + + lua_pushcfunction(*L, honey_lua_clear_color); + lua_setfield(*L, -2, "clear_color"); + + lua_pushcfunction(*L, honey_lua_enable_depth_test); + lua_setfield(*L, -2, "enable_depth_test"); + lua_setglobal(*L, "honey"); return true; @@ -145,8 +196,6 @@ bool honey_run(lua_State* L, honey_options opts) { if (drawTime > 0.016) { drawTime -= 0.016; - glClearColor(0,0,0,1); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (draw_callback != LUA_NOREF) { lua_rawgeti(L, LUA_REGISTRYINDEX, draw_callback); @@ -185,3 +234,13 @@ int honey_get_callback(lua_State* L, char* callback) return ref; } + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_set_framebuffer(lua_State* L) +{ + int framebuffer; + honey_lua_parse_arguments(L, 1, HONEY_INTEGER, &framebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); + return 0; +} -- cgit v1.2.1