+// Globals
+// ===========================================================================================================
+
+// Opengl
+GLuint ui_glyph_texture;
+GLuint ui_vao;
+GLuint ui_vbo;
+GLuint ui_ebo;
+
+#define UI_BUFFER_SIZE 30000
+#define UI_INDEX_SIZE 20000
+
+ui_ctx ui_global_ctx = { .padding = 8 };
+
+
+// Initialization
+// ===========================================================================================================
+
+static void ui_default_init(void)
+{
+ // Load font
+ {
+ u32 compressed[] = {
+ #include "fonts/weiholmir.h"
+ };
+
+ u32 pixels = 0, total = 72*72, data = 0;
+ u8 *image = malloc( total );
+
+ while( pixels < total )
+ {
+ for( int b = 31; b >= 0; b-- )
+ {
+ image[ pixels ++ ] = (compressed[data] & (0x1 << b))? 0xff: 0x00;
+
+ if( pixels >= total )
+ {
+ total = 0;
+ break;
+ }
+ }
+ data++;
+ }
+
+ glGenTextures( 1, &ui_glyph_texture );
+ glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
+
+ glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 72, 72, 0, GL_RED, GL_UNSIGNED_BYTE, image );
+
+ vg_tex2d_clamp();
+ vg_tex2d_nearest();
+
+ free( image );
+ }
+
+ // Setup OpenGL memory
+ {
+ SHADER_INIT( shader_ui );
+
+ // Generate the buffer we are gonna be drawing to
+ glGenVertexArrays(1, &ui_vao);
+ glGenBuffers( 1, &ui_vbo );
+ glGenBuffers( 1, &ui_ebo );
+ glBindVertexArray( ui_vao );
+
+ glBindBuffer( GL_ARRAY_BUFFER, ui_vbo );
+
+ glBufferData( GL_ARRAY_BUFFER, UI_BUFFER_SIZE * sizeof( struct ui_vert ), NULL, GL_DYNAMIC_DRAW );
+ glBindVertexArray( ui_vao );
+
+ glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ui_ebo );
+ glBufferData( GL_ELEMENT_ARRAY_BUFFER, UI_INDEX_SIZE * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW );
+
+ u32 const stride = sizeof( struct ui_vert );
+
+ // XY
+ glVertexAttribPointer( 0, 2, GL_UNSIGNED_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, co ) );
+ glEnableVertexAttribArray( 0 );
+
+ // UV
+ glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE, stride, (void *)offsetof( struct ui_vert, uv ) );
+ glEnableVertexAttribArray( 1 );
+
+ // COLOUR
+ glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void *)offsetof( struct ui_vert, colour ) );
+ glEnableVertexAttribArray( 2 );
+ }
+
+ // Initialize default context
+ {
+ ui_global_ctx.verts = (struct ui_vert *)malloc( UI_BUFFER_SIZE * sizeof(struct ui_vert) );
+ ui_global_ctx.indices = (u16*)malloc( UI_INDEX_SIZE * sizeof(u16) );
+ }
+}
+
+static void ui_default_free(void)
+{
+ glDeleteTextures( 1, &ui_glyph_texture );
+
+ glDeleteVertexArrays( 1, &ui_vao );
+ glDeleteBuffers( 1, &ui_vbo );
+ glDeleteBuffers( 1, &ui_ebo );
+
+ free( ui_global_ctx.verts );
+ free( ui_global_ctx.indices );
+}
+
+static void ui_draw( ui_ctx *ctx )
+{
+ glBindVertexArray( ui_vao );
+
+ glBindBuffer( GL_ARRAY_BUFFER, ui_vbo );
+ glBufferSubData( GL_ARRAY_BUFFER, 0, ctx->num_verts * sizeof( struct ui_vert ), ctx->verts );
+
+ glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ui_ebo );
+ glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, ctx->num_indices * sizeof( u16 ), ctx->indices );
+
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glBlendEquation(GL_FUNC_ADD);
+
+ SHADER_USE( shader_ui );
+
+ m3x3f view = M3X3_IDENTITY;
+ m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } );
+ m3x3_scale( view, (v3f){ 1.0f/((float)vg_window_x*0.5f), -1.0f/((float)vg_window_y*0.5f), 1.0f } );
+ glUniformMatrix3fv( SHADER_UNIFORM( shader_ui, "uPv" ), 1, GL_FALSE, (float *)view );
+
+ glActiveTexture( GL_TEXTURE0 );
+ glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
+ glUniform1i( SHADER_UNIFORM( shader_ui, "uTexGlyphs" ), 0 );
+
+ glDrawElements( GL_TRIANGLES, ctx->num_indices, GL_UNSIGNED_SHORT, (void*)(0) );
+
+ //vg_info( "Verts: %u, Indices: %u\n", ctx->num_verts, ctx->num_indices );
+
+ glDisable(GL_BLEND);
+}
+