summaryrefslogtreecommitdiff
path: root/src/opengl/gl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/opengl/gl.c')
-rw-r--r--src/opengl/gl.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/opengl/gl.c b/src/opengl/gl.c
new file mode 100644
index 0000000..b8da1ec
--- /dev/null
+++ b/src/opengl/gl.c
@@ -0,0 +1,112 @@
+#include <stdlib.h>
+#include <stdbool.h>
+#include <glad/glad.h>
+#include <GLFW/glfw3.h>
+#include <lua.h>
+#include <lauxlib.h>
+#include "util/util.h"
+#include "gl.h"
+
+/* needs to be here because glad uses macros to define glBufferData */
+#ifdef HONEY_TEST_H
+#undef glBufferData
+#define glBufferData mock_glBufferData_
+#endif
+
+
+int gl_init(lua_State *L);
+int glad_init(lua_State *L);
+int gl_terminate(lua_State *L);
+int gl_get_error(lua_State *L);
+int gl_enable(lua_State *L);
+int gl_disable(lua_State *L);
+
+void setup_gl(lua_State *L, int honey_index)
+{
+ struct honey_tbl_t tbl[] = {
+ /* functions */
+ H_FUNC("Init", gl_init),
+ H_FUNC("InitGlad", glad_init),
+ H_FUNC("Terminate", gl_terminate),
+ H_FUNC("GetError", gl_get_error),
+ H_FUNC("Enable", gl_enable),
+ H_FUNC("Disable", gl_disable),
+
+ /******** enums ********/
+ /* data types */
+ H_INT("UNSIGNED_BYTE", GL_UNSIGNED_BYTE),
+ H_INT("UNSIGNED_INT", GL_UNSIGNED_INT),
+ H_INT("INT", GL_INT),
+ H_INT("FLOAT", GL_FLOAT),
+
+ /* error types */
+ H_INT("NO_ERROR", GL_NO_ERROR),
+ H_INT("INVALID_ENUM", GL_INVALID_ENUM),
+ H_INT("INVALID_VALUE", GL_INVALID_VALUE),
+ H_INT("INVALID_OPERATION", GL_INVALID_OPERATION),
+ H_INT("INVALID_FRAMEBUFFER_OPERATION", GL_INVALID_FRAMEBUFFER_OPERATION),
+ H_INT("OUT_OF_MEMORY", GL_OUT_OF_MEMORY),
+
+ /* opengl capabilities */
+ H_INT("DEPTH_TEST", GL_DEPTH_TEST),
+ H_INT("CULL_FACE", GL_CULL_FACE),
+
+ H_END
+ };
+ create_table(L, tbl);
+ int gl_index = lua_gettop(L);
+
+ setup_shader(L, gl_index);
+ setup_drawing(L, gl_index);
+ setup_data(L, gl_index);
+ setup_texture(L, gl_index);
+
+ lua_setfield(L, honey_index, "gl");
+}
+
+
+int gl_init(lua_State *L)
+{
+ if (!glfwInit()) {
+ luaL_error(L, "failed to initialize GLFW");
+ }
+ return 0;
+}
+
+
+int glad_init(lua_State *L)
+{
+ if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
+ luaL_error(L, "failed to initialize GLAD");
+ }
+}
+
+
+int gl_terminate(lua_State *L)
+{
+ glfwTerminate();
+ return 0;
+}
+
+
+int gl_get_error(lua_State *L)
+{
+ lua_pushinteger(L, glGetError());
+ return 1;
+}
+
+
+int gl_enable(lua_State *L)
+{
+ lua_Integer cap = luaL_checkinteger(L, 1);
+ glEnable(cap);
+ return 0;
+}
+
+
+int gl_disable(lua_State *L)
+{
+ lua_Integer cap = luaL_checkinteger(L, 1);
+ glDisable(cap);
+ return 0;
+}