X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=src%2Fvg%2Fvg_ui.h;h=844194e73867e9e468c70d9b9fcb732d983e9e2d;hb=6aeba08ad9ad8bdecafcfd9f946173e99a84fc59;hp=fa4d9cd1e149fde4880041cbd01b9ea3789d5873;hpb=db4dbcc3b685ea6038c9378016f850cc95fa0193;p=vg.git diff --git a/src/vg/vg_ui.h b/src/vg/vg_ui.h index fa4d9cd..844194e 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,62 +175,6 @@ static ui_colourset ui_default_colours = { static ui_ctx ui_global_ctx; -static void ui_init_context( ui_ctx *ctx, int index_buffer_size ) -{ - 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 ); - } - - /* 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; - } -} - static void ui_context_free( ui_ctx *ctx ) { glDeleteVertexArrays( 1, &ctx->vao ); @@ -237,7 +185,88 @@ static void ui_context_free( ui_ctx *ctx ) free( ctx->indices ); } -static void ui_default_init(void) +static int ui_init_context( ui_ctx *ctx, int index_buffer_size ) +{ + 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 ); + + if( VG_CHECK_GL_ERR() ) + goto il_fail_alloc_gpu; + + /* 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 ); + + if( VG_CHECK_GL_ERR() ) + goto il_fail_attributes; + + + /* Alloc RAM default context */ + ctx->verts = (struct ui_vert *)malloc( + vertex_buffer_size * sizeof(struct ui_vert) ); + if( !ctx->verts ) + goto il_fail_alloc_verts; + + ctx->indices = (u16*)malloc( index_buffer_size * sizeof(u16) ); + if( !ctx->indices ) + goto il_fail_alloc_indices; + + if( !ctx->colours ) + ctx->colours = &ui_default_colours; + + return 1; + + free( ctx->indices ); + ctx->indices = NULL; +il_fail_alloc_indices: + free( ctx->verts ); + ctx->verts = NULL; +il_fail_alloc_verts: +il_fail_attributes: +il_fail_alloc_gpu: + glDeleteBuffers( 1, &ctx->ebo ); + glDeleteBuffers( 1, &ctx->vbo ); + glDeleteVertexArrays( 1, &ctx->vao ); + VG_CHECK_GL_ERR(); + return 0; +} + +static int ui_default_init(void) { /* Load default font */ u32 compressed[] = { @@ -246,6 +275,7 @@ static void ui_default_init(void) u32 pixels = 0, total = 256*256, data = 0; u8 *image = malloc( total ); + if( !image ) goto il_fail_alloc_image; while( pixels < total ) { @@ -263,22 +293,39 @@ static void ui_default_init(void) } glGenTextures( 1, &ui_glyph_texture ); + if( !ui_glyph_texture ) goto il_fail_gen_texture; + glBindTexture( GL_TEXTURE_2D, ui_glyph_texture ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0, GL_RED, GL_UNSIGNED_BYTE, image ); + + if( VG_CHECK_GL_ERR() ) goto il_fail_alloc_gpu; 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_fail_alloc_gpu: +il_generic_fail: + glDeleteTextures( 1, &ui_glyph_texture ); + +il_fail_gen_texture: + free( image ); + +il_fail_alloc_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 ); } @@ -378,7 +425,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 +740,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 ) {