its ot'
[vg.git] / src / vg / vg_input.h
index 2506ba412d7686cde0b8fba74c863aea89e6eccf..76b3b95e7781934783363c6cb3345f0cf526661e 100644 (file)
@@ -1,4 +1,6 @@
 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
+#ifndef VG_INPUT_H
+#define VG_INPUT_H
 
 static inline float vg_get_axis( const char *axis );
 static inline int vg_get_button( const char *button );
@@ -45,11 +47,11 @@ static struct button_binding
 {
        const char *name;
        int bind;
-   int controller;
        
        int value; int prev;
 }
-vg_button_binds[];
+vg_button_binds[],
+vg_controller_binds[];
 
 #include "vg_config.h"
 
@@ -59,43 +61,72 @@ vg_button_binds[];
 static inline float vg_get_axis( const char *axis )
 {
        for( int i = 0; i < vg_list_size( vg_axis_binds ); i ++ )
-       {
                if( !strcmp( axis, vg_axis_binds[i].name ) )
-               {
                        return vg_axis_binds[i].value;
-               }
-       }
 }
 
 static inline struct button_binding *vg_get_button_ptr( const char *button )
 {
        for( int i=0; i<vg_list_size(vg_button_binds); i ++ )
-       {
                if( !strcmp(button,vg_button_binds[i].name) )
-               {
                        return vg_button_binds + i;
-               }
-       }
+   return NULL;
+}
+
+static inline struct button_binding *vg_get_button_ptr_c( const char *button )
+{
+       for( int i=0; i<vg_list_size(vg_controller_binds); i ++ )
+               if( !strcmp(button,vg_controller_binds[i].name) )
+                       return vg_controller_binds + i;
+   return NULL;
 }
+
 #pragma GCC diagnostic pop
 
 static int vg_console_enabled(void);
 
+static inline void vg_get_button_states( const char *name, int *cur, int *prev )
+{
+       struct button_binding *bind = vg_get_button_ptr( name ),
+                        *bindc = vg_get_button_ptr_c( name );
+   
+   *cur = 0; *prev = 0;
+
+   if( bind ) 
+   {
+      *cur |= bind->value;
+      *prev |= bind->prev;
+   }
+
+   if( bindc ) 
+   {
+      *cur |= bindc->value;
+      *prev |= bindc->prev;
+   }
+}
+
 static inline int vg_get_button( const char *button )
 {
-       return vg_get_button_ptr( button )->value && !vg_console_enabled();
+   int cur, prev;
+   vg_get_button_states( button, &cur, &prev );
+
+       return cur && !vg_console_enabled();
 }
 
 static inline int vg_get_button_down( const char *button )
 {
-       struct button_binding *bind = vg_get_button_ptr( button );
-       return bind->value & (bind->value ^ bind->prev) && !vg_console_enabled();
+   int cur, prev;
+   vg_get_button_states( button, &cur, &prev );
+
+       return cur & (cur ^ prev) && !vg_console_enabled();
 }
 
 static inline int vg_get_button_up( const char *button )
 {
-       struct button_binding *bind = vg_get_button_ptr( button );
-       return bind->prev & (bind->value ^ bind->prev) && !vg_console_enabled();
+   int cur, prev;
+   vg_get_button_states( button, &cur, &prev );
+
+       return prev & (cur ^ prev) && !vg_console_enabled();
 }
 
 static inline enum vg_button_state vg_get_button_state( const char *button )
@@ -133,12 +164,15 @@ void vg_update_inputs(void)
        {
                struct button_binding *binding = vg_button_binds + i;
                binding->prev = binding->value;
-          
-      if( binding->controller )
-         binding->value = vg_gamepad.buttons[ binding->controller ];
-      else
-         binding->value = get_button_cross_device( binding->bind );
+      binding->value = get_button_cross_device( binding->bind );
        }
+
+   for( int i=0; i<vg_list_size( vg_controller_binds ); i++ )
+   {
+               struct button_binding *binding = vg_controller_binds + i;
+               binding->prev = binding->value;
+      binding->value = vg_gamepad.buttons[ binding->bind ];
+   }
        
        /* Update axis inputs */
        for( int i = 0; i < vg_list_size( vg_axis_binds ); i ++ )
@@ -169,3 +203,5 @@ static void vg_gamepad_init(void)
       }
    }
 }
+
+#endif