summaryrefslogtreecommitdiff
path: root/src/gl
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-08-22 09:08:59 -0500
committersanine <sanine.not@pm.me>2022-08-22 09:08:59 -0500
commit33f3eb1c5a489a1b0c7d508e6a42f0dbb20e3a1b (patch)
treeb414766946396a138def51f27edcb15bf23455b6 /src/gl
parent68d4b40cd00dd0ec2f0ed09033f8e05a98e5ca2d (diff)
add types to gl_buffer_data
Diffstat (limited to 'src/gl')
-rw-r--r--src/gl/gl.c48
-rw-r--r--src/gl/gl.test.c69
2 files changed, 98 insertions, 19 deletions
diff --git a/src/gl/gl.c b/src/gl/gl.c
index f9419e2..953cefb 100644
--- a/src/gl/gl.c
+++ b/src/gl/gl.c
@@ -181,26 +181,50 @@ int gl_bind_buffer(lua_State *L)
int gl_buffer_data(lua_State *L)
{
- lua_Integer target, usage;
+ lua_Integer target, type, usage;
int table;
- hs_parse_args(L, hs_int(target), hs_tbl(table), hs_int(usage));
+ hs_parse_args(L, hs_int(target), hs_int(type), hs_tbl(table), hs_int(usage));
+
+ if (type != GL_INT && type != GL_FLOAT) {
+ hs_throw_error(L, "invalid type");
+ }
/* build raw buffer */
size_t len = lua_objlen(L, table);
- float *buf = malloc(len * sizeof(float));
- if (buf == NULL)
- hs_throw_error(L, "failed to allocate intermediary buffer");
- for (int i=0; i<len; i++) {
- lua_rawgeti(L, table, i+1);
- if (!lua_isnumber(L, -1)) {
- hs_throw_error(L, "all table items must be numbers (failed at index %d)", i);
+ void *buf;
+ if (type == GL_FLOAT) {
+ float *fbuf = malloc(len * sizeof(float));
+ if (fbuf == NULL)
+ hs_throw_error(L, "failed to allocate intermediary fbuffer");
+ for (int i=0; i<len; i++) {
+ lua_rawgeti(L, table, i+1);
+ if (!lua_isnumber(L, -1)) {
+ hs_throw_error(L, "all table items must be numbers (failed at index %d)", i);
+ }
+ fbuf[i] = lua_tonumber(L, -1);
+ lua_pop(L, 1);
+ }
+ len = len * sizeof(float);
+ buf = fbuf;
+ }
+ else {
+ int *ibuf = malloc(len * sizeof(int));
+ if (ibuf == NULL)
+ hs_throw_error(L, "failed to allocate intermediary ibuffer");
+ for (int i=0; i<len; i++) {
+ lua_rawgeti(L, table, i+1);
+ if (!lua_isnumber(L, -1)) {
+ hs_throw_error(L, "all table items must be integers (failed at index %d)", i);
+ }
+ ibuf[i] = lua_tointeger(L, -1);
+ lua_pop(L, 1);
}
- buf[i] = lua_tonumber(L, -1);
- lua_pop(L, 1);
+ len = len * sizeof(int);
+ buf = ibuf;
}
/* call */
- glBufferData(target, len*sizeof(float), buf, usage);
+ glBufferData(target, len, buf, usage);
free(buf);
return 0;
}
diff --git a/src/gl/gl.test.c b/src/gl/gl.test.c
index 9782caf..bab8529 100644
--- a/src/gl/gl.test.c
+++ b/src/gl/gl.test.c
@@ -67,10 +67,21 @@ void mock_glBufferData_(int target, size_t size, const void *data, int usage)
};
lily_mock_store_call(mock_glBufferData, args);
- size_t count = size/sizeof(float);
- float *numbers = data;
- for (int i=0; i<count; i++) {
- lily_store_value(mock_glBufferData, float, numbers[i]);
+ int use_ints; lily_get_value(mock_glBufferData, int, &use_ints);
+
+ if (use_ints) {
+ size_t count = size/sizeof(int);
+ int *numbers = data;
+ for (int i=0; i<count; i++) {
+ lily_store_value(mock_glBufferData, int, numbers[i]);
+ }
+ }
+ else {
+ size_t count = size/sizeof(float);
+ float *numbers = data;
+ for (int i=0; i<count; i++) {
+ lily_store_value(mock_glBufferData, float, numbers[i]);
+ }
}
}
@@ -124,21 +135,23 @@ void gl_terminate_works()
}
-void gl_buffer_data_works()
+void gl_buffer_float_works()
{
lily_mock_use(&mock_hs_throw_error);
lily_mock_use(&mock_glBufferData);
+ lily_store_value(mock_glBufferData, int, 0); // use floats
lua_State *L = luaL_newstate();
lua_pushcfunction(L, gl_buffer_data);
lua_pushinteger(L, GL_ARRAY_BUFFER);
+ lua_pushinteger(L, GL_FLOAT);
hs_create_table(L,
hs_int_num(1, 22),
hs_int_num(2, 33),
hs_int_num(3, 44),
);
lua_pushinteger(L, GL_STATIC_DRAW);
- int err = hs_call(L, 3, 0);
+ int err = hs_call(L, 4, 0);
lua_close(L);
lily_assert_int_equal(err, 0);
@@ -164,12 +177,54 @@ void gl_buffer_data_works()
}
+void gl_buffer_int_works()
+{
+ lily_mock_use(&mock_hs_throw_error);
+ lily_mock_use(&mock_glBufferData);
+ lily_store_value(mock_glBufferData, int, 1); // use ints
+
+ lua_State *L = luaL_newstate();
+ lua_pushcfunction(L, gl_buffer_data);
+ lua_pushinteger(L, GL_ARRAY_BUFFER);
+ lua_pushinteger(L, GL_INT);
+ hs_create_table(L,
+ hs_int_num(1, 22),
+ hs_int_num(2, 33),
+ hs_int_num(3, 44),
+ );
+ lua_pushinteger(L, GL_STATIC_DRAW);
+ int err = hs_call(L, 4, 0);
+ lua_close(L);
+
+ lily_assert_int_equal(err, 0);
+ lily_assert_int_equal(mock_glBufferData->n_calls, 1);
+ int target; size_t size; int usage;
+ struct lily_mock_arg_t args[] = {
+ { sizeof(int), &target },
+ { sizeof(size_t), &size },
+ { sizeof(int), &usage }
+ };
+ lily_mock_get_call(mock_glBufferData, args, 0);
+ lily_assert_int_equal(target, GL_ARRAY_BUFFER);
+ lily_assert_int_equal(size, 3*sizeof(float));
+ lily_assert_int_equal(usage, GL_STATIC_DRAW);
+
+ int n;
+ lily_get_value(mock_glBufferData, int, &n);
+ lily_assert_int_equal(n, 22);
+ lily_get_value(mock_glBufferData, int, &n);
+ lily_assert_int_equal(n, 33);
+ lily_get_value(mock_glBufferData, int, &n);
+ lily_assert_int_equal(n, 44);
+}
+
void suite_gl()
{
lily_run_test(gl_init_succeeds);
lily_run_test(gl_init_fails);
lily_run_test(gl_terminate_works);
- lily_run_test(gl_buffer_data_works);
+ lily_run_test(gl_buffer_float_works);
+ lily_run_test(gl_buffer_int_works);
lily_mock_destroy(mock_glfwInit);
lily_mock_destroy(mock_hs_throw_error);