summaryrefslogtreecommitdiff
path: root/src/render/gl.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/render/gl.h')
-rw-r--r--src/render/gl.h117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/render/gl.h b/src/render/gl.h
new file mode 100644
index 0000000..cf2b5dd
--- /dev/null
+++ b/src/render/gl.h
@@ -0,0 +1,117 @@
+#pragma once
+
+#include <SDL.h>
+#include <GLES2/gl2.h>
+
+#include "../util.h"
+#include "../util/matrix.h"
+
+typedef struct GL_FONT *PGL_FONT;
+typedef struct GL_DATA *PGL_DATA;
+
+struct VERTEX {
+ VEC2 pos;
+ VEC2 uv;
+ CLR clr;
+ U8 sampler;
+};
+
+struct GL_SHADER_DEF {
+ const char* name;
+ const char* code;
+ I32 type;
+ U32 id;
+ U8 compiled;
+};
+
+struct GL_SHADER_PROGRAM {
+ const char* name;
+ GL_SHADER_DEF fsh;
+ GL_SHADER_DEF vsh;
+ U32 id;
+ U32 vbuffer;
+
+ GL_DATA* gl;
+};
+
+struct GL_TEX2D {
+ GLuint id;
+ char name[256];
+ U32 width;
+ U32 height;
+ U32 channels;
+ U8* data;
+};
+
+typedef struct GL_DATA {
+ SDL_Window* window;
+ SDL_GLContext ctx;
+ SDL_Renderer* renderer;
+
+ I32 canvas_size[2];
+
+ LIST<GL_SHADER_PROGRAM*> programs;
+ LIST<GL_TEX2D*> textures;
+ LIST<GL_FONT*> fonts;
+
+ I32 shader_texture_limit;
+ U64 last_tick;
+ F32 frametime;
+ F32 fps;
+
+ GLuint vbuffer;
+
+ VEC2 clip_start;
+ VEC2 clip_dim;
+
+ VEC2 viewport_start;
+ VEC2 viewport_dim;
+
+ MAT4* proj_matrix;
+} *PGL_DATA;
+
+GL_DATA* gl_instance();
+GL_DATA* gl_create( I32* _canvas );
+void gl_destroy( GL_DATA* gl );
+void gl_gen_buffers( GL_DATA* gl );
+STAT gl_beginframe( GL_DATA* gl );
+STAT gl_endframe( GL_DATA* gl );
+STAT gl_shader_compile( GL_DATA* gl, GL_SHADER_DEF* shader );
+void gl_shader_destroy( GL_DATA* gl, GL_SHADER_DEF* shader );
+GL_SHADER_PROGRAM* gl_program_create( GL_DATA* gl, const char* name, U32 size = sizeof(GL_SHADER_PROGRAM) );
+STAT gl_program_compile( GL_DATA* gl, GL_SHADER_PROGRAM* program );
+void gl_program_destroy( GL_DATA* gl, GL_SHADER_PROGRAM* program );
+GL_TEX2D* gl_texture_create( GL_DATA* gl, const char* name );
+GL_TEX2D* gl_texture_from_file( GL_DATA* gl, const char* name );
+GL_TEX2D* gl_texture_from_bitmap( GL_DATA* gl, const char* name, U8* bitmap, U32 width, U32 height );
+void gl_texture_destroy( GL_DATA* gl, GL_TEX2D* tex );
+void gl_update_window( GL_DATA* gl, I32* size );
+void gl_set_clip( GL_DATA* gl, VEC2 start, VEC2 dim );
+void gl_get_clip( GL_DATA* gl, VEC2* start, VEC2* dim );
+void gl_set_viewport( GL_DATA* gl, VEC2 start, VEC2 dim );
+void gl_get_viewport( GL_DATA* gl, VEC2* start, VEC2* dim );
+void gl_reset_clip( GL_DATA* gl );
+
+// special sampler id for no texture
+const U8 SAMPLER_ID_NONE = 255;
+extern I32 SAMPLER_INDICES[255];
+
+template <typename VERTEX>
+LIST<VERTEX> triangle_fan_to_list( VERTEX* vertices, U32 vert_count ) {
+ LIST<VERTEX> ret{};
+ if( vert_count < 3 ) {
+ return ret;
+ }
+
+ ret.reserve( (vert_count - 2) * 3 );
+ VERTEX* start = &vertices[0];
+ VERTEX* last = &vertices[1];
+ for( U32 i = 2; i < vert_count; ++i ) {
+ ret.push( *start );
+ ret.push( *last );
+ ret.push( vertices[i] );
+ last = &vertices[i];
+ }
+
+ return ret;
+}