X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg%2Fvg_ui.h;h=b8f826c297dc1a07ac51cd216f118707c3eac1a2;hb=bb4cab005a763f086a41686b18fa00a39914226f;hp=3e4ae5e3717d272c90af5d9832368b5f5bb032c5;hpb=fa88b9de1e8416023822f91ec88971bfadcf1702;p=fishladder.git diff --git a/vg/vg_ui.h b/vg/vg_ui.h index 3e4ae5e..b8f826c 100644 --- a/vg/vg_ui.h +++ b/vg/vg_ui.h @@ -4,7 +4,7 @@ SHADER_DEFINE( shader_ui, // VERTEX "layout (location=0) in vec2 a_co;" // i16, i16, .. ? - "layout (location=1) in vec2 a_uv;" // i8, i8 + "layout (location=1) in vec2 a_uv;" // i16, i16 "layout (location=2) in vec4 a_colour;" // u32 "layout (location=3) in vec4 a_clip;" // i16, i16, i16, i16 "uniform mat3 uPv;" @@ -37,9 +37,19 @@ SHADER_DEFINE( shader_ui, "void main()" "{" "float clip_blend = step( aWsp.x, aClip.z ) * step( aWsp.y, aClip.w ) * step( aClip.x, aWsp.x ) * step( aClip.y, aWsp.y );" - - "vec4 glyph = texture( uTexGlyphs, aTexCoords );" - "FragColor = vec4( aColour.rgb * glyph.rgb, aColour.a*glyph.a*clip_blend );" + "vec4 glyph = vec4(1.0,1.0,1.0,1.0);" + + "if( aColour.a == 0.0 )" + "{" + "glyph = texture( uTexGlyphs, aTexCoords );" + "glyph.a = smoothstep( 0.47, 0.53, glyph.r );" + "}" + "else" + "{" + "glyph.a = aColour.a;" + "}" + + "FragColor = vec4( aColour.rgb, glyph.a*clip_blend );" "}" , UNIFORMS({ "uPv", "uTexGlyphs" }) @@ -138,9 +148,8 @@ struct ui_ctx // =========================================================================================================== // Opengl -int ui_glyph_override = 0; -ui_px ui_glyph_spacing_x = 6; -GLuint ui_glyph_texture = 0; +static ui_px ui_glyph_spacing_x = 9; +static GLuint ui_glyph_texture = 0; ui_colourset ui_default_colours = { .main = 0xff00ff00, @@ -214,53 +223,40 @@ static void ui_context_free( ui_ctx *ctx ) free( ctx->indices ); } -static void ui_override_font( GLuint new_tex, ui_px space_x ) -{ - if( ui_glyph_texture ) - glDeleteTextures( 1, &ui_glyph_texture ); - - ui_glyph_texture = new_tex; - ui_glyph_override = 1; - ui_glyph_spacing_x = space_x; -} - static void ui_default_init(void) { // Load default font - if( !ui_glyph_override ) - { - 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 ); - } + u32 compressed[] = { + #include "vg/vg_pxfont.h" + }; + + u32 pixels = 0, total = 256*256, 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, 256, 256, 0, GL_RED, GL_UNSIGNED_BYTE, image ); + + vg_tex2d_clamp(); + vg_tex2d_nearest(); + + free( image ); // Setup OpenGL memory SHADER_INIT( shader_ui ); @@ -269,8 +265,7 @@ static void ui_default_init(void) static void ui_default_free(void) { - if( !ui_glyph_override ) - glDeleteTextures( 1, &ui_glyph_texture ); + glDeleteTextures( 1, &ui_glyph_texture ); ui_context_free( &ui_global_ctx ); } @@ -384,8 +379,8 @@ static void ui_new_node( ui_ctx *ctx ) if( parent->mouse_over ) { - if( ctx->mouse[0] >= node->rect[0] && ctx->mouse[0] <= node->rect[0]+node->rect[2] && - ctx->mouse[1] >= node->rect[1] && ctx->mouse[1] <= node->rect[1]+node->rect[3] ) + if( ctx->mouse[0] >= node->rect[0] && ctx->mouse[0] < node->rect[0]+node->rect[2] && + ctx->mouse[1] >= node->rect[1] && ctx->mouse[1] < node->rect[1]+node->rect[3] ) node->mouse_over = 1; else node->mouse_over = 0; @@ -557,35 +552,19 @@ static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, u32 colour, u static struct ui_vert *ui_fill_rect( ui_ctx *ctx, ui_rect rect, u32 colour ) { - return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,124,4,124 } ); + return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,4, 4,4 } ); } -static void ui_text_use_title( ui_ctx *ctx ) -{ - ctx->glyph_base = 0; -} - -static void ui_text_use_paragraph( ui_ctx *ctx ) -{ - ctx->glyph_base = 6; -} - -enum text_alignment -{ - k_text_alignment_left = 0, - k_text_alignment_center -}; - -static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_alignment alignment ) +static void ui_text( ui_ctx *ctx, const char *str, ui_px scale ) { ui_rect text_cursor; text_cursor[0] = ctx->cursor[0]; text_cursor[1] = ctx->cursor[1]; - text_cursor[2] = (scale*8)/2; - text_cursor[3] = (scale*8)/2; + text_cursor[2] = 8*scale; + text_cursor[3] = 14*scale; - u32 current_colour = ctx->override_colour; + u32 current_colour = 0x00ffffff; const char *_c = str; char c; @@ -593,27 +572,21 @@ static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_alignm { if( c == '\n' ) { - text_cursor[1] += (7*scale)/2; + text_cursor[1] += 14*scale; text_cursor[0] = ctx->cursor[0]; continue; } else if( c >= 33 && c <= 126 ) { u8 glyph_base[2]; - u8 glyph_index = c - 32; - glyph_base[0] = (glyph_index&0xf); - glyph_base[1] = ctx->glyph_base + ((glyph_index-glyph_base[0])>>4); - + u8 glyph_index = c; + glyph_base[0] = glyph_index & 0xf; + glyph_base[1] = (glyph_index-glyph_base[0])>>4; + glyph_base[0] *= 8; glyph_base[1] *= 8; - ui_fill_rect_uv( ctx, text_cursor, current_colour, - (ui_px[4]){ - glyph_base[0], - 128-glyph_base[1], - glyph_base[0]+8, - 128-(glyph_base[1]+8) - }); + ui_fill_rect_uv( ctx, text_cursor, current_colour, (ui_px[4]){glyph_base[0]+2,glyph_base[1]+1,glyph_base[0]+6,glyph_base[1]+8} ); } else if( c == '\x1B' ) { @@ -629,14 +602,14 @@ static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_alignm switch( colour_id ) { - case '0': current_colour = 0xffffffff; break; - case '3'|'1'<<8: current_colour = 0xff201fee; break; - case '3'|'2'<<8: current_colour = 0xff37e420; break; - case '3'|'3'<<8: current_colour = 0xff0ed8e2; break; - case '3'|'4'<<8: current_colour = 0xfff15010; break; - case '3'|'5'<<8: current_colour = 0xffee20ee; break; - case '3'|'6'<<8: current_colour = 0xffeeee20; break; - case '3'|'7'<<8: current_colour = 0xffffffff; break; + case '0': current_colour = 0x00ffffff; break; + case '3'|'1'<<8: current_colour = 0x00201fee; break; + case '3'|'2'<<8: current_colour = 0x0037e420; break; + case '3'|'3'<<8: current_colour = 0x000ed8e2; break; + case '3'|'4'<<8: current_colour = 0x00f15010; break; + case '3'|'5'<<8: current_colour = 0x00ee20ee; break; + case '3'|'6'<<8: current_colour = 0x00eeee20; break; + case '3'|'7'<<8: current_colour = 0x00ffffff; break; } break; @@ -650,10 +623,9 @@ static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_alignm break; } } - continue; } - text_cursor[0] += (ui_glyph_spacing_x*scale)/4; + text_cursor[0] += 9*scale; } } @@ -786,7 +758,7 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) // title.. ctx->cursor[0] += 2; ctx->cursor[1] += 2; - ui_text( ctx, window->title, 2, 0 ); + ui_text( ctx, window->title, 2 ); // Close button ctx->cursor[3] = 25; @@ -800,7 +772,7 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) vg_info( "Click clacked\n" ); } ctx->cursor[0] += 2; - ui_text( ctx, "x", 2, 0 ); + ui_text( ctx, "x", 2 ); ui_end( ctx ); if( ui_hasmouse( ctx ) )