summaryrefslogtreecommitdiff
path: root/src/cairo_bindings.c
blob: 95dea925b5546695b0d99e452107958577b8c59b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */