From c4e640773aac3e864dc45b86f22ee21715119a7d Mon Sep 17 00:00:00 2001 From: hgn Date: Wed, 13 Oct 2021 22:24:56 +0100 Subject: [PATCH] create font/ui text system --- .gitignore | 1 + build.sh | 1 + fishladder.c | 3 + fontcomp.c | 62 +++++++++ fonts/weiholmir.h | 22 +++ vg/vg.h | 2 + vg/vg_ui.h | 346 ++++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 425 insertions(+), 12 deletions(-) create mode 100644 fontcomp.c create mode 100644 fonts/weiholmir.h diff --git a/.gitignore b/.gitignore index 4f96ea5..755fe21 100755 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ build.linux/ build.win32/ +restricted/ # ALLOW ============================ !.gitattributes diff --git a/build.sh b/build.sh index cea0e94..fb8efff 100755 --- a/build.sh +++ b/build.sh @@ -53,6 +53,7 @@ if [ "$compile_tools" = true ]; then echo "Building tools" mkdir tools -p gcc -Wall -Wstrict-aliasing=3 $lib $flags mdlcomp.c gl/glad.c -o tools/mdlcomp $libs -Wl,-rpath=./ $defines + gcc -Wall -Wstrict-aliasing=3 $lib $flags fontcomp.c gl/glad.c -o tools/fontcomp $libs -Wl,-rpath=./ $defines fi # Resources diff --git a/fishladder.c b/fishladder.c index d66cd2e..3bcd9af 100644 --- a/fishladder.c +++ b/fishladder.c @@ -415,6 +415,8 @@ void vg_start(void) } map_load( level_pack[ 0 ] ); + + ui_test_init(); } void vg_free(void) @@ -425,6 +427,7 @@ void vg_free(void) free_mesh( &world.splitter_r ); map_free(); + ui_test_free(); } static int cell_interactive( v2i co ) diff --git a/fontcomp.c b/fontcomp.c new file mode 100644 index 0000000..ea1fac9 --- /dev/null +++ b/fontcomp.c @@ -0,0 +1,62 @@ +// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved + +#define VG_TOOLS +#include "vg/vg.h" + +// Super basic model compiler +int main( int argc, char *argv[] ) +{ + if( argc < 3 ) + { + vg_error( "Need input/output files\n" ); + return 0; + } + + FILE *output; + + int x,y,n; + unsigned char *data = stbi_load( argv[1], &x, &y, &n, 4 ); + + if( data ) + { + output = fopen( argv[2], "w" ); + if( !output ) + { + vg_error( "couldn't open output for writing\n" ); + free(data); + return 0; + } + + fprintf( output, "/* Font buffer generated from source file: '%s' */\n", argv[1] ); + + u32 pixel_max = x*y; + u32 pixel = 0, chars = 0; + while(pixel_max) + { + u32 buff = 0; + for( int b = 31; b >= 0; b-- ) + { + buff |= data[pixel*4+3]>128?0x1<= pixel_max ) + { + pixel_max = 0; + break; + } + } + + fprintf( output, "%#x,", buff ); + if( (++chars) % 8 == 0 ) + fprintf( output, "\n" ); + } + + free(data); + fclose(output); + } + else + { + vg_error( "Couldn't open source file\n" ); + return 0; + } +} diff --git a/fonts/weiholmir.h b/fonts/weiholmir.h new file mode 100644 index 0000000..db2755b --- /dev/null +++ b/fonts/weiholmir.h @@ -0,0 +1,22 @@ +/* Font buffer generated from source file: 'restricted/Weiholmir_GameMaker_sheet.png' */ +0x61b001,0x198e1818,0x600061b1,0x47f99818,0x30302060,0x93ed031b,0x8303020,0x60014fe7, +0xf803030,0x6003e1,0x661b0030,0x30000001,0x4fccdb00,0x30300060,0x10ccf,0x186000, +0x1,0x8e0c78f0,0x54600000,0x19b1c6c,0xd8386000,0x31b0c,0xc1839f8,0x1c0071b, +0xc183054,0x60c00006,0x1b0c3018,0x60c003,0xc1b0c60,0xd8000040,0x30c0e1e,0x7cf06cf8, +0xe3e3870c,0x1800006c,0xc18066cd,0x8c180c00,0x6cc180c6,0xcd80003c,0xf07c71e0,0xc3878000, +0x70000c19,0xb186c18c,0x183cf00c,0xd9b186c1,0x8c180c00,0xcf0e183,0x87000800,0xf1e3, +0x87871e3c,0x787860da,0x1346cd9b,0x3060d878,0x1ad366cd,0x9b3060c0,0x1c7ad3e7,0x8c1b3870, +0xc07862e3,0x66cd9b30,0x60d86002,0xb66cd9b,0x3060c800,0x61f36787,0x1e3c6078,0x6c61f366, +0x18d99c78,0x706c6033,0x661dddb6,0x6cd86c60,0x33c61ddd,0xb66cd87c,0x61b3861f,0xdfb678d8, +0x6c61b3c6,0x1adbb660,0xf86c61b3,0x6618dbb6,0x60706c60,0xe36798d9,0x9c601878,0x71fb66d8, +0xdb337cf0,0x6cd86366,0xd8db330c,0xc06cc063,0x66dace1e,0x1cc07870,0x6366dfc4,0xc38c078, +0x186366dd,0xce0c70c0,0x6cd86363,0x9ddb0c60,0xc06c7061,0xc118db0c,0x7cf060f0,0x40060018, +0xc0060,0x30a00300,0x18000c00,0x30300000,0x79e1c3c,0x70383000,0xd9b36,0x6cd81830, +0xd9b,0x306cf80c,0x3000000f,0x9b366cc0,0xcf003e0,0x69e1c3c,0x70180180,0x18c0c00, +0x3471,0x81818c0c,0x30,0xd9e1800d,0x8c767870,0x30d9b001,0x8d8c7f6c,0xd87871b1, +0x818f0c6b,0x6cd8301d,0xb1858d8c,0x6b6cd830,0x79b1838d,0x8c6b6c70,0x3,0, +0,0x3000000,0x7c71,0xb3c78d9b,0x636cd86c,0xd9f7030d,0x9b6b6cd8,0x78d9c3c3, +0xd8e6b38,0x78607180,0xe30d8e3e,0x6c186019,0x83c18704,0x366c7000,0x30c18000,0xfc, +0x60c0c0,0x6000000,0xfc7c60c0,0xc6460000,0xfc1cc0,0xc0698600,0xfc38,0x60c0c006, +0xfc,0x7060c0c0,0x6000000,0xfc7c30c1,0x80000000,0xfc0000,0,0, +0,0, \ No newline at end of file diff --git a/vg/vg.h b/vg/vg.h index 658130b..bd518c4 100644 --- a/vg/vg.h +++ b/vg/vg.h @@ -210,6 +210,8 @@ static void vg_init( int argc, char *argv[], const char *window_name ) vg_register_exit( &vg_audio_free, "vg_audio_free" ); vg_lines_init(); vg_register_exit( &vg_lines_free, "vg_lines_free" ); + ui_default_init(); + vg_register_exit( &ui_default_free, "UI" ); vg_register(); vg_register_exit( &vg_free, "vg_free" ); diff --git a/vg/vg_ui.h b/vg/vg_ui.h index 9758d98..a9bea2e 100644 --- a/vg/vg_ui.h +++ b/vg/vg_ui.h @@ -12,6 +12,40 @@ */ +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=2) in vec4 a_colour;" // u32 + "uniform mat3 uPv;" + "" + "out vec2 aTexCoords;" + "out vec4 aColour;" + "" + "void main()" + "{" + "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );" + "aTexCoords = a_uv * 0.01388888888;" + "aColour = a_colour;" + "}", + + // FRAGMENT + "uniform sampler2D uTexGlyphs;" + "out vec4 FragColor;" + "" + "in vec2 aTexCoords;" + "in vec4 aColour;" + "" + "void main()" + "{" + "vec4 glyph = texture( uTexGlyphs, aTexCoords );" + "FragColor = aColour * vec4( 1.0, 1.0, 1.0, glyph.r );" + "}" + , + UNIFORMS({ "uPv", "uTexGlyphs" }) +) + #define UI_AUTO_FILL 0 // Types @@ -21,6 +55,15 @@ typedef u32 ui_colour; typedef ui_px ui_rect[4]; typedef struct ui_ctx ui_ctx; +#pragma pack(push,1) +struct ui_vert +{ + ui_px co[2]; + u8 uv[2]; + u32 colour; +}; +#pragma pack(pop) + struct ui_ctx { ui_px padding; @@ -34,6 +77,11 @@ struct ui_ctx } stack[ 32 ]; + struct ui_vert *verts; + u32 num_verts; + u16 *indices; + u32 num_indices; + ui_rect cursor; u32 stack_count; u32 capture_mouse_id; @@ -45,6 +93,131 @@ struct ui_ctx int click_state; // 0: released, 1: on down, 2: pressed, 3: on release }; +// Opengl +GLuint ui_glyph_texture; +GLuint ui_vao; +GLuint ui_vbo; +GLuint ui_ebo; + + + +#define UI_BUFFER_SIZE 30000 +#define UI_INDEX_SIZE 20000 + +static void ui_glyphs_load( u32 *compressed ) +{ + 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++; + } + + for( int j = 0; j < 70; j ++ ) + { + for( int i = 0; i < 70; i ++ ) + printf( "%s",image[j*72+i]?"#":" " ); + printf( "\n" ); + } + + 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 ); +} + +static void ui_default_init(void) +{ + ui_glyphs_load( (u32[]){ + #include "fonts/weiholmir.h" + }); + + 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 ); + + // XY + glVertexAttribPointer( 0, 2, GL_UNSIGNED_SHORT, GL_FALSE, sizeof( struct ui_vert ), (void *)offsetof( struct ui_vert, co ) ); + glEnableVertexAttribArray( 0 ); + + // UV + glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE, sizeof( struct ui_vert ), (void *)offsetof( struct ui_vert, uv ) ); + glEnableVertexAttribArray( 1 ); + + // COLOUR + glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof( struct ui_vert ), (void *)offsetof( struct ui_vert, colour ) ); + glEnableVertexAttribArray( 2 ); +} + +static void ui_default_free(void) +{ + glDeleteTextures( 1, &ui_glyph_texture ); + + glDeleteVertexArrays( 1, &ui_vao ); + glDeleteBuffers( 1, &ui_vbo ); + glDeleteBuffers( 1, &ui_ebo ); +} + +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); +} + // Rect controls // ========================================================== @@ -119,14 +292,14 @@ static void ui_end( ui_ctx *ctx ) static void ui_end_down( ui_ctx *ctx ) { - ui_px height = ctx->stack[ ctx->stack_count ].rect[3]; + ui_px height = ctx->stack[ ctx->stack_count-1 ].rect[3]; ui_end( ctx ); ctx->cursor[1] += height; } static void ui_end_right( ui_ctx *ctx ) { - ui_px width = ctx->stack[ ctx->stack_count ].rect[2]; + ui_px width = ctx->stack[ ctx->stack_count-1 ].rect[2]; ui_end( ctx ); ctx->cursor[0] += width; } @@ -137,6 +310,12 @@ static void ui_align_right( ui_ctx *ctx ) ctx->cursor[0] = node->rect[0] + node->rect[2] - ctx->cursor[2]; } +static void ui_align_top( ui_ctx *ctx ) +{ + struct ui_qnode *node = &ctx->stack[ ctx->stack_count-1 ]; + ctx->cursor[1] = node->rect[1] + node->rect[3] - ctx->cursor[3]; +} + static void ui_clamp_rect( ui_rect parent, ui_rect dest ) { dest[0] = vg_min( parent[0] + parent[2] - dest[2], dest[0] ); @@ -163,6 +342,122 @@ static void ui_capture_mouse( ui_ctx *ctx, u32 id ) } } +static void ui_fill_rect( ui_ctx *ctx, ui_rect rect, u32 colour ) +{ + ui_px uv[2]; + uv[0] = 66; + uv[1] = 66; + + struct ui_vert *vertices = &ctx->verts[ ctx->num_verts ]; + vertices[0].co[0] = rect[0]; + vertices[0].co[1] = rect[1]; + vertices[0].uv[0] = uv[0]; + vertices[0].uv[1] = uv[1]; + vertices[0].colour = colour; + vertices[1].co[0] = rect[0]+rect[2]; + vertices[1].co[1] = rect[1]; + vertices[1].uv[0] = uv[0]; + vertices[1].uv[1] = uv[1]; + vertices[1].colour = colour; + vertices[2].co[0] = rect[0]+rect[2]; + vertices[2].co[1] = rect[1]+rect[3]; + vertices[2].uv[0] = uv[0]; + vertices[2].uv[1] = uv[1]; + vertices[2].colour = colour; + vertices[3].co[0] = rect[0]; + vertices[3].co[1] = rect[1]+rect[3]; + vertices[3].uv[0] = uv[0]; + vertices[3].uv[1] = uv[1]; + vertices[3].colour = colour; + u16 ind_start = ctx->num_verts; + u16 *indices = &ctx->indices[ ctx->num_indices ]; + + indices[0] = ind_start+0; + indices[1] = ind_start+2; + indices[2] = ind_start+1; + + indices[3] = ind_start+0; + indices[4] = ind_start+3; + indices[5] = ind_start+2; + + ctx->num_indices += 6; + ctx->num_verts += 4; +} + +static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, int alignment ) +{ + ui_rect text_cursor; + + text_cursor[0] = ctx->cursor[0]; + text_cursor[1] = ctx->cursor[1]; + text_cursor[2] = 7*scale; + text_cursor[3] = 7*scale; + + const char *_c = str; + char c; + while( (c = *(_c ++)) ) + { + if( c == '\n' ) + { + text_cursor[1] += 10*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%10; + glyph_base[1] = (glyph_index-glyph_base[0])/10; + + glyph_base[0] *= 7; + glyph_base[1] *= 7; + + + struct ui_vert *vertices = &ctx->verts[ctx->num_verts]; + vertices[0].co[0] = text_cursor[0]; + vertices[0].co[1] = text_cursor[1]; + vertices[0].uv[0] = glyph_base[0]; + vertices[0].uv[1] = glyph_base[1]; + vertices[0].colour = 0xffffffff; + + vertices[1].co[0] = text_cursor[0]+text_cursor[2]; + vertices[1].co[1] = text_cursor[1]; + vertices[1].uv[0] = glyph_base[0]+7; + vertices[1].uv[1] = glyph_base[1]; + vertices[1].colour = 0xffffffff; + + vertices[2].co[0] = text_cursor[0]+text_cursor[2]; + vertices[2].co[1] = text_cursor[1]+text_cursor[3]; + vertices[2].uv[0] = glyph_base[0]+7; + vertices[2].uv[1] = glyph_base[1]+7; + vertices[2].colour = 0xffffffff; + + vertices[3].co[0] = text_cursor[0]; + vertices[3].co[1] = text_cursor[1]+text_cursor[3]; + vertices[3].uv[0] = glyph_base[0]; + vertices[3].uv[1] = glyph_base[1]+7; + vertices[3].colour = 0xffffffff; + + u16 ind_start = ctx->num_verts; + u16 *indices = &ctx->indices[ ctx->num_indices ]; + + indices[0] = ind_start+0; + indices[1] = ind_start+2; + indices[2] = ind_start+1; + + indices[3] = ind_start+0; + indices[4] = ind_start+3; + indices[5] = ind_start+2; + + ctx->num_indices += 6; + ctx->num_verts += 4; + } + + text_cursor[0] += 6*scale; + } +} + // API control // ==================================================================== @@ -177,6 +472,9 @@ static void ui_begin( ui_ctx *ctx, ui_px res_x, ui_px res_y ) ctx->stack[0].mouse_over = 1; ctx->stack_count = 1; + + ctx->num_verts = 0; + ctx->num_indices = 0; } static void ui_resolve( ui_ctx *ctx ) @@ -255,18 +553,27 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) ui_new_node( ctx ); { ui_capture_mouse( ctx, __COUNTER__ ); - + + ui_fill_rect( ctx, ctx->cursor, 0xff333333 ); + // Drag bar ctx->cursor[3] = 25; ui_new_node( ctx ); { ui_capture_mouse( ctx, __COUNTER__ ); + ui_fill_rect( ctx, ctx->cursor, 0xff555555 ); + // title.. + ctx->cursor[0] += 2; + ctx->cursor[1] += 2; + ui_text( ctx, window->title, 2, 0 ); // Close button + ctx->cursor[3] = 25; ctx->cursor[2] = 25; ui_align_right( ctx ); + ui_align_top( ctx ); ui_rect_pad( ctx, ctx->cursor, 4 ); if( ui_button( ctx, __COUNTER__ ) ) @@ -286,12 +593,26 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) } } } - ui_end( ctx ); + ui_end_down( ctx ); } return 1; } +ui_ctx test_ctx = { .padding = 8 }; + +static void ui_test_init(void) +{ + test_ctx.verts = (struct ui_vert *)malloc( UI_BUFFER_SIZE * sizeof(struct ui_vert) ); + test_ctx.indices = (u16*)malloc( UI_INDEX_SIZE * sizeof(u16) ); +} + +static void ui_test_free(void) +{ + free( test_ctx.verts ); + free( test_ctx.indices ); +} + static void ui_test(void) { /* @@ -311,10 +632,8 @@ static void ui_test(void) | |--------------| | [] Eternal flames 6 | | +------+--------------+-+----------------------------+-+ */ - - static ui_ctx ctx = { .padding = 8 }; - - ui_begin( &ctx, vg_window_x, vg_window_y ); + + ui_begin( &test_ctx, vg_window_x, vg_window_y ); // TODO: Find a more elegent form for this int mouse_state = 0; @@ -322,7 +641,7 @@ static void ui_test(void) if( vg_get_button_down( "primary" ) ) mouse_state = 1; if( vg_get_button_up( "primary" ) ) mouse_state = 3; - ui_set_mouse( &ctx, vg_mouse[0], vg_mouse[1], mouse_state ); + ui_set_mouse( &test_ctx, vg_mouse[0], vg_mouse[1], mouse_state ); static struct ui_window window = { @@ -330,16 +649,19 @@ static void ui_test(void) .title = "Central Market" }; - if( ui_window( &ctx, &window, __COUNTER__ ) ) + if( ui_window( &test_ctx, &window, __COUNTER__ ) ) { // Contents + ui_text( &test_ctx, "Just say anything, George, say what ever's natural,\nthe first thing that comes to your mind.\nTake that you mutated son-of-a-bitch.", 1, 0 ); } - ui_end( &ctx ); + ui_end( &test_ctx ); - ui_resolve( &ctx ); + ui_resolve( &test_ctx ); 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 } ); vg_lines_drawall( (float*)view ); + + ui_draw( &test_ctx ); } -- 2.25.1