misc and also stretchy delete implementation
authorhgn <hgodden00@gmail.com>
Tue, 7 Oct 2025 20:49:06 +0000 (20:49 +0000)
committerhgn <hgodden00@gmail.com>
Tue, 7 Oct 2025 20:49:06 +0000 (20:49 +0000)
foundation.kv
include/common_api.h
include/graphics_api.h
source/engine/input.c
source/foundation/allocator_stretchy.c
source/graphics/graphics.c
source/graphics/ui.c
source/tools/metacompiler.c

index 259175b8af4d7692c95782dfd0efc855f36b1eca..96319dbee874784fd39944d18a692e4a663efaef 100644 (file)
@@ -201,7 +201,6 @@ add source/foundation/temporary.c
    append graphics.kv
    append glfw3.kv
 
-
    ccmd
    {
       name exec
@@ -213,8 +212,8 @@ add source/foundation/temporary.c
          description "The path to the config"
       }
    }
-
    config "bind ALT+GRAVE_ACCENT console"
+
    config "bind BACKSPACE ui_backspace"
    config "bind DELETE ui_delete"
    config "bind ENTER ui_enter"
index da6de5e4842b11598d7a022bd44adcd567515038..59965004373bf722f739065ce34ff80802e8f229 100644 (file)
@@ -47,7 +47,11 @@ static inline f32 f32_min( f32 a, f32 b ){ return a < b? a: b; }
 static inline f32 f32_max( f32 a, f32 b ){ return a > b? a: b; }
 static inline i32 i32_min( i32 a, i32 b ){ return a < b? a: b; }
 static inline i32 i32_max( i32 a, i32 b ){ return a > b? a: b; }
+static inline i16 i16_min( i16 a, i16 b ){ return a < b? a: b; }
+static inline i16 i16_max( i16 a, i16 b ){ return a > b? a: b; }
 static inline f32 f32_clamp( f32 a, f32 min, f32 max ) { return f32_min( max, f32_max( a, min ) ); }
+static inline i16 i16_clamp( i16 a, i16 min, i16 max ){ return i16_min( max, i16_max( a, min ) ); }
+static inline i16 i32_clamp( i32 a, i32 min, i32 max ){ return i32_min( max, i32_max( a, min ) ); }
 static inline f32 f32_sign( f32 a ) { return a < 0.0f? -1.0f: 1.0f; }
 
 void _exit_init(void);
@@ -89,7 +93,7 @@ struct stretchy_allocator
 void stretchy_init( struct stretchy_allocator *stretchy, u32 element_size );
 void *stretchy_append( struct stretchy_allocator *stretchy );
 void *stretchy_get( struct stretchy_allocator *stretchy, u32 index );
-void *stretchy_delete( struct stretchy_allocator *stretchy, u32 index ); // TODO
+void stretchy_delete( struct stretchy_allocator *stretchy, u32 index );
 u32 stretchy_count( struct stretchy_allocator *stretchy );
 void stretchy_free( struct stretchy_allocator *stretchy );
 
index 701e4924626f611d56abcf1b8a1497323dddc2d0..57f52650e3eeec19a0b0d4e5f800d56ae8c4680a 100644 (file)
@@ -55,6 +55,7 @@ void rect_copy( i16 a[4], i16 b[4] );
 void rect_pad( i16 rect[4], i16 padding[2] );
 void rect_split( i16 rect[4], u32 vertical, i16 width, i16 gap, i16 out_left[4], i16 out_right[4] );
 void rect_split_ratio( i16 rect[4], u32 vertical, f32 ratio, i16 gap, i16 out_left[4], i16 out_right[4] );
+bool ui_inside_rect( i16 rect[4], i16 co[2] );
 
 /* Font
  * ------------------------------------------------------------------------------------------------------------------ */
@@ -89,6 +90,7 @@ bool _font_decode_bitmap( i16 uv[2] );
 /* Immediate mode UI
  * ------------------------------------------------------------------------------------------------------------------ */
 void _ui_set_mouse( i16 x, i16 y );
+void _ui_get_mouse_co( i16 out_co[2] );
 void _ui_input_text( const c8 *text );
 
 #define UI_PADDING_PX     8
index d5e5c2382147bb1a5589c277b09f09b35db859d8..4cb6c0b4aeacf1463b930e974e6aba59c350fc20 100644 (file)
@@ -364,6 +364,7 @@ static void _input_key_callback( GLFWwindow *window, i32 key, i32 scancode, i32
 void _input_init(void)
 {
    glfwSetKeyCallback( _engine.window_handle, _input_key_callback );
+   glfwSetMouseButtonCallback( _engine.window_handle, _input_mouse_callback );
    for( u32 i=0; i<ARRAY_COUNT( _input_aliases ); i ++ )
       _input_aliases[i].alias_hash = buffer_djb2( _input_aliases[i].alias, 0 );
 }
index 6d913d03be1688cffa32bf4900d2e5638c58e3e7..37b9bf4761029cf662529c574b31be3b2ae768f1 100644 (file)
@@ -1,4 +1,4 @@
-#define SMALL_SEGMENTS 1
+#define SMALL_SEGMENTS 4
 void stretchy_init( struct stretchy_allocator *stretchy, u32 element_size )
 {
    stretchy->count = 0;
@@ -23,7 +23,10 @@ void *stretchy_append( struct stretchy_allocator *stretchy )
    i32 i, offset;
    stretchy_core( stretchy, stretchy->count ++, &i, &offset );
    if( offset == 0 )
+   {
+      $log( $ok, {"Allocated new stretchy segment: "}, $unsigned( i ), {", size: "}, $unsigned(1<<(i+SMALL_SEGMENTS)) );
       stretchy->segments[ i ] = _heap_allocate( (1<<(i+SMALL_SEGMENTS)) * stretchy->element_size );
+   }
    return stretchy->segments[ i ] + stretchy->element_size*offset;
 }
 
@@ -42,3 +45,22 @@ void stretchy_free( struct stretchy_allocator *stretchy )
       stretchy->segments[i] = NULL;
    }
 }
+
+void stretchy_delete( struct stretchy_allocator *stretchy, u32 index )
+{
+   if( (stretchy->count > 1) && (index != stretchy->count-1) )
+   {
+      buffer_copy( stretchy_get( stretchy, stretchy->count-1 ), stretchy->element_size, 
+                   stretchy_get( stretchy, index ), stretchy->element_size );
+   }
+   stretchy->count --;
+
+   i32 i, offset;
+   stretchy_core( stretchy, stretchy->count, &i, &offset );
+   if( offset == 0 )
+   {
+      $log( $ok, {"Deleted stretchy segment: "}, $unsigned(i) );
+      _heap_free( stretchy->segments[i] );
+      stretchy->segments[i] = NULL;
+   }
+}
index 7a3f4dd239112cf35d86d622f9628f621450c0b2..0a0a11f813a7e852b4b066295dde2d1b3c1d7fa5 100644 (file)
@@ -7,9 +7,6 @@ void _graphics_set_target( struct graphics_target *target )
    _graphics_target = target;
 }
 
-static i16 i16_min( i16 a, i16 b ){ return a<b?a:b; }
-static i16 i16_max( i16 a, i16 b ){ return a>b?a:b; }
-static i16 i16_clamp( i16 a, i16 min, i16 max ){ return i16_min( max, i16_max( a, min ) ); }
 void rect_clip( i16 child[4], i16 parent[4], i16 clipped[4] )
 {
    i16 px = parent[0], py = parent[1], pw = parent[2], ph = parent[3];
index 51de2f64e8c70c88a284d038b37932d5065fc1be..e33a72c5c2edb636c3111fda3c0e0c9c912faa2d 100644 (file)
@@ -55,12 +55,18 @@ struct
 }
 _ui;
 
+void _ui_get_mouse_co( i16 out_co[2] )
+{
+   out_co[0] = _ui.mouse_co[0];
+   out_co[1] = _ui.mouse_co[1];
+}
+
 void _ui_set_encoding( enum ui_text_encoding encoding )
 {
    _ui.text_encoding = encoding;
 }
 
-static bool ui_inside_rect( i16 rect[4], i16 co[2] )
+bool ui_inside_rect( i16 rect[4], i16 co[2] )
 {
    return (co[0] >= rect[0]) && (co[1] >= rect[1]) && (co[0] < rect[0]+rect[2]) && (co[1] < rect[1]+rect[3]);
 }
index c8c46e98eb912bf6058d253d62e3e7c820a8dd73..2412850731589be621c469d2af808d258eaf5a4d 100644 (file)
@@ -511,6 +511,8 @@ bool use_asan = 0;
 bool shared = 0;
 bool no_pdb = 0;
 
+u32 optimise = 0;
+
 enum libc_version 
 {
    k_libc_version_native = 0,
@@ -591,6 +593,9 @@ void _metacompiler_options(void)
       stream_open_buffer_read( &arg_stream, (void*)arg, buffer_last_index(arg,0,0)+1, 0 );
       ASSERT_CRITICAL( string_parse_u64( &arg_stream, &processors ) == k_string_parse_ok );
    }
+
+   if( _option_flag( 'O', "Run optimisers" ) )
+      optimise = 3;
    
    target_file_path = _option();
    ASSERT_CRITICAL( target_file_path );
@@ -847,6 +852,7 @@ i32 main( i32 argc, const c8 *argv[] )
 
       if( use_tsan ) $v_string( &command_string, {"   -fsanitize=thread -lasan \\\n"} );
       if( use_asan ) $v_string( &command_string, {"   -fsanitize=address -lasan \\\n"} );
+      $v_string( &command_string, { "   -O" }, $unsigned( optimise ), {" \\\n"} );
 
       $v_string( &command_string, {"   -target "}, {string_get(&tripple_string)}, {" "} );
       if( platform == k_platform_windows )