X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=src%2Fvg%2Fvg_ui.h;h=4e09338dc2985149ea84f711e596433754ba37f4;hb=367883958336d1c04c8a304af6119b21f0f2f15a;hp=fa4d9cd1e149fde4880041cbd01b9ea3789d5873;hpb=94b61bed97af43179b37593f4453a05d03687cbc;p=vg.git diff --git a/src/vg/vg_ui.h b/src/vg/vg_ui.h index fa4d9cd..4e09338 100644 --- a/src/vg/vg_ui.h +++ b/src/vg/vg_ui.h @@ -1,5 +1,9 @@ /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ +/* + * TODO: Get rid of many context design + */ + #ifndef VG_UI_H #define VG_UI_H @@ -171,73 +175,93 @@ static ui_colourset ui_default_colours = { static ui_ctx ui_global_ctx; -static void ui_init_context( ui_ctx *ctx, int index_buffer_size ) +static void ui_context_vg_free( ui_ctx *ctx ) { - u32 vertex_buffer_size = (index_buffer_size+(index_buffer_size/2)); - - /* Generate the buffer we are gonna be drawing to */ - { - glGenVertexArrays(1, &ctx->vao); - glGenBuffers( 1, &ctx->vbo ); - glGenBuffers( 1, &ctx->ebo ); - glBindVertexArray( ctx->vao ); - - glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo ); - - glBufferData( GL_ARRAY_BUFFER, - vertex_buffer_size * sizeof( struct ui_vert ), - NULL, GL_DYNAMIC_DRAW ); - glBindVertexArray( ctx->vao ); - - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo ); - glBufferData( GL_ELEMENT_ARRAY_BUFFER, - index_buffer_size * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW ); - - u32 const stride = sizeof( struct ui_vert ); - - /* XY */ - glVertexAttribPointer( 0, 2, GL_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 ); - - /* CLIPPING */ - glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride, - (void *)offsetof( struct ui_vert, clip ) ); - glEnableVertexAttribArray( 3 ); - } + glDeleteVertexArrays( 1, &ctx->vao ); + glDeleteBuffers( 1, &ctx->vbo ); + glDeleteBuffers( 1, &ctx->ebo ); - /* Initialize default context */ - { - ctx->verts = (struct ui_vert *)malloc( - vertex_buffer_size * sizeof(struct ui_vert) ); - ctx->indices = (u16*)malloc( index_buffer_size * sizeof(u16) ); - - if( !ctx->colours ) - ctx->colours = &ui_default_colours; - } + vg_free( ctx->verts ); + vg_free( ctx->indices ); } -static void ui_context_free( ui_ctx *ctx ) +static int ui_init_context( ui_ctx *ctx, int index_buffer_size ) { - glDeleteVertexArrays( 1, &ctx->vao ); - glDeleteBuffers( 1, &ctx->vbo ); - glDeleteBuffers( 1, &ctx->ebo ); + u32 vertex_buffer_size = (index_buffer_size+(index_buffer_size/2)); - free( ctx->verts ); - free( ctx->indices ); + /* Generate the buffer we are gonna be drawing to */ + glGenVertexArrays( 1, &ctx->vao ); + glGenBuffers( 1, &ctx->vbo ); + glGenBuffers( 1, &ctx->ebo ); + + glBindVertexArray( ctx->vao ); + glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo ); + + glBufferData( GL_ARRAY_BUFFER, + vertex_buffer_size * sizeof( struct ui_vert ), + NULL, GL_DYNAMIC_DRAW ); + glBindVertexArray( ctx->vao ); + + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo ); + glBufferData( GL_ELEMENT_ARRAY_BUFFER, + index_buffer_size * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW ); + + VG_CHECK_GL_ERR(); + + /* Set pointers */ + u32 const stride = sizeof( struct ui_vert ); + + /* XY */ + glVertexAttribPointer( 0, 2, GL_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 ); + + /* CLIPPING */ + glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride, + (void *)offsetof( struct ui_vert, clip ) ); + glEnableVertexAttribArray( 3 ); + + VG_CHECK_GL_ERR(); + + /* Alloc RAM default context */ + ctx->verts = (struct ui_vert *)vg_alloc( + vertex_buffer_size * sizeof(struct ui_vert) ); + if( !ctx->verts ) + goto il_fail_alloc_verts; + + ctx->indices = (u16*)vg_alloc( index_buffer_size * sizeof(u16) ); + if( !ctx->indices ) + goto il_fail_alloc_indices; + + if( !ctx->colours ) + ctx->colours = &ui_default_colours; + + return 1; + + vg_free( ctx->indices ); + ctx->indices = NULL; +il_fail_alloc_indices: + vg_free( ctx->verts ); + ctx->verts = NULL; +il_fail_alloc_verts: + glDeleteBuffers( 1, &ctx->ebo ); + glDeleteBuffers( 1, &ctx->vbo ); + glDeleteVertexArrays( 1, &ctx->vao ); + VG_CHECK_GL_ERR(); + return 0; } -static void ui_default_init(void) +static int ui_default_init(void) { /* Load default font */ u32 compressed[] = { @@ -245,7 +269,7 @@ static void ui_default_init(void) }; u32 pixels = 0, total = 256*256, data = 0; - u8 *image = malloc( total ); + u8 *image = vg_alloc( total ); while( pixels < total ) { @@ -264,23 +288,33 @@ static void ui_default_init(void) glGenTextures( 1, &ui_glyph_texture ); glBindTexture( GL_TEXTURE_2D, ui_glyph_texture ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0, GL_RED, GL_UNSIGNED_BYTE, image ); + vg_free( image ); + + VG_CHECK_GL_ERR(); + vg_tex2d_clamp(); vg_tex2d_nearest(); - - free( image ); - vg_shader_register( &_shader_ui ); - ui_init_context( &ui_global_ctx, 20000 ); + if( !ui_init_context( &ui_global_ctx, 20000 ) ) goto il_generic_fail; + if( !vg_shader_compile( &_shader_ui ) ) goto il_shader_fail; + + return 1; + +il_shader_fail: +il_generic_fail: + glDeleteTextures( 1, &ui_glyph_texture ); + vg_free( image ); + return 0; } static void ui_default_free(void) { + vg_free_shader( &_shader_ui ); glDeleteTextures( 1, &ui_glyph_texture ); - ui_context_free( &ui_global_ctx ); + ui_context_vg_free( &ui_global_ctx ); } static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, @@ -378,7 +412,10 @@ static struct ui_qnode *ui_current( ui_ctx *ctx ) static void ui_new_node( ui_ctx *ctx ) { if( ctx->stack_count == vg_list_size( ctx->stack ) ) - vg_exiterr( "[UI] Stack overflow while creating box!" ); + { + vg_error( "[UI] Stack overflow while creating box!" ); + return; + } struct ui_qnode *parent = &ctx->stack[ ctx->stack_count-1 ]; struct ui_qnode *node = &ctx->stack[ ctx->stack_count++ ]; @@ -690,7 +727,10 @@ static void ui_begin( ui_ctx *ctx, ui_px res_x, ui_px res_y ) static void ui_resolve( ui_ctx *ctx ) { if( ctx->stack_count-1 ) - vg_exiterr( "[UI] Mismatched node create/drestroy!" ); + { + vg_error( "[UI] Mismatched node create/drestroy!" ); + return; + } if( ctx->click_state == 3 || ctx->click_state == 0 ) {