X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=src%2Fvg%2Fvg_ui.h;h=0c4b4159bc5636419e86f430fe185aac93410adb;hb=3dd767bb10e6fee9cbffeb185d1a9685810c17b5;hp=0dc4eba33c3fb3eecfa605ad7f3a67fba1692e26;hpb=5df88af6730a8b9c4ef698070bb729866ed2e597;p=vg.git diff --git a/src/vg/vg_ui.h b/src/vg/vg_ui.h index 0dc4eba..0c4b415 100644 --- a/src/vg/vg_ui.h +++ b/src/vg/vg_ui.h @@ -130,7 +130,7 @@ struct struct ui_vert *vertex_buffer; u16 *indice_buffer; - u32 num_verts, num_indices; + u32 max_verts, max_indices, cur_vert, cur_indice; ui_rect clipping; ui_rect cursor; @@ -176,8 +176,9 @@ VG_STATIC void ui_init_context(void) * Vertex buffer * ---------------------------------------- */ - u32 index_buffer_size = 20000, - vertex_buffer_size = (index_buffer_size+(index_buffer_size/2)); + + vg_uictx.max_indices = 20000; + vg_uictx.max_verts = 30000; /* Generate the buffer we are gonna be drawing to */ glGenVertexArrays( 1, &vg_uictx.vao ); @@ -188,13 +189,13 @@ VG_STATIC void ui_init_context(void) glBindBuffer( GL_ARRAY_BUFFER, vg_uictx.vbo ); glBufferData( GL_ARRAY_BUFFER, - vertex_buffer_size * sizeof( struct ui_vert ), + vg_uictx.max_verts * sizeof( struct ui_vert ), NULL, GL_DYNAMIC_DRAW ); glBindVertexArray( vg_uictx.vao ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_uictx.ebo ); glBufferData( GL_ELEMENT_ARRAY_BUFFER, - index_buffer_size * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW ); + vg_uictx.max_indices * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW ); VG_CHECK_GL_ERR(); @@ -224,14 +225,11 @@ VG_STATIC void ui_init_context(void) VG_CHECK_GL_ERR(); /* Alloc RAM default context */ - u32 vert_size = vertex_buffer_size*sizeof(struct ui_vert), - inds_size = index_buffer_size *sizeof(u16); + u32 vert_size = vg_uictx.max_verts*sizeof(struct ui_vert), + inds_size = vg_uictx.max_indices*sizeof(u16); - vg_uictx.vertex_buffer = - vg_create_linear_allocator(vg_mem.rtmemory,vert_size); - - vg_uictx.indice_buffer = - vg_create_linear_allocator(vg_mem.rtmemory,inds_size); + vg_uictx.vertex_buffer = vg_linear_alloc( vg_mem.rtmemory, vert_size ); + vg_uictx.indice_buffer = vg_linear_alloc( vg_mem.rtmemory, inds_size ); /* font * ----------------------------------------------------- @@ -274,7 +272,7 @@ static struct ui_vert *ui_fill_rect_uv( ui_rect rect, u32 colour, ui_px uv[4] ); VG_STATIC void ui_draw( m3x3f view_override ) { - u32 num_indices_normal = vg_uictx.num_indices; + u32 num_indices_normal = vg_uictx.cur_indice; /* Append images to back of buffer */ for( int i = 0; i < vg_uictx.image_count; i ++ ) @@ -287,12 +285,12 @@ VG_STATIC void ui_draw( m3x3f view_override ) glBindBuffer( GL_ARRAY_BUFFER, vg_uictx.vbo ); glBufferSubData( GL_ARRAY_BUFFER, 0, - vg_uictx.num_verts * sizeof( struct ui_vert ), + vg_uictx.cur_vert * sizeof( struct ui_vert ), vg_uictx.vertex_buffer ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_uictx.ebo ); glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, - vg_uictx.num_indices * sizeof( u16 ), + vg_uictx.cur_indice * sizeof( u16 ), vg_uictx.indice_buffer ); glEnable(GL_BLEND); @@ -493,8 +491,13 @@ VG_STATIC void ui_release_clip(void) static struct ui_vert *ui_fill_rect_uv( ui_rect rect, u32 colour, ui_px uv[4] ) { - struct ui_vert *vertices = &vg_uictx.vertex_buffer[ vg_uictx.num_verts ]; - u16 *indices = &vg_uictx.indice_buffer[ vg_uictx.num_indices ]; + /* this if far from ideal but stops us from crashing */ + if( (vg_uictx.cur_vert + 6 > vg_uictx.max_verts) || + (vg_uictx.cur_indice + 4 > vg_uictx.max_indices)) + return vg_uictx.vertex_buffer; + + struct ui_vert *vertices = &vg_uictx.vertex_buffer[ vg_uictx.cur_vert ]; + u16 *indices = &vg_uictx.indice_buffer[ vg_uictx.cur_indice ]; vertices[0].co[0] = rect[0]; vertices[0].co[1] = rect[1]; @@ -516,7 +519,7 @@ static struct ui_vert *ui_fill_rect_uv( ui_rect rect, u32 colour, ui_px uv[4] ) vertices[3].uv[0] = uv[0]; vertices[3].uv[1] = uv[3]; vertices[3].colour = colour; - u16 ind_start = vg_uictx.num_verts; + u16 ind_start = vg_uictx.cur_vert; ui_rect_copy( vg_uictx.clipping, vertices[0].clip ); ui_rect_copy( vg_uictx.clipping, vertices[1].clip ); @@ -531,8 +534,8 @@ static struct ui_vert *ui_fill_rect_uv( ui_rect rect, u32 colour, ui_px uv[4] ) indices[4] = ind_start+3; indices[5] = ind_start+2; - vg_uictx.num_indices += 6; - vg_uictx.num_verts += 4; + vg_uictx.cur_indice += 6; + vg_uictx.cur_vert += 4; return vertices; } @@ -667,8 +670,8 @@ VG_STATIC void ui_begin( ui_px res_x, ui_px res_y ) vg_uictx.stack_count = 1; - vg_uictx.num_verts = 0; - vg_uictx.num_indices = 0; + vg_uictx.cur_vert = 0; + vg_uictx.cur_indice = 0; ui_release_clip();