input programs? experimental..
authorhgn <hgodden00@gmail.com>
Wed, 15 Nov 2023 10:33:13 +0000 (10:33 +0000)
committerhgn <hgodden00@gmail.com>
Wed, 15 Nov 2023 10:33:13 +0000 (10:33 +0000)
submodules/SDL_GameControllerDB
submodules/anyascii
submodules/qoi
submodules/stb
vg_input.h

index c5b4df0e1061175cb11e3ebbf8045178339864a5..6ed8d054340ee8a93a684e11360b66cd8a5c168e 160000 (submodule)
@@ -1 +1 @@
-Subproject commit c5b4df0e1061175cb11e3ebbf8045178339864a5
+Subproject commit 6ed8d054340ee8a93a684e11360b66cd8a5c168e
index eb5332d0b5e48d58397e6f27475a18e058330d23..44e971c774d9ec67ca6c1f16c5a476724821ab63 160000 (submodule)
@@ -1 +1 @@
-Subproject commit eb5332d0b5e48d58397e6f27475a18e058330d23
+Subproject commit 44e971c774d9ec67ca6c1f16c5a476724821ab63
index dfc056e813c98d307238d35f7f041a725d699dfc..b8d77df1e80b652a57f0b7270449b179a6b91f40 160000 (submodule)
@@ -1 +1 @@
-Subproject commit dfc056e813c98d307238d35f7f041a725d699dfc
+Subproject commit b8d77df1e80b652a57f0b7270449b179a6b91f40
index 5736b15f7ea0ffb08dd38af21067c314d6a3aae9..8b5f1f37b5b75829fc72d38e7b5d4bcbf8a26d55 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 5736b15f7ea0ffb08dd38af21067c314d6a3aae9
+Subproject commit 8b5f1f37b5b75829fc72d38e7b5d4bcbf8a26d55
index e9ae65b1971c9d714e174e19e1a7bc0b1552f9b2..9c5be8447ecc242f4baff8171601fbfc18f39a18 100644 (file)
@@ -8,9 +8,46 @@
 #define VG_MAX_CONTROLLERS 4
 
 static float controller_deadzone = 0.05f;
+typedef u32 vg_input_op;
+typedef vg_input_op *vg_input_program;
+
+enum vg_input_type {
+   k_vg_input_type_button_u8,
+   k_vg_input_type_axis_f32,
+   k_vg_input_type_joy_v2f
+};
+
+enum vg_input_op {
+   /* data source */
+   vg_keyboard,
+   vg_mouse,
+   vg_joy_button,
+   vg_joy_axis,
+
+   /* hacky constants */
+   vg_0_0f,
+   vg_0_5f,
+   vg_1_0f,
+   vg_2_0f,
+
+   /* modes */
+   vg_mode_mul,
+   vg_mode_sub,
+   vg_mode_add,
+   vg_mode_absmax,
+   vg_mode_max,
+
+   /* control */
+   vg_index,
+   vg_end,
+
+   /* math */
+   vg_normalize
+};
 
 struct{
    const u8 *sdl_keys;
+   u32 sdl_mouse;
 
    struct vg_controller{
       SDL_GameController *handle; /* handle for controller. NULL if unused */
@@ -89,9 +126,6 @@ static int vg_open_gamecontroller( Sint32 index )
          for( u32 i=0; i< SDL_CONTROLLER_AXIS_MAX; i++ )
             controller->axises[i] = 0.0f;
 
-         controller->axises[ SDL_CONTROLLER_AXIS_TRIGGERLEFT ] = -1.0f;
-         controller->axises[ SDL_CONTROLLER_AXIS_TRIGGERRIGHT ] = -1.0f;
-
          if( vg_input.active_controller_index == -2 ){
             vg_input.active_controller_index = vg_id;
             vg_input.display_input_method = k_input_method_controller;
@@ -173,6 +207,10 @@ static void vg_input_controller_event( SDL_Event *ev )
                
                value = vg_signf(value) * (high / (1.0f-deadz));
             }
+            else if( ev->caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT ||
+                     ev->caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT ){
+               value = 0.5f + value*0.5f;
+            }
 
             esta->axises[ ev->caxis.axis ] = value;
             break;
@@ -234,6 +272,7 @@ static void vg_process_inputs(void)
 {
    int count;
    vg_input.sdl_keys = SDL_GetKeyboardState( &count );
+   vg_input.sdl_mouse = SDL_GetMouseState(NULL,NULL);
 
    if( vg_input.display_input_method != k_input_method_kbm ){
       /* check for giving keyboard priority */
@@ -246,7 +285,7 @@ static void vg_process_inputs(void)
       }
 
       /* check for giving mouse priority */
-      if( SDL_GetMouseState(NULL,NULL) & 
+      if( vg_input.sdl_mouse & 
             (SDL_BUTTON(SDL_BUTTON_LEFT)|SDL_BUTTON(SDL_BUTTON_RIGHT)|
              SDL_BUTTON(SDL_BUTTON_MIDDLE)) )
       {
@@ -293,4 +332,113 @@ static void vg_input_free(void)
    }
 }
 
+struct vg_controller *vg_active_controller(void){
+   if( vg_input.active_controller_index >= 0 )
+      return &vg_input.controllers[vg_input.active_controller_index];
+   else
+      return NULL;
+}
+
+static u8 vg_controller_button( SDL_GameControllerButton button ){
+   struct vg_controller *c = vg_active_controller();
+   if( c ) return c->buttons[ button ];
+   else return 0;
+}
+
+static f32 vg_controller_axis( SDL_GameControllerAxis axis ){
+   struct vg_controller *c = vg_active_controller();
+   if( c ) return c->axises[ axis ];
+   else return 0;
+}
+
+static void vg_input_apply_to_u8( vg_input_op mode, u8 data, u8 *inout_result ){
+   if     ( mode == vg_mode_absmax )  *inout_result |= data;
+   else if( mode == vg_mode_mul )     *inout_result &= data;
+   else vg_fatal_error( "mode not supported for destination type (%d)", mode );
+}
+
+static void vg_input_apply_to_f32( vg_input_op mode, f32 data, 
+                                   f32 *inout_result ){
+   if     ( mode == vg_mode_absmax ){
+      if( fabsf(data) > fabsf(*inout_result) )
+         *inout_result = data;
+   }
+   else if( mode == vg_mode_max ) *inout_result = vg_maxf(*inout_result,data);
+   else if( mode == vg_mode_mul ) *inout_result *= (f32)data;
+   else if( mode == vg_mode_sub ) *inout_result -= (f32)data;
+   else if( mode == vg_mode_add ) *inout_result += (f32)data;
+   else vg_fatal_error( "mode not supported for destination type (%d)", mode );
+}
+
+static void vg_exec_input_program( enum vg_input_type type, vg_input_op *ops, 
+                                   void *out_result ){
+   u8 *out_button = NULL;
+   f32 *out_joy = NULL;
+
+   if( type == k_vg_input_type_button_u8 ){
+      out_button = out_result;
+      *out_button = 0;
+   }
+   else if( type == k_vg_input_type_axis_f32 ){
+      out_joy = out_result;
+      out_joy[0] = 0.0f;
+   }
+   else if( type == k_vg_input_type_joy_v2f ){
+      out_joy = out_result;
+      out_joy[0] = 0.0f;
+      out_joy[1] = 0.0f;
+   }
+
+   /* computer state */
+   vg_input_op mode = vg_mode_absmax;
+   u32 pc = 0, index = 0;
+
+next_code:;
+   vg_input_op op = ops[ pc ++ ];
+
+   if( (op >= vg_mode_mul) && (op <= vg_mode_max) ){
+      mode = op;
+   }
+   else if( (op == vg_keyboard) || (op == vg_mouse) || (op == vg_joy_button) ){
+      u8 state = 0;
+
+      if( op == vg_keyboard )
+         state = vg_getkey(ops[pc ++]);
+      else if( op == vg_mouse )
+         state = (vg_input.sdl_mouse & SDL_BUTTON(ops[pc ++]))?1:0;
+      else
+         state = vg_controller_button(ops[pc ++]);
+
+      if( type == k_vg_input_type_button_u8 )
+         vg_input_apply_to_u8( mode, state, out_button );
+      else
+         vg_input_apply_to_f32( mode, (f32)state, &out_joy[index] );
+   }
+   else if( op == vg_joy_axis ){
+      f32 state = vg_controller_axis( ops[pc ++] );
+      if( type == k_vg_input_type_button_u8 )
+         vg_input_apply_to_u8( mode, state>0.5f?1:0, out_button );
+      else 
+         vg_input_apply_to_f32( mode, state, &out_joy[index] );
+   }
+   else if( (op >= vg_0_0f) && (op <= vg_2_0f) ){
+      f32 value = (f32)(op - vg_0_5f) * 0.5f;
+      vg_input_apply_to_f32( mode, value, &out_joy[index] );
+   }
+   else if( op == vg_index ){
+      index = ops[pc ++];
+   }
+   else if( op == vg_end ){
+      return;
+   }
+   else if( op == vg_normalize ){
+      v2_normalize( out_joy );
+   }
+   else {
+      vg_fatal_error( "unknown op\n" );
+   }
+
+   goto next_code;
+}
+
 #endif