From: hgn Date: Fri, 17 Oct 2025 14:21:35 +0000 (+0100) Subject: shaders, graphics/ui api, kv fixes, terminal entry X-Git-Url: https://skaterift.com/git/?a=commitdiff_plain;h=456b05fcd21b1fd3f4a2fd90013a75a6c3935774;p=vg.git shaders, graphics/ui api, kv fixes, terminal entry --- diff --git a/foundation.kv b/foundation.kv index 96319db..42eaefb 100644 --- a/foundation.kv +++ b/foundation.kv @@ -44,7 +44,7 @@ add source/foundation/temporary.c { if terminal - add source/terminal/main.c + add source/terminal_main.c } { diff --git a/include/common_api.h b/include/common_api.h index 0657af8..4a18eae 100644 --- a/include/common_api.h +++ b/include/common_api.h @@ -1,8 +1,5 @@ /* Voyager common application interface */ -#define API -#define IMPL - #define VG_PRE_MAIN \ _exit_init(); \ _log_init(); \ @@ -26,6 +23,7 @@ typedef double f64; typedef unsigned char bool; #define NULL 0 + #define BYTES_KB( X ) X*1024 #define BYTES_MB( X ) X*1024*1024 #define BYTES_GB( X ) X*1024*1024*1024 @@ -337,7 +335,7 @@ u32 keyvalues_type( struct keyvalues *kvs, u32 kv_offset ); c8 *keyvalues_key( struct keyvalues *kvs, u32 kv_offset, u32 *out_length ); c8 *keyvalues_value( struct keyvalues *kvs, u32 kv_offset, u32 *out_length ); -u32 keyvalues_get( struct keyvalues *kvs, u32 root_offset, const c8 *key ); +u32 keyvalues_get( struct keyvalues *kvs, u32 root_offset, const c8 *key, bool relative ); u32 keyvalues_get_next( struct keyvalues *kvs, u32 kv_offset ); u32 keyvalues_get_child( struct keyvalues *kvs, u32 root_offset, u32 index ); @@ -345,6 +343,7 @@ const c8 *keyvalues_read_string( struct keyvalues *kvs, u32 root_offset, const c bool keyvalues_read_i32s( struct keyvalues *kvs, u32 root_offset, const c8 *key, i32 *default_values, i32 *out_values, u32 len ); bool keyvalues_read_u32s( struct keyvalues *kvs, u32 root_offset, const c8 *key, u32 *default_values, u32 *out_values, u32 len ); bool keyvalues_read_f32s( struct keyvalues *kvs, u32 root_offset, const c8 *key, f32 *default_values, f32 *out_values, u32 len ); +u32 keyvalues_append_frame( struct keyvalues *kvs, u32 parent_offset, const c8 *key ); u32 keyvalues_append_string( struct keyvalues *kvs, u32 parent_offset, const c8 *key, const c8 *value ); u32 keyvalues_append_i32s( struct keyvalues *kvs, u32 parent_offset, const c8 *key, i32 *values, u32 len ); u32 keyvalues_append_u32s( struct keyvalues *kvs, u32 parent_offset, const c8 *key, u32 *values, u32 len ); diff --git a/include/engine_backend.h b/include/engine_backend.h index 887fb48..7f1621a 100644 --- a/include/engine_backend.h +++ b/include/engine_backend.h @@ -1,7 +1,7 @@ -IMPL void _engine_console_ui(void); -IMPL void _engine_console_init(void); +void _engine_console_ui(void); +void _engine_console_init(void); -IMPL void _engine_ui_init(void); -IMPL void _engine_ui_pre_render(void); -IMPL void _engine_ui_post_render(void); -IMPL void _engine_ui_input_character( u32 codepoint ); +void _engine_ui_init(void); +void _engine_ui_pre_render(void); +void _engine_ui_post_render(void); +void _engine_ui_input_character( u32 codepoint ); diff --git a/include/graphics_api.h b/include/graphics_api.h index 57f5265..e66096e 100644 --- a/include/graphics_api.h +++ b/include/graphics_api.h @@ -27,6 +27,8 @@ union colour }; #pragma pack(pop) +union colour graphics_lerp_colour( union colour c0, union colour c1, i32 d, i32 t ); + enum blending_mode { k_blending_mode_mix_alpha, @@ -46,6 +48,7 @@ void _graphics_push_blendmode( enum blending_mode mode ); void _graphics_fill_rect( i16 rect[4], union colour colour ); void _graphics_line_rect( i16 rect[4], union colour colour ); void _graphics_line( i16 p0[2], i16 p1[2], union colour colour ); +void _graphics_line2( i16 p0[2], i16 p1[2], union colour c0, union colour c1 ); void _graphics_line_triangle( i16 p0[2], i16 p1[2], i16 p2[2], union colour colour ); void _graphics_fill_triangle( i16 in_p0[2], i16 in_p1[2], i16 in_p2[2], union colour colour ); void _graphics_glyph( i16 co[2], u32 glyph, i16 scale, union colour colour ); @@ -92,6 +95,7 @@ bool _font_decode_bitmap( i16 uv[2] ); void _ui_set_mouse( i16 x, i16 y ); void _ui_get_mouse_co( i16 out_co[2] ); void _ui_input_text( const c8 *text ); +bool _ui_want_mouse( i16 area[4] ); #define UI_PADDING_PX 8 #define UI_ROW_PADDING_PX 18 diff --git a/include/input_api.h b/include/input_api.h index 5af31f6..e4f21dd 100644 --- a/include/input_api.h +++ b/include/input_api.h @@ -29,5 +29,6 @@ u8 _input_button( enum input_id id ); void _input_layer_whitelist( u32 whitelist ); bool _input_layer_filter( u32 mask ); +void _input_string( struct stream *stream, enum input_id id ); #include "generated/input.h" diff --git a/include/opengl.h b/include/opengl.h index b04c497..8e7dac3 100644 --- a/include/opengl.h +++ b/include/opengl.h @@ -1,7 +1,7 @@ #include "vg/dep/glad.4.3/glad/glad.h" #include "generated/shaders.h" -API GLuint compile_opengl_subshader( GLint type, const c8 *sources[], u32 source_count, bool critical, const c8 *debug_path ); -API bool link_opengl_program( GLuint program, bool critical ); +GLuint compile_opengl_subshader( GLint type, const c8 *sources[], u32 source_count, bool critical, const c8 *debug_path ); +bool link_opengl_program( GLuint program, bool critical ); void _shader_bind( enum shader_id id ); diff --git a/source/engine/input.c b/source/engine/input.c index ba79bec..6c9c54f 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -413,3 +413,37 @@ void _input_layer_whitelist( u32 whitelist ) { _whitelist = whitelist; } + +void _input_string( struct stream *stream, enum input_id id ) +{ + u32 matched = 0; + + for( u32 i=0; iinput_index == id ) + { + if( matched ) + $v_string( stream, {" or "} ); + + matched ++; + + for( u32 j=0; jdevice_type == k_input_device_modifier) && (bind->modifiers & alias_j->key) ) + { + $v_string( stream, {alias_j->alias}, {"+"} ); + } + } + for( u32 j=0; jdevice_type == bind->device) && (alias_j->key == bind->id) ) + { + $v_string( stream, {alias_j->alias} ); + } + } + } + } +} diff --git a/source/engine/ui.c b/source/engine/ui.c index 9ed7b11..a135bd2 100644 --- a/source/engine/ui.c +++ b/source/engine/ui.c @@ -54,7 +54,7 @@ void _engine_ui_pre_render(void) _graphics_set_target( &_ui_surface ); _graphics_viewport( 0, 0, _engine.w, _engine.h ); - _graphics_fill_rect( (i16[]){ 0, 0, _engine.w, _engine.h }, (union colour){{0,0,0,255}} ); + //_graphics_fill_rect( (i16[]){ 0, 0, _engine.w, _engine.h }, (union colour){{0,0,0,255}} ); } void _engine_ui_input_character( u32 codepoint ) @@ -65,6 +65,10 @@ void _engine_ui_input_character( u32 codepoint ) void _engine_ui_post_render(void) { + glEnable( GL_BLEND ); + glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); + glBlendEquation( GL_FUNC_ADD ); + glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, _ui_surface_texture ); glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 1920, 1080, GL_BGRA, GL_UNSIGNED_BYTE, _ui_surface.buffer ); @@ -75,4 +79,6 @@ void _engine_ui_post_render(void) _shader_blit_uInverseRatio( (f32[2]){ (f64)_engine.w/1920.0, (f64)_engine.h/1080.0 } ); glBindVertexArray( quad_vao ); glDrawArrays( GL_TRIANGLES, 0, 6 ); + + glDisable( GL_BLEND ); } diff --git a/source/foundation/allocator_stretchy.c b/source/foundation/allocator_stretchy.c index 86a099a..a7568db 100644 --- a/source/foundation/allocator_stretchy.c +++ b/source/foundation/allocator_stretchy.c @@ -44,6 +44,7 @@ void stretchy_free( struct stretchy_allocator *stretchy ) c += 1 << (SMALL_SEGMENTS + i); stretchy->segments[i] = NULL; } + stretchy->count = 0; } void stretchy_shrink( struct stretchy_allocator *stretchy ) diff --git a/source/foundation/keyvalues.c b/source/foundation/keyvalues.c index 039982c..e1cab13 100644 --- a/source/foundation/keyvalues.c +++ b/source/foundation/keyvalues.c @@ -99,6 +99,11 @@ u32 keyvalues_append_string( struct keyvalues *kvs, u32 parent_offset, const c8 return kv_offset; } +u32 keyvalues_append_frame( struct keyvalues *kvs, u32 parent_offset, const c8 *key ) +{ + return keyvalues_append_string( kvs, parent_offset, key, NULL ); +} + u32 keyvalues_type( struct keyvalues *kvs, u32 kv_offset ) { struct keyvalue *kv = stack_pointer( kvs->stack, kv_offset ); @@ -163,13 +168,19 @@ u32 keyvalues_get_child( struct keyvalues *kvs, u32 root_offset, u32 index ) else return 0; } -u32 keyvalues_get( struct keyvalues *kvs, u32 root_offset, const c8 *key ) +u32 keyvalues_get( struct keyvalues *kvs, u32 root_offset, const c8 *key, bool relative ) { if( root_offset == 0 ) root_offset = kvs->root_offset; u32 hash = buffer_djb2( key, 0 ); - u32 child_offset = keyvalues_get_child( kvs, root_offset, 0 ); + u32 child_offset; + + if( relative ) + child_offset = keyvalues_get_next( kvs, root_offset ); + else + child_offset = keyvalues_get_child( kvs, root_offset, 0 ); + while( child_offset ) { struct keyvalue *kv = stack_pointer( kvs->stack, child_offset ); @@ -197,7 +208,7 @@ bool keyvalues_read_i32s( struct keyvalues *kvs, u32 root_offset, const c8 *key, bool good = 1; u32 value_length; - char *value = keyvalues_value( kvs, keyvalues_get( kvs, root_offset, key ), &value_length ); + char *value = keyvalues_value( kvs, keyvalues_get( kvs, root_offset, key, 0 ), &value_length ); struct stream s; stream_open_buffer_read( &s, value, value_length, 0 ); @@ -221,7 +232,7 @@ bool keyvalues_read_u32s( struct keyvalues *kvs, u32 root_offset, const c8 *key, bool good = 1; u32 value_length; - char *value = keyvalues_value( kvs, keyvalues_get( kvs, root_offset, key ), &value_length ); + char *value = keyvalues_value( kvs, keyvalues_get( kvs, root_offset, key, 0 ), &value_length ); struct stream s; stream_open_buffer_read( &s, value, value_length, 0 ); @@ -245,7 +256,7 @@ bool keyvalues_read_f32s( struct keyvalues *kvs, u32 root_offset, const c8 *key, bool good = 1; u32 value_length; - char *value = keyvalues_value( kvs, keyvalues_get( kvs, root_offset, key ), &value_length ); + char *value = keyvalues_value( kvs, keyvalues_get( kvs, root_offset, key, 0 ), &value_length ); struct stream s; stream_open_buffer_read( &s, value, value_length, 0 ); @@ -266,35 +277,33 @@ bool keyvalues_read_f32s( struct keyvalues *kvs, u32 root_offset, const c8 *key, const c8 *keyvalues_read_string( struct keyvalues *kvs, u32 root_offset, const c8 *key, const c8 *default_value ) { - const c8 *value = keyvalues_value( kvs, keyvalues_get( kvs, root_offset, key ), NULL ); + const c8 *value = keyvalues_value( kvs, keyvalues_get( kvs, root_offset, key, 0 ), NULL ); return value? value: default_value; } -#define KV_APPEND_TEMPLATE( FUNCTION, ... ) \ - u32 temp_frame = _start_temporary_frame(); \ - struct stream value_str; \ - stream_open_stack( &value_str, _temporary_stack_allocator(), k_stream_null_terminate ); \ - for( u32 i=0; istack, k_stream_null_terminate ); \ + for( u32 i=0; iroot_offset; + + ASSERT_CRITICAL( keyvalues_type( kvs, node ) == k_keyvalue_type_frame ); u32 kv = keyvalues_get_child( kvs, node, 0 ); while( kv ) { @@ -480,10 +537,30 @@ void keyvalues_write_stream( struct keyvalues *kvs, struct stream *out_stream, u { for( u32 i=0; istream, (c8[]){ ' ' }, 1 ); } -static void vg_kv_write_string( vg_kv_write *w, const c8 *string, u32 length ) -{ - if( length == 0 ) - length = 0xffffffff; - - u32 i=0; - char delim=0; - for( ;istream, (c8[]){ delim }, 1 ); - vg_stream_write( w->stream, string, i ); - if( delim ) vg_stream_write( w->stream, (c8[]){ delim }, 1 ); -} - void vg_kv_write_block( vg_kv_write *w, const c8 *name, u32 name_length ) { vg_kv_write_indent( w ); diff --git a/source/graphics/graphics_software.c b/source/graphics/graphics_software.c index 1f125d8..660c221 100644 --- a/source/graphics/graphics_software.c +++ b/source/graphics/graphics_software.c @@ -169,6 +169,31 @@ void _graphics_line( i16 p0[2], i16 p1[2], union colour colour ) _graphics_pixel( wl, colour ); } +union colour graphics_lerp_colour( union colour c0, union colour c1, i32 d, i32 t ) +{ + if( t == 0 ) return c0; + if( t == d ) return c1; + if( d == 0 ) return c0; + + return (union colour){{ (i32)c0.r + (((i32)c1.r - (i32)c0.r)*d*t) / (d*d), + (i32)c0.g + (((i32)c1.g - (i32)c0.g)*d*t) / (d*d), + (i32)c0.b + (((i32)c1.b - (i32)c0.b)*d*t) / (d*d), + (i32)c0.a + (((i32)c1.a - (i32)c0.a)*d*t) / (d*d) }}; +} + +void _graphics_line2( i16 p0[2], i16 p1[2], union colour c0, union colour c1 ) +{ + struct bresenham b; + bresenham_init( &b, p0, p1 ); + i16 wl[2]; + while( bresenham_iter( &b, wl ) ) + { + i32 d = b.delta[0], + t = b.x; + _graphics_pixel( wl, graphics_lerp_colour( c0, c1, d, t ) ); + } +} + void _graphics_line_triangle( i16 p0[2], i16 p1[2], i16 p2[2], union colour colour ) { _graphics_line( p0, p1, colour ); diff --git a/source/graphics/ui.c b/source/graphics/ui.c index e33a72c..11eb47f 100644 --- a/source/graphics/ui.c +++ b/source/graphics/ui.c @@ -12,6 +12,7 @@ struct //i16 clipping_area[4]; i16 mouse_co[2], mouse_co_clicked[2]; + bool mouse_went_in_click_hole; /* internal state */ enum ui_control_type @@ -329,11 +330,11 @@ static void _ui_textbox_input_string( const c8 *input ) } for( i32 i=0; i