Skip to content

Commit

Permalink
v0.8.2.1 - Gyro Alpha Build
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Faith Lain Void committed Oct 12, 2021
1 parent 8a595e1 commit 28a5544
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Makefile.nx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions code/q3_ui/ui_controls2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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}
};

Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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;
}

/*
Expand Down Expand Up @@ -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" );
}

Expand Down Expand Up @@ -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;
}

/*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down
96 changes: 96 additions & 0 deletions code/sdl/sdl_input_nx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}

/*
===============
Expand Down Expand Up @@ -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( );

Expand Down Expand Up @@ -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 );

Expand All @@ -1371,6 +1463,8 @@ void IN_Init( void *windowData )

IN_InitJoystick( );

IN_InitGyro( );

IN_InitKeys( );

Com_DPrintf( "------------------------------------\n" );
Expand All @@ -1390,6 +1484,8 @@ void IN_Shutdown( void )

IN_ShutdownJoystick( );

IN_ShutdownGyro( );

SDL_window = NULL;
}

Expand Down

0 comments on commit 28a5544

Please sign in to comment.