From 1cf9cc3408af7008451f9133fb95af66a9697d15 Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 28 Sep 2022 17:42:56 -0500 Subject: add basic cairo demo functions --- src/gl/texture.c | 12 ++++ src/image/image.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 186 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/gl/texture.c b/src/gl/texture.c index f9a65b3..002ec95 100644 --- a/src/gl/texture.c +++ b/src/gl/texture.c @@ -37,6 +37,10 @@ void setup_texture(lua_State *L, int gl_index) hs_str_int("TEXTURE_WRAP_T", GL_TEXTURE_WRAP_T), hs_str_int("TEXTURE_MIN_FILTER", GL_TEXTURE_MIN_FILTER), hs_str_int("TEXTURE_MAG_FILTER", GL_TEXTURE_MAG_FILTER), + hs_str_int("TEXTURE_SWIZZLE_R", GL_TEXTURE_SWIZZLE_R), + hs_str_int("TEXTURE_SWIZZLE_G", GL_TEXTURE_SWIZZLE_G), + hs_str_int("TEXTURE_SWIZZLE_B", GL_TEXTURE_SWIZZLE_B), + hs_str_int("TEXTURE_SWIZZLE_A", GL_TEXTURE_SWIZZLE_A), /* wrapping types */ hs_str_int("REPEAT", GL_REPEAT), @@ -44,6 +48,14 @@ void setup_texture(lua_State *L, int gl_index) /* filter types */ hs_str_int("NEAREST", GL_NEAREST), hs_str_int("LINEAR", GL_LINEAR), + + /* swizzle targets */ + hs_str_int("RED", GL_RED), + hs_str_int("GREEN", GL_GREEN), + hs_str_int("BLUE", GL_BLUE), + hs_str_int("ALPHA", GL_ALPHA), + hs_str_int("ZERO", GL_ZERO), + hs_str_int("ONE", GL_ONE), ); append_table(L, gl_index, tbl); diff --git a/src/image/image.c b/src/image/image.c index a8a0502..1c747e2 100644 --- a/src/image/image.c +++ b/src/image/image.c @@ -1,10 +1,61 @@ #include #include +#include /* assimp provides its own stb_image implementation */ /*#define STB_IMAGE_IMPLEMENTATION*/ #include "stb_image.h" #include "image.h" + +int load_image(lua_State *L); +int free_image(lua_State *L); + +int surface_create(lua_State *L); +int surface_destroy(lua_State *L); +int surface_get_data(lua_State *L); +int context_create(lua_State *L); +int context_destroy(lua_State *L); +int context_select_font_face(lua_State *L); +int context_set_font_size(lua_State *L); +int context_show_text(lua_State *L); +int context_set_source_rgb(lua_State *L); +int context_move_to(lua_State *L); + + +void setup_image(lua_State *L, int honey_index) +{ + hs_create_table(L, + /* basic images */ + hs_str_cfunc("load", load_image), + hs_str_cfunc("destroy", free_image), + + /* cairo bindings */ + hs_str_cfunc("surface_create", surface_create), + hs_str_cfunc("surface_destroy", surface_destroy), + hs_str_cfunc("surface_get_data", surface_get_data), + hs_str_cfunc("context_create", context_create), + hs_str_cfunc("context_destroy", context_destroy), + hs_str_cfunc("context_select_font_face", context_select_font_face), + hs_str_cfunc("context_set_font_size", context_set_font_size), + hs_str_cfunc("context_show_text", context_show_text), + hs_str_cfunc("context_set_source_rgb", context_set_source_rgb), + hs_str_cfunc("context_move_to", context_move_to), + + /* format enum */ + hs_str_int("FORMAT_ARGB32", CAIRO_FORMAT_ARGB32), + + + /* font enums */ + hs_str_int("FONT_SLANT_NORMAL", CAIRO_FONT_SLANT_NORMAL), + hs_str_int("FONT_WEIGHT_NORMAL", CAIRO_FONT_WEIGHT_NORMAL), + ); + + lua_setfield(L, honey_index, "image"); +} + + +/* --===== basic images =====-- */ + int load_image(lua_State *L) { char *filename; @@ -33,12 +84,129 @@ int free_image(lua_State *L) } -void setup_image(lua_State *L, int honey_index) +/* --===== cairo bindings =====-- */ + +int surface_create(lua_State *L) { - hs_create_table(L, - hs_str_cfunc("load", load_image), - hs_str_cfunc("destroy", free_image), - ); + lua_Integer format, width, height; + hs_parse_args(L, hs_int(format), hs_int(width), hs_int(height)); - lua_setfield(L, honey_index, "image"); + cairo_surface_t *surface = cairo_image_surface_create(format, width, height); + cairo_status_t status = cairo_surface_status(surface); + if (status != CAIRO_STATUS_SUCCESS) + hs_throw_error(L, "error creating cairo surface: %s", cairo_status_to_string(status)); + + lua_pushlightuserdata(L, surface); + return 1; +} + + +int surface_destroy(lua_State *L) +{ + void *surface_ptr; + hs_parse_args(L, hs_light(surface_ptr)); + cairo_surface_t *surface = surface_ptr; + + cairo_surface_destroy(surface); + return 0; +} + + +int surface_get_data(lua_State *L) +{ + void *surface_ptr; + hs_parse_args(L, hs_light(surface_ptr)); + cairo_surface_t *surface = surface_ptr; + + unsigned char *data = cairo_image_surface_get_data(surface); + lua_pushlightuserdata(L, data); + return 1; +} + + +int context_create(lua_State *L) +{ + void *surface_ptr; + hs_parse_args(L, hs_light(surface_ptr)); + cairo_surface_t *surface = surface_ptr; + + cairo_t *cr = cairo_create(surface); + cairo_status_t status = cairo_status(cr); + if (status != CAIRO_STATUS_SUCCESS) + hs_throw_error(L, "error creating cairo context: %s", cairo_status_to_string(status)); + + lua_pushlightuserdata(L, cr); + return 1; +} + + +int context_destroy(lua_State *L) +{ + void *cr_ptr; + hs_parse_args(L, hs_light(cr_ptr)); + cairo_t *cr = cr_ptr; + + cairo_destroy(cr); + return 0; +} + + +int context_select_font_face(lua_State *L) +{ + void *cr_ptr; + char *family; + lua_Integer slant, weight; + hs_parse_args(L, hs_light(cr_ptr), hs_str(family), hs_int(slant), hs_int(weight)); + cairo_t *cr = cr_ptr; + + cairo_select_font_face(cr, family, slant, weight); + return 0; +} + + +int context_set_font_size(lua_State *L) +{ + void *cr_ptr; + lua_Number size; + hs_parse_args(L, hs_light(cr_ptr), hs_num(size)); + cairo_t *cr = cr_ptr; + + cairo_set_font_size(cr, size); + return 0; +} + + +int context_show_text(lua_State *L) +{ + void *cr_ptr; + char *str; + hs_parse_args(L, hs_light(cr_ptr), hs_str(str)); + cairo_t *cr = cr_ptr; + + cairo_show_text(cr, str); + return 0; +} + + +int context_set_source_rgb(lua_State *L) +{ + void *cr_ptr; + lua_Number r, g, b; + hs_parse_args(L, hs_light(cr_ptr), hs_num(r), hs_num(g), hs_num(b)); + cairo_t *cr = cr_ptr; + + cairo_set_source_rgb(cr, r, g, b); + return 0; +} + + +int context_move_to(lua_State *L) +{ + void *cr_ptr; + lua_Number x, y; + hs_parse_args(L, hs_light(cr_ptr), hs_num(x), hs_num(y)); + cairo_t *cr = cr_ptr; + + cairo_move_to(cr, x, y); + return 0; } -- cgit v1.2.1