From 09d34c697a488a8450161c62a601b04748098d96 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sat, 12 Dec 2020 21:55:48 -0600 Subject: add basic cairo bindings --- src/cairo_bindings.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cairo_bindings.h | 21 ++++++++++++++++ src/common.h | 3 +++ src/honey.c | 2 ++ src/honey.h | 1 + 5 files changed, 97 insertions(+) create mode 100644 src/cairo_bindings.c create mode 100644 src/cairo_bindings.h (limited to 'src') diff --git a/src/cairo_bindings.c b/src/cairo_bindings.c new file mode 100644 index 0000000..95dea92 --- /dev/null +++ b/src/cairo_bindings.c @@ -0,0 +1,70 @@ +#include "cairo_bindings.h" +#include "texture.h" + +int honey_cairo_mt_ref = LUA_NOREF; + +int honey_setup_cairo(lua_State* L) +{ + honey_lua_create_table + (L, 2, + HONEY_TABLE, "__index", 1, + HONEY_FUNCTION, "getTexture", honey_cairo_get_texture, + + HONEY_FUNCTION, "__gc", honey_cairo_destroy); + honey_cairo_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + lua_pushcfunction(L, honey_cairo_new); + lua_setfield(L, -2, "cairo"); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cairo_new(lua_State* L) +{ + int width, height; + honey_lua_parse_arguments(L, 1, 2, HONEY_INTEGER, &width, HONEY_INTEGER, &height); + + cairo_surface_t** surface = lua_newuserdata(L, sizeof(cairo_surface_t*)); + *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); + + cairo_status_t status = cairo_surface_status(*surface); + if (status != CAIRO_STATUS_SUCCESS) + honey_lua_throw_error + (L, "libcairo error: %s", cairo_status_to_string(status)); + + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_cairo_mt_ref); + lua_setmetatable(L, -2); + + return 1; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cairo_get_texture(lua_State* L) +{ + /* todo + cairo_surface_t** surface; + honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &surface); + + unsigned char* data = cairo_image_surface_get_data(*surface); + honey_texture* texture = lua_newuserdata(L, sizeof(honey_texture)); + + glGenTextures(1, &(texture->id)); + glBindTexture(GL_TEXTURE_2D, texture->id); + */ + return 1; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cairo_destroy(lua_State* L) +{ + cairo_surface_t** surface; + honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &surface); + + cairo_surface_destroy(*surface); + + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ diff --git a/src/cairo_bindings.h b/src/cairo_bindings.h new file mode 100644 index 0000000..f125879 --- /dev/null +++ b/src/cairo_bindings.h @@ -0,0 +1,21 @@ +#ifndef HONEY_CAIRO_H +#define HONEY_CAIRO_H + +#include "common.h" + +/* @file cairo_bindings.h + * @brief Define some binding functions for creating and manipulating cairo surfaces, + * as well as enabling turning them into OpenGL textures. + */ + +extern int honey_cairo_mt_ref; + +int honey_setup_cairo(lua_State* L); + +int honey_cairo_new(lua_State* L); + +int honey_cairo_get_texture(lua_State* L); + +int honey_cairo_destroy(lua_State* L); + +#endif diff --git a/src/common.h b/src/common.h index 7eac236..cb0ba45 100644 --- a/src/common.h +++ b/src/common.h @@ -34,6 +34,9 @@ // stb image #include "stb_image/stb_image.h" +// cairo +#include + typedef GLFWwindow* honey_window; typedef struct { diff --git a/src/honey.c b/src/honey.c index fef75d7..793f5b5 100644 --- a/src/honey.c +++ b/src/honey.c @@ -129,6 +129,8 @@ bool honey_setup(lua_State** L) honey_setup_texture(*L); + honey_setup_cairo(*L); + lua_pushcfunction(*L, honey_exit); lua_setfield(*L, -2, "exit"); diff --git a/src/honey.h b/src/honey.h index 38a2562..11d0a2f 100644 --- a/src/honey.h +++ b/src/honey.h @@ -8,6 +8,7 @@ #include "common.h" +#include "cairo_bindings.h" #include "glm_bindings.h" #include "input.h" #include "mesh.h" -- cgit v1.2.1