From e1b892e8b45d7e4b5a5cd7690b8547d832a37a8d Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sat, 5 Aug 2023 03:13:56 +0200 Subject: [PATCH] port: hack in mouselook --- include/PR/os_cont.h | 6 ---- port/include/input.h | 12 +++++++- port/src/input.c | 66 ++++++++++++++++++++++++++++++-------------- src/game/bondmove.c | 42 ++++++++++++++++++++++++++++ src/game/gamefile.c | 8 ++++++ src/include/types.h | 4 +++ 6 files changed, 110 insertions(+), 28 deletions(-) diff --git a/include/PR/os_cont.h b/include/PR/os_cont.h index dd3466ad4..15ee60f3f 100644 --- a/include/PR/os_cont.h +++ b/include/PR/os_cont.h @@ -60,12 +60,6 @@ typedef struct { u16 button; s8 stick_x; /* -80 <= stick_x <= 80 */ s8 stick_y; /* -80 <= stick_y <= 80 */ -#ifndef PLATFORM_N64 /* for that sick mouselook */ - s32 mouse_x; - s32 mouse_y; - s32 mouse_dx; - s32 mouse_dy; -#endif u8 errnum; } OSContPad; diff --git a/port/include/input.h b/port/include/input.h index 513941cbe..6f0a5efe4 100644 --- a/port/include/input.h +++ b/port/include/input.h @@ -96,7 +96,17 @@ void inputRumble(s32 idx, f32 strength, f32 time); void inputLockMouse(s32 lock); // returns the current state of the above -s32 inputIsMouseLocked(void); +s32 inputMouseIsLocked(void); + +// returns mouse position in window +void inputMouseGetPosition(s32 *x, s32 *y); + +// returns changes in mouse position since last frame +void inputMouseGetDelta(s32 *dx, s32 *dy); + +// returns changes in mouse position since last frame, in normalized window coordinates scaled by sensitivity +void inputMouseGetNormalizedDelta(f32 *dx, f32 *dy); + // call this every frame void inputUpdate(void); diff --git a/port/src/input.c b/port/src/input.c index 7d319582b..e534838c3 100644 --- a/port/src/input.c +++ b/port/src/input.c @@ -3,6 +3,7 @@ #include #include #include "input.h" +#include "video.h" #define MAX_BINDS 4 #define TRIG_THRESHOLD (30 * 256) @@ -18,23 +19,26 @@ static s32 mouseX, mouseY; static s32 mouseDX, mouseDY; static u32 mouseButtons; +static f32 mouseSensX = 0.33f; +static f32 mouseSensY = 0.33f; + void inputSetDefaultKeyBinds(void) { // TODO: make VK constants for all these static const u32 kbbinds[][3] = { - { CK_A, SDL_SCANCODE_Q, 0 }, - { CK_B, SDL_SCANCODE_E, 0 }, - { CK_LTRIG, VK_MOUSE_RIGHT, SDL_SCANCODE_Z }, - { CK_RTRIG, VK_MOUSE_X1, SDL_SCANCODE_X }, - { CK_ZTRIG, VK_MOUSE_LEFT, SDL_SCANCODE_SPACE }, - { CK_START, VK_MOUSE_LEFT, SDL_SCANCODE_RETURN }, - { CK_DPAD_D, SDL_SCANCODE_DOWN, 0 }, - { CK_DPAD_U, SDL_SCANCODE_UP, 0 }, - { CK_DPAD_R, SDL_SCANCODE_RIGHT, 0 }, - { CK_DPAD_L, SDL_SCANCODE_LEFT, 0 }, - { CK_STICK_XNEG, SDL_SCANCODE_A, 0 }, - { CK_STICK_XPOS, SDL_SCANCODE_D, 0 }, - { CK_STICK_YNEG, SDL_SCANCODE_S, 0 }, - { CK_STICK_YPOS, SDL_SCANCODE_W, 0 }, + { CK_A, SDL_SCANCODE_Q, 0 }, + { CK_B, SDL_SCANCODE_E, 0 }, + { CK_RTRIG, VK_MOUSE_RIGHT, SDL_SCANCODE_Z }, + { CK_LTRIG, VK_MOUSE_X1, SDL_SCANCODE_X }, + { CK_ZTRIG, VK_MOUSE_LEFT, SDL_SCANCODE_SPACE }, + { CK_START, SDL_SCANCODE_RETURN, 0 }, + { CK_DPAD_D, SDL_SCANCODE_S, 0 }, + { CK_DPAD_U, SDL_SCANCODE_W, 0 }, + { CK_DPAD_R, SDL_SCANCODE_D, 0 }, + { CK_DPAD_L, SDL_SCANCODE_A, 0 }, + { CK_STICK_XNEG, SDL_SCANCODE_LEFT, 0 }, + { CK_STICK_XPOS, SDL_SCANCODE_RIGHT, 0 }, + { CK_STICK_YNEG, SDL_SCANCODE_DOWN, 0 }, + { CK_STICK_YPOS, SDL_SCANCODE_UP, 0 }, }; static const u32 joybinds[][2] = { @@ -129,11 +133,6 @@ s32 inputReadController(s32 idx, OSContPad *npad) npad->stick_x = xdiff < 0 ? -0x80 : (xdiff > 0 ? 0x7F : 0); npad->stick_y = ydiff < 0 ? -0x80 : (ydiff > 0 ? 0x7F : 0); - npad->mouse_x = mouseX; - npad->mouse_y = mouseY; - npad->mouse_dx = mouseDX; - npad->mouse_dy = mouseDY; - if (!pads[idx]) { return 0; } @@ -161,17 +160,25 @@ void inputUpdate(void) { SDL_GameControllerUpdate(); - int mx, my; + s32 mx, my; mouseButtons = SDL_GetMouseState(&mx, &my); + if (mouseLocked) { SDL_GetRelativeMouseState(&mouseDX, &mouseDY); } else { mouseDX = mx - mouseX; mouseDY = my - mouseY; } + mouseX = mx; mouseY = my; + if (!mouseLocked && (mouseButtons & SDL_BUTTON_LMASK)) { + inputLockMouse(1); + } else if (mouseLocked && inputKeyPressed(VK_ESCAPE)) { + inputLockMouse(0); + } + // TODO: handle controller changes } @@ -256,6 +263,23 @@ void inputLockMouse(s32 lock) { SDL_SetRelativeMouseMode(mouseLocked); } -s32 inputIsMouseLocked(void) { +s32 inputMouseIsLocked(void) { return mouseLocked; } + +void inputMouseGetPosition(s32 *x, s32 *y) { + if (x) *x = mouseX; + if (y) *y = mouseY; +} + +void inputMouseGetDelta(s32 *dx, s32 *dy) { + if (dx) *dx = mouseDX; + if (dy) *dy = mouseDY; +} + +void inputMouseGetNormalizedDelta(f32 *dx, f32 *dy) { + s32 w, h; + SDL_GetWindowSize(videoGetWindowHandle(), &w, &h); + if (dx) *dx = mouseSensX * (float)mouseDX * ((float)w / 480.f) * 0.1f; + if (dy) *dy = mouseSensY * (float)mouseDY * ((float)h / 480.f) * 0.1f; +} diff --git a/src/game/bondmove.c b/src/game/bondmove.c index 8e1c2893e..3a873c3e8 100644 --- a/src/game/bondmove.c +++ b/src/game/bondmove.c @@ -38,6 +38,9 @@ #include "lib/anim.h" #include "data.h" #include "types.h" +#ifndef PLATFORM_N64 +#include "input.h" +#endif void bmoveSetControlDef(u32 controldef) { @@ -571,6 +574,10 @@ void bmoveResetMoveData(struct movedata *data) data->analogpitch = 0; data->analogstrafe = 0; data->analogwalk = 0; +#ifndef PLATFORM_N64 + data->freelookdx = 0.0f; + data->freelookdy = 0.0f; +#endif } /** @@ -688,6 +695,12 @@ void bmoveProcessInput(bool allowc1x, bool allowc1y, bool allowc1buttons, bool i movedata.analogpitch = movedata.c1stickysafe; movedata.analogwalk = movedata.c1stickysafe; +#ifndef PLATFORM_N64 + if ((PLAYERCOUNT() == 1) && inputMouseIsLocked()) { + inputMouseGetNormalizedDelta(&movedata.freelookdx, &movedata.freelookdy); + } +#endif + // Pausing if (g_Vars.currentplayer->isdead == false) { if (g_Vars.currentplayer->pausemode == PAUSEMODE_UNPAUSED && (c1buttonsthisframe & START_BUTTON)) { @@ -781,6 +794,10 @@ void bmoveProcessInput(bool allowc1x, bool allowc1y, bool allowc1buttons, bool i movedata.analogwalk = g_Vars.currentplayer->autocontrol_y; movedata.analogturn = g_Vars.currentplayer->autocontrol_x; movedata.analogpitch = 0; +#ifndef PLATFORM_N64 + movedata.freelookdx = 0.0f; + movedata.freelookdy = 0.0f; +#endif } if (controlmode == CONTROLMODE_21 || controlmode == CONTROLMODE_22) { @@ -1155,6 +1172,11 @@ void bmoveProcessInput(bool allowc1x, bool allowc1y, bool allowc1buttons, bool i movedata.speedvertaup = 0; movedata.cannaturalturn = !g_Vars.currentplayer->insightaimmode; +#ifndef PLATFORM_N64 + movedata.cannaturalpitch = movedata.cannaturalpitch || (movedata.freelookdy != 0.0f); + movedata.cannaturalturn = movedata.cannaturalturn || (movedata.freelookdx != 0.0f); +#endif + if (g_Vars.tickmode == TICKMODE_AUTOWALK) { movedata.digitalstepforward = (g_Vars.currentplayer->autocontrol_y > 0); movedata.digitalstepback = (g_Vars.currentplayer->autocontrol_y < 0); @@ -1162,6 +1184,10 @@ void bmoveProcessInput(bool allowc1x, bool allowc1y, bool allowc1buttons, bool i movedata.analogwalk = 0; movedata.analogturn = g_Vars.currentplayer->autocontrol_x; movedata.analogpitch = 0; +#ifndef PLATFORM_N64 + movedata.freelookdx = 0.0f; + movedata.freelookdy = 0.0f; +#endif } } else { // 1.1 or 1.3 @@ -1200,6 +1226,10 @@ void bmoveProcessInput(bool allowc1x, bool allowc1y, bool allowc1buttons, bool i movedata.analogwalk = g_Vars.currentplayer->autocontrol_y; movedata.analogturn = g_Vars.currentplayer->autocontrol_x; movedata.analogpitch = 0; +#ifndef PLATFORM_N64 + movedata.freelookdx = 0.0f; + movedata.freelookdy = 0.0f; +#endif } } @@ -1668,6 +1698,12 @@ void bmoveProcessInput(bool allowc1x, bool allowc1y, bool allowc1buttons, bool i fVar25 *= -fVar25; } +#ifndef PLATFORM_N64 + if (movedata.freelookdy) { + fVar25 += tmp * movedata.freelookdy; + } +#endif + g_Vars.currentplayer->speedverta = -fVar25 * tmp; } else if (movedata.speedvertadown > 0) { bmoveUpdateSpeedVerta(movedata.speedvertadown); @@ -1705,6 +1741,12 @@ void bmoveProcessInput(bool allowc1x, bool allowc1y, bool allowc1buttons, bool i fVar25 *= -fVar25; } +#ifndef PLATFORM_N64 + if (movedata.freelookdx) { + fVar25 += tmp * movedata.freelookdx; + } +#endif + g_Vars.currentplayer->speedthetacontrol = fVar25 * tmp; } else if (movedata.aimturnleftspeed > 0) { bmoveUpdateSpeedThetaControl(movedata.aimturnleftspeed); diff --git a/src/game/gamefile.c b/src/game/gamefile.c index 5061e77d9..70b6a61a1 100644 --- a/src/game/gamefile.c +++ b/src/game/gamefile.c @@ -160,7 +160,11 @@ void gamefileLoadDefaults(struct gamefile *file) optionsSetMusicVolume(0x7f80); #endif sndSetSoundMode(SOUNDMODE_STEREO); +#ifdef PLATFORM_N64 optionsSetControlMode(player1, CONTROLMODE_11); +#else + optionsSetControlMode(player1, CONTROLMODE_12); +#endif optionsSetControlMode(player2, CONTROLMODE_11); pakClearAllBitflags(file->flags); @@ -168,7 +172,11 @@ void gamefileLoadDefaults(struct gamefile *file) pakSetBitflag(GAMEFILEFLAG_P1_AUTOAIM, file->flags, true); pakSetBitflag(GAMEFILEFLAG_P1_AIMCONTROL, file->flags, AIMCONTROL_HOLD); pakSetBitflag(GAMEFILEFLAG_P1_SIGHTONSCREEN, file->flags, true); +#ifdef PLATFORM_N64 pakSetBitflag(GAMEFILEFLAG_P1_LOOKAHEAD, file->flags, true); +#else + pakSetBitflag(GAMEFILEFLAG_P1_LOOKAHEAD, file->flags, false); +#endif pakSetBitflag(GAMEFILEFLAG_P1_AMMOONSCREEN, file->flags, true); pakSetBitflag(GAMEFILEFLAG_P1_HEADROLL, file->flags, true); pakSetBitflag(GAMEFILEFLAG_P1_SHOWGUNFUNCTION, file->flags, true); diff --git a/src/include/types.h b/src/include/types.h index 0de5f89f9..f9399ad1f 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -5278,6 +5278,10 @@ struct movedata { /*0xa0*/ s32 analogpitch; /*0xa4*/ s32 analogstrafe; /*0xa8*/ s32 analogwalk; +#ifndef PLATFORM_N64 + /* */ f32 freelookdx; // how much the mouse moved ... + /* */ f32 freelookdy; // ... in normalized window coordinates +#endif }; struct attackanimgroup {