diff options
author | pommicket <pommicket@gmail.com> | 2023-01-01 23:25:58 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2023-01-01 23:25:58 -0500 |
commit | 7ca811b72f10ce76d0372343bde71beea3528b20 (patch) | |
tree | 11c1620bd2ad85009c37ef700724468a36d9aff4 | |
parent | f173b7715cf1385db1cea67a3db07bb7d38174e9 (diff) |
gl.c restructuring
-rw-r--r-- | development.md | 3 | ||||
-rw-r--r-- | gl.c | 21 | ||||
-rw-r--r-- | ted.h | 33 |
3 files changed, 43 insertions, 14 deletions
diff --git a/development.md b/development.md index 437443b..19326ef 100644 --- a/development.md +++ b/development.md @@ -5,6 +5,9 @@ As much as possible, OS-dependent functions should be put in `os.h/os-*.c`. in theory be implemented on any platform with just plain C, should be put in `util.c` even if they use OS-specific library functions.) +## Header files + +## Drawing ## Adding settings @@ -1,3 +1,4 @@ +#include "ted.h" #include "lib/glcorearb.h" // macro trickery to avoid having to write everything twice @@ -142,7 +143,7 @@ static int glsl_version(void) { } // compile a GLSL shader -static GLuint gl_compile_shader(char error_buf[256], char const *code, GLenum shader_type) { +GLuint gl_compile_shader(char error_buf[256], char const *code, GLenum shader_type) { GLuint shader = glCreateShader(shader_type); char header[128]; int glsl = glsl_version(); @@ -172,7 +173,7 @@ static GLuint gl_compile_shader(char error_buf[256], char const *code, GLenum sh } // link together GL shaders -static GLuint gl_link_program(char error_buf[256], GLuint *shaders, size_t count) { +GLuint gl_link_program(char error_buf[256], GLuint *shaders, size_t count) { GLuint program = glCreateProgram(); if (program) { for (size_t i = 0; i < count; ++i) { @@ -200,7 +201,7 @@ static GLuint gl_link_program(char error_buf[256], GLuint *shaders, size_t count return program; } -static GLuint gl_compile_and_link_shaders(char error_buf[256], char const *vshader_code, char const *fshader_code) { +GLuint gl_compile_and_link_shaders(char error_buf[256], char const *vshader_code, char const *fshader_code) { GLuint shaders[2]; shaders[0] = gl_compile_shader(error_buf, vshader_code, GL_VERTEX_SHADER); shaders[1] = gl_compile_shader(error_buf, fshader_code, GL_FRAGMENT_SHADER); @@ -213,7 +214,7 @@ static GLuint gl_compile_and_link_shaders(char error_buf[256], char const *vshad return program; } -static GLuint gl_attrib_loc(GLuint program, char const *attrib) { +GLuint gl_attrib_loc(GLuint program, char const *attrib) { GLint loc = glGetAttribLocation(program, attrib); if (loc == -1) { debug_print("Couldn't find vertex attribute %s.\n", attrib); @@ -222,7 +223,7 @@ static GLuint gl_attrib_loc(GLuint program, char const *attrib) { return (GLuint)loc; } -static GLint gl_uniform_loc(GLuint program, char const *uniform) { +GLint gl_uniform_loc(GLuint program, char const *uniform) { GLint loc = glGetUniformLocation(program, uniform); if (loc == -1) { debug_print("Couldn't find uniform: %s.\n", uniform); @@ -246,7 +247,7 @@ static GLuint gl_geometry_v_color; static GLint gl_geometry_u_window_size; static GLuint gl_geometry_vbo, gl_geometry_vao; -static void gl_geometry_init(void) { +void gl_geometry_init(void) { char const *vshader_code = "attribute vec2 v_pos;\n\ attribute vec4 v_color;\n\ uniform vec2 u_window_size;\n\ @@ -275,7 +276,7 @@ static void gl_geometry_init(void) { static float gl_window_width, gl_window_height; -static void gl_geometry_rect(Rect r, u32 color_rgba) { +void gl_geometry_rect(Rect r, u32 color_rgba) { v4 color = rgba_u32_to_v4(color_rgba); v2 p1 = r.pos; @@ -295,7 +296,7 @@ static void gl_geometry_rect(Rect r, u32 color_rgba) { arr_add(gl_geometry_triangles, triangle); } -static void gl_geometry_rect_border(Rect r, float border_thickness, u32 color) { +void gl_geometry_rect_border(Rect r, float border_thickness, u32 color) { float x1 = r.pos.x, y1 = r.pos.y, x2 = x1 + r.size.x, y2 = y1 + r.size.y; // make sure rectangle isn't too small @@ -308,7 +309,7 @@ static void gl_geometry_rect_border(Rect r, float border_thickness, u32 color) { gl_geometry_rect(rect4(x2-border_thickness, y1+border_thickness, x2, y2), color); } -static void gl_geometry_draw(void) { +void gl_geometry_draw(void) { size_t ntriangles = arr_len(gl_geometry_triangles); if (ntriangles == 0) return; @@ -330,7 +331,7 @@ static void gl_geometry_draw(void) { arr_clear(gl_geometry_triangles); } -static GLuint gl_load_texture_from_image(const char *path) { +GLuint gl_load_texture_from_image(const char *path) { GLuint texture = 0; int w=0, h=0, n=0; unsigned char *data = stbi_load(path, &w, &h, &n, 4); @@ -20,6 +20,10 @@ // max number of LSPs running at once #define TED_LSP_MAX 200 +typedef u32 GLuint; +typedef i32 GLint; +typedef unsigned GLenum; + // these all say "CPP" but really they're C/C++ enum { SYNTAX_STATE_CPP_MULTI_LINE_COMMENT = 0x1u, // are we in a multi-line comment? (delineated by /* */) @@ -129,15 +133,15 @@ typedef struct { // so we need to be extra careful about when we delete textures. typedef struct { u32 ref_count; - u32 texture; + GLuint texture; } GlRcTexture; // shader-array-buffer combo. typedef struct { u32 ref_count; - u32 shader; - u32 array; - u32 buffer; + GLuint shader; + GLuint array; + GLuint buffer; } GlRcSAB; @@ -588,6 +592,27 @@ typedef struct Ted { char error_shown[512]; // error display in box on screen } Ted; +// === gl.c === +GlRcSAB *gl_rc_sab_new(GLuint shader, GLuint array, GLuint buffer); +void gl_rc_sab_incref(GlRcSAB *s); +void gl_rc_sab_decref(GlRcSAB **ps); +GlRcTexture *gl_rc_texture_new(GLuint texture); +void gl_rc_texture_incref(GlRcTexture *t); +void gl_rc_texture_decref(GlRcTexture **pt); +GLuint gl_compile_shader(char error_buf[256], char const *code, GLenum shader_type); +GLuint gl_link_program(char error_buf[256], GLuint *shaders, size_t count); +GLuint gl_compile_and_link_shaders(char error_buf[256], char const *vshader_code, char const *fshader_code); +// prints a debug message if `attrib` is not found +GLuint gl_attrib_location(GLuint program, char const *attrib); +// prints a debug message if `uniform` is not found +GLint gl_uniform_location(GLuint program, char const *uniform); +void gl_geometry_init(void); +void gl_geometry_rect(Rect r, u32 color_rgba); +void gl_geometry_rect_border(Rect r, float border_thickness, u32 color); +void gl_geometry_draw(void); +GLuint gl_load_texture_from_image(const char *path); + + char *buffer_contents_utf8_alloc(TextBuffer *buffer); Command command_from_str(const char *str); const char *command_to_str(Command command); |