From 28a5544cd43b3f1b86ab923c155d73d6ae31e2ea Mon Sep 17 00:00:00 2001 From: Faith Lain Void Date: Tue, 12 Oct 2021 18:29:03 -0400 Subject: [PATCH] v0.8.2.1 - Gyro Alpha Build Modified sdl_input_nx.c to implement basic gyro support. Still a touch janky (leaning left + right on a wireless controller feels wonky sometimes). Speed can be adjusted by editing the mouse speed in-game. --- .gitignore | 2 + Makefile.nx | 2 +- code/q3_ui/ui_controls2.c | 40 ++++++++++++++++ code/sdl/sdl_input_nx.c | 96 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c751034c..a634d1e1 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,5 @@ VoyagerNX.nso VoyagerNX.nro VoyagerNX.nacp VoyagerNX.elf +code/sdl/backup/sdl_input_nx.c +code/q3_ui/bak/ui_controls2.c diff --git a/Makefile.nx b/Makefile.nx index 60c47a4b..8d1b70df 100644 --- a/Makefile.nx +++ b/Makefile.nx @@ -32,7 +32,7 @@ include $(DEVKITPRO)/libnx/switch_rules #--------------------------------------------------------------------------------- APP_TITLE := VoyagerNX APP_AUTHOR := faithvoid -APP_VERSION := 0.8.2 +APP_VERSION := 0.8.2.1 TARGET := VoyagerNX BUILD := build diff --git a/code/q3_ui/ui_controls2.c b/code/q3_ui/ui_controls2.c index f5806a19..137f5f73 100644 --- a/code/q3_ui/ui_controls2.c +++ b/code/q3_ui/ui_controls2.c @@ -122,6 +122,8 @@ typedef struct #define ID_JOYENABLE 40 #define ID_JOYTHRESHOLD 41 #define ID_SMOOTHMOUSE 42 +#define ID_GYROENABLE 43 +#define ID_INVERTGYRO 44 #define ANIM_IDLE 0 #define ANIM_RUN 1 @@ -209,6 +211,8 @@ typedef struct menuaction_s togglemenu; menuradiobutton_s joyenable; menuslider_s joythreshold; + menuradiobutton_s gyroenable; + menuradiobutton_s gyroinvert; int section; qboolean waitingforkey; char playerModel[64]; @@ -277,6 +281,8 @@ static configcvar_t g_configcvars[] = {"joy_threshold", 0, 0}, {"m_filter", 0, 0}, {"cl_freelook", 0, 0}, + {"in_gyromouse", 0, 0}, + {"in_gyromouse_pitch", 0, 0}, {NULL, 0, 0} }; @@ -325,6 +331,8 @@ static menucommon_s *g_looking_controls[] = { (menucommon_s *)&s_controls.zoomview, (menucommon_s *)&s_controls.joyenable, (menucommon_s *)&s_controls.joythreshold, + (menucommon_s *)&s_controls.gyroenable, + (menucommon_s *)&s_controls.gyroinvert, NULL, }; @@ -815,6 +823,8 @@ static void Controls_GetConfig( void ) s_controls.joyenable.curvalue = UI_ClampCvar( 0, 1, Controls_GetCvarValue( "in_joystick" ) ); s_controls.joythreshold.curvalue = UI_ClampCvar( 0.05f, 0.75f, Controls_GetCvarValue( "joy_threshold" ) ); s_controls.freelook.curvalue = UI_ClampCvar( 0, 1, Controls_GetCvarValue( "cl_freelook" ) ); + s_controls.gyroenable.curvalue = UI_ClampCvar( 0, 1, Controls_GetCvarValue( "in_gyromouse" ) ); + s_controls.gyroinvert.curvalue = Controls_GetCvarValue( "in_gyromouse_pitch" ) < 0; } /* @@ -857,6 +867,13 @@ static void Controls_SetConfig( void ) trap_Cvar_SetValue( "in_joystick", s_controls.joyenable.curvalue ); trap_Cvar_SetValue( "joy_threshold", s_controls.joythreshold.curvalue ); trap_Cvar_SetValue( "cl_freelook", s_controls.freelook.curvalue ); + + trap_Cvar_SetValue( "in_gyromouse", s_controls.gyroenable.curvalue ); + if ( s_controls.gyroinvert.curvalue ) + trap_Cvar_SetValue( "in_gyromouse_pitch", -fabs( trap_Cvar_VariableValue( "in_gyromouse_pitch" ) ) ); + else + trap_Cvar_SetValue( "in_gyromouse_pitch", fabs( trap_Cvar_VariableValue( "in_gyromouse_pitch" ) ) ); + trap_Cmd_ExecuteText( EXEC_APPEND, "in_restart\n" ); } @@ -891,6 +908,8 @@ static void Controls_SetDefaults( void ) s_controls.joyenable.curvalue = Controls_GetCvarDefault( "in_joystick" ); s_controls.joythreshold.curvalue = Controls_GetCvarDefault( "joy_threshold" ); s_controls.freelook.curvalue = Controls_GetCvarDefault( "cl_freelook" ); + s_controls.gyroenable.curvalue = Controls_GetCvarDefault( "in_gyromouse" ); + s_controls.gyroinvert.curvalue = Controls_GetCvarDefault( "in_gyromouse_pitch" ) < 0; } /* @@ -1121,6 +1140,8 @@ static void Controls_MenuEvent( void* ptr, int event ) case ID_AUTOSWITCH: case ID_JOYENABLE: case ID_JOYTHRESHOLD: + case ID_GYROENABLE: + case ID_INVERTGYRO: if (event == QM_ACTIVATED) { s_controls.changesmade = qtrue; @@ -1560,6 +1581,22 @@ static void Controls_MenuInit( void ) s_controls.joythreshold.maxvalue = 0.75f; s_controls.joythreshold.generic.statusbar = Controls_StatusBar; + s_controls.gyroenable.generic.type = MTYPE_RADIOBUTTON; + s_controls.gyroenable.generic.flags = QMF_SMALLFONT; + s_controls.gyroenable.generic.x = SCREEN_WIDTH/2; + s_controls.gyroenable.generic.name = "gyro aiming"; + s_controls.gyroenable.generic.id = ID_GYROENABLE; + s_controls.gyroenable.generic.callback = Controls_MenuEvent; + s_controls.gyroenable.generic.statusbar = Controls_StatusBar; + + s_controls.gyroinvert.generic.type = MTYPE_RADIOBUTTON; + s_controls.gyroinvert.generic.flags = QMF_SMALLFONT; + s_controls.gyroinvert.generic.x = SCREEN_WIDTH/2; + s_controls.gyroinvert.generic.name = "invert gyro pitch"; + s_controls.gyroinvert.generic.id = ID_INVERTGYRO; + s_controls.gyroinvert.generic.callback = Controls_MenuEvent; + s_controls.gyroinvert.generic.statusbar = Controls_StatusBar; + s_controls.name.generic.type = MTYPE_PTEXT; s_controls.name.generic.flags = QMF_CENTER_JUSTIFY|QMF_INACTIVE; s_controls.name.generic.x = 320; @@ -1591,6 +1628,9 @@ static void Controls_MenuInit( void ) Menu_AddItem( &s_controls.menu, &s_controls.joyenable ); Menu_AddItem( &s_controls.menu, &s_controls.joythreshold ); + Menu_AddItem( &s_controls.menu, &s_controls.gyroenable ); + Menu_AddItem( &s_controls.menu, &s_controls.gyroinvert ); + Menu_AddItem( &s_controls.menu, &s_controls.alwaysrun ); Menu_AddItem( &s_controls.menu, &s_controls.run ); Menu_AddItem( &s_controls.menu, &s_controls.walkforward ); diff --git a/code/sdl/sdl_input_nx.c b/code/sdl/sdl_input_nx.c index e58e47cd..d6a56e79 100644 --- a/code/sdl/sdl_input_nx.c +++ b/code/sdl/sdl_input_nx.c @@ -46,6 +46,15 @@ static qboolean mouseActive = qfalse; static cvar_t *in_mouse = NULL; static cvar_t *in_nograb; +static cvar_t *in_gyromouse = NULL; +static cvar_t *in_gyromouse_pitch = NULL; // Negative values invert +static cvar_t *in_gyromouse_yaw = NULL; // Negative values invert +static cvar_t *in_gyromouse_pitch_ui = NULL; // Negative values invert (in-menu) +static cvar_t *in_gyromouse_yaw_ui = NULL; // Negative values invert (in-menu) +static cvar_t *in_gyromouse_yaw_axis = NULL; // 0 = .y is yaw, 1 = .z is yaw +static cvar_t *in_gyromouse_debug = NULL; +static HidSixAxisSensorHandle sixaxis_handles[4]; + static cvar_t *in_joystick = NULL; static cvar_t *in_joystickThreshold = NULL; static cvar_t *in_joystickNo = NULL; @@ -1282,6 +1291,79 @@ static void IN_ProcessEvents( void ) } } +/* +=============== +IN_InitGyro +=============== +*/ +void IN_InitGyro( void ) +{ + hidGetSixAxisSensorHandles(&sixaxis_handles[0], 2, HidNpadIdType_No1, HidNpadStyleTag_NpadJoyDual); + hidGetSixAxisSensorHandles(&sixaxis_handles[2], 1, HidNpadIdType_No1, HidNpadStyleTag_NpadFullKey); + hidGetSixAxisSensorHandles(&sixaxis_handles[3], 1, HidNpadIdType_Handheld, HidNpadStyleTag_NpadHandheld); + hidStartSixAxisSensor(sixaxis_handles[0]); + hidStartSixAxisSensor(sixaxis_handles[1]); + hidStartSixAxisSensor(sixaxis_handles[2]); + hidStartSixAxisSensor(sixaxis_handles[3]); +} + + +/* +=============== +IN_ProcessGyro +=============== +*/ +void IN_ProcessGyro( void ) +{ + if( in_gyromouse->integer ) { + HidSixAxisSensorState sixaxis = { 0 }; + const u64 stylemask = hidGetNpadStyleSet(HidNpadIdType_No1) | hidGetNpadStyleSet(HidNpadIdType_Handheld); + size_t numstates = 0; + if (stylemask & HidNpadStyleTag_NpadHandheld) + numstates = hidGetSixAxisSensorStates(sixaxis_handles[3], &sixaxis, 1); + else if (stylemask & HidNpadStyleTag_NpadFullKey) + numstates = hidGetSixAxisSensorStates(sixaxis_handles[2], &sixaxis, 1); + else if (stylemask & HidNpadStyleTag_NpadJoyDual) // hope to god right joycon is connected + numstates = hidGetSixAxisSensorStates(sixaxis_handles[1], &sixaxis, 1); + if (numstates) + + if ( in_gyromouse_debug->integer ) { + Com_Printf("Acceleration: x=% .4f, y=% .4f, z=% .4f\n", sixaxis.angular_velocity.x, sixaxis.angular_velocity.y, sixaxis.angular_velocity.z); + Com_Printf("Direction matrix:\n" + " [ % .4f, % .4f, % .4f ]\n" + " [ % .4f, % .4f, % .4f ]\n" + " [ % .4f, % .4f, % .4f ]\n", + sixaxis.direction.direction[0][3], sixaxis.direction.direction[1][3], sixaxis.direction.direction[2][3], + sixaxis.direction.direction[0][2], sixaxis.direction.direction[1][2], sixaxis.direction.direction[2][2], + sixaxis.direction.direction[0][0], sixaxis.direction.direction[1][0], sixaxis.direction.direction[2][0]); + } + + float pitch = sixaxis.angular_velocity.x; + float yaw = in_gyromouse_yaw_axis->integer ? sixaxis.angular_velocity.z : sixaxis.angular_velocity.y; + if( clc.state == CA_DISCONNECTED || clc.state == CA_CINEMATIC || ( Key_GetCatcher( ) & KEYCATCH_UI ) ) { + pitch *= in_gyromouse_pitch_ui->value; + yaw *= in_gyromouse_yaw_ui->value; + } else { + pitch *= in_gyromouse_pitch->value; + yaw *= in_gyromouse_yaw->value; + } + + Com_QueueEvent( in_eventTime, SE_MOUSE, yaw, pitch, 0, NULL ); + } +} + +/* +=============== +IN_ShutdownGyro +=============== +*/ +void IN_ShutdownGyro( void ) +{ + hidStopSixAxisSensor(sixaxis_handles[0]); + hidStopSixAxisSensor(sixaxis_handles[1]); + hidStopSixAxisSensor(sixaxis_handles[2]); + hidStopSixAxisSensor(sixaxis_handles[3]); +} /* =============== @@ -1320,6 +1402,8 @@ void IN_Frame( void ) IN_ProcessEvents( ); + IN_ProcessGyro( ); + // Set event time for next frame to earliest possible time an event could happen in_eventTime = Sys_Milliseconds( ); @@ -1359,6 +1443,14 @@ void IN_Init( void *windowData ) in_mouse = Cvar_Get( "in_mouse", "1", CVAR_ARCHIVE ); in_nograb = Cvar_Get( "in_nograb", "0", CVAR_ARCHIVE ); + in_gyromouse = Cvar_Get( "in_gyromouse", "0", CVAR_ARCHIVE ); + in_gyromouse_debug = Cvar_Get( "in_gyromouse_debug", "0", CVAR_ARCHIVE ); + in_gyromouse_yaw_axis = Cvar_Get( "in_gyromouse_yaw_axis", "0", CVAR_ARCHIVE ); + in_gyromouse_pitch = Cvar_Get( "in_gyromouse_pitch", "-10.0", CVAR_ARCHIVE ); + in_gyromouse_yaw = Cvar_Get( "in_gyromouse_yaw", "-20.0", CVAR_ARCHIVE ); + in_gyromouse_pitch_ui = Cvar_Get( "in_gyromouse_pitch_ui", "0.0", CVAR_ARCHIVE ); + in_gyromouse_yaw_ui = Cvar_Get( "in_gyromouse_yaw_ui", "0.0", CVAR_ARCHIVE ); + in_joystick = Cvar_Get( "in_joystick", "1", CVAR_ARCHIVE|CVAR_LATCH ); in_joystickThreshold = Cvar_Get( "joy_threshold", "0.15", CVAR_ARCHIVE ); @@ -1371,6 +1463,8 @@ void IN_Init( void *windowData ) IN_InitJoystick( ); + IN_InitGyro( ); + IN_InitKeys( ); Com_DPrintf( "------------------------------------\n" ); @@ -1390,6 +1484,8 @@ void IN_Shutdown( void ) IN_ShutdownJoystick( ); + IN_ShutdownGyro( ); + SDL_window = NULL; }