summaryrefslogtreecommitdiff
path: root/src/cglm_bindings.c
blob: c50114c8446a5bd50a9e65aac8a000d1de42ecd2 (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
#include "cglm_bindings.h"

void honey_setup_cglm(lua_State* L)
{
    honey_lua_element cglm_elements[] = {
        { "new_array_zero", HONEY_FUNC, { .function = honey_cglm_new_array_zero } },
        { "set_value", HONEY_FUNC, { .function = honey_cglm_array_set_value } },
        { "get_value", HONEY_FUNC, { .function = honey_cglm_array_get_value } },
    };

    honey_lua_create_table(L, cglm_elements, 3);
}

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

int honey_cglm_new_array_zero(lua_State* L)
{
    if (!honey_lua_validate_types(L, 1, HONEY_INT))
        lua_error(L);

    int size = lua_tointeger(L, 1);
    
    float* array = lua_newuserdata(L, size*sizeof(float));
    memset(array, 0, size*sizeof(float));
    return 1;
}

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

int honey_cglm_array_set_value(lua_State* L)
{
    if (!honey_lua_validate_types(L, 3, HONEY_USERDATA, HONEY_INT, HONEY_NUM))
        lua_error(L);

    float* array = lua_touserdata(L, 1);
    int index = lua_tointeger(L, 2);
    float value = lua_tonumber(L, 3);

    array[index] = value;
    return 0;
}

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

int honey_cglm_array_get_value(lua_State* L)
{
    if (!honey_lua_validate_types(L, 2, HONEY_USERDATA, HONEY_INT))
        lua_error(L);

    float* array = lua_touserdata(L, 1);
    int index = lua_tointeger(L, 2);

    lua_pushnumber(L, array[index]);
    return 1;
}