Skip to content

Commit

Permalink
Merge branch 'port' of https://github.com/fgsfdsfgs/perfect_dark into…
Browse files Browse the repository at this point in the history
… port-debugger
  • Loading branch information
fgsfdsfgs committed Oct 22, 2023
2 parents ac46cf8 + 0b9f99a commit 10baed1
Show file tree
Hide file tree
Showing 33 changed files with 608 additions and 196 deletions.
39 changes: 23 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,29 @@ You must already have a Perfect Dark ROM to run the game, as specified above.

## Controls

1964GEPD-style and Xbox-style bindings are partially implemented.

N64 pad buttons X and Y (or `X_BUTTON`, `Y_BUTTON` in the code) refer to the reserved buttons 0x40 and 0x80, which are also leveraged by 1964GEPD.

| Action | Keyboard and mouse | Xbox pad | N64 pad |
| - | - | - | - |
| Fire / Accept | LMB/Space | RT | Z Trigger |
| Aim mode | RMB/Z | LT | R Trigger |
| Use / Cancel | E | N/A | B |
| Use / Accept | N/A | A | A |
| Reload | R | X | X `(0x40)` |
| Previous weapon | Mousewheel forward | B | D-Left |
| Next weapon | Mousewheel back | Y | Y `(0x80)` |
| Radial menu | Q | LB | D-Down |
| Alt fire mode | F | RB | L Trigger |
| Quick-detonate | E + Q | A + B | A + D-Left |
1964GEPD-style and Xbox-style bindings are implemented.

N64 pad buttons X and Y (or `X_BUTTON`, `Y_BUTTON` in the code) refer to the reserved buttons `0x40` and `0x80`, which are also leveraged by 1964GEPD.

Support for one controller, two-stick configurations are enabled for 1.2.


| Action | Keyboard and mouse | Xbox pad | N64 pad |
| - | - | - | - |
| Fire / Accept | LMB/Space | RT | Z Trigger |
| Aim mode | RMB/Z | LT | R Trigger |
| Use / Cancel | E | N/A | B |
| Use / Accept | N/A | A | A |
| Crouch cycle | N/A | L3 | `0x80000000` (Extra) |
| Half-Crouch | Shift | N/A | `0x40000000` (Extra) |
| Full-Crouch | Control | N/A | `0x20000000` (Extra) |
| Reload | R | X | X `(0x40)` |
| Previous weapon | Mousewheel forward | B | D-Left |
| Next weapon | Mousewheel back | Y | Y `(0x80)` |
| Radial menu | Q | LB | D-Down |
| Alt fire mode | F | RB | L Trigger |
| Alt-fire oneshot | F + LMB or E + LMB | A + RT or RB + RT | A + Z, L + Z |
| Quick-detonate | E + Q | A + B | A + D-Left |

## Building

Expand Down
52 changes: 34 additions & 18 deletions include/PR/os_cont.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef struct {
}OSContStatus;

typedef struct {
u16 button;
u32 button;
s8 stick_x; /* -80 <= stick_x <= 80 */
s8 stick_y; /* -80 <= stick_y <= 80 */
u8 errnum;
Expand Down Expand Up @@ -122,23 +122,39 @@ typedef struct {
#define CONT_EEPROM_BUSY 0x80

/* Buttons */

#define CONT_A 0x8000
#define CONT_B 0x4000
#define CONT_G 0x2000
#define CONT_START 0x1000
#define CONT_UP 0x0800
#define CONT_DOWN 0x0400
#define CONT_LEFT 0x0200
#define CONT_RIGHT 0x0100
#define CONT_EXTRA1 0x0080
#define CONT_EXTRA0 0x0040
#define CONT_L 0x0020
#define CONT_R 0x0010
#define CONT_E 0x0008
#define CONT_D 0x0004
#define CONT_C 0x0002
#define CONT_F 0x0001
#define CONT_8000 0x80000000
#define CONT_4000 0x40000000
#define CONT_2000 0x20000000
#define CONT_1000 0x10000000
#define CONT_0800 0x08000000
#define CONT_0400 0x04000000
#define CONT_0200 0x02000000
#define CONT_0100 0x01000000
#define CONT_0080 0x00800000
#define CONT_0040 0x00400000
#define CONT_0020 0x00200000
#define CONT_0010 0x00100000
#define CONT_0008 0x00080000
#define CONT_0004 0x00040000
#define CONT_0002 0x00020000
#define CONT_0001 0x00010000

#define CONT_A 0x00008000
#define CONT_B 0x00004000
#define CONT_G 0x00002000
#define CONT_START 0x00001000
#define CONT_UP 0x00000800
#define CONT_DOWN 0x00000400
#define CONT_LEFT 0x00000200
#define CONT_RIGHT 0x00000100
#define CONT_EXTRA1 0x00000080
#define CONT_EXTRA0 0x00000040
#define CONT_L 0x00000020
#define CONT_R 0x00000010
#define CONT_E 0x00000008
#define CONT_D 0x00000004
#define CONT_C 0x00000002
#define CONT_F 0x00000001

/* Nintendo's official button names */

Expand Down
3 changes: 3 additions & 0 deletions port/fast3d/gfx_pc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2652,6 +2652,9 @@ static void gfx_run_dl(Gfx* cmd) {
// TODO: skyRender uses these to render some types of skies and skybox water
// by issuing low-level ucode commands G_TRI_FILL and G_TRI_SHADE_TXTR
break;
case G_RDPFLUSH_EXT:
gfx_flush();
break;
case G_RDPPIPESYNC:
case G_RDPFULLSYNC:
case G_RDPLOADSYNC:
Expand Down
22 changes: 19 additions & 3 deletions port/fast3d/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,30 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s

// ideally we need 3.0 compat
// if that doesn't work, try 3.2 core in case we're on mac, 2.1 compat as a last resort
static const u32 glver[][3] = {
static u32 glver[][3] = {
{ 0, 0, 0 }, // for command line override
{ 3, 0, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY },
{ 3, 2, SDL_GL_CONTEXT_PROFILE_CORE },
{ 2, 1, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY },
};

u32 glcore = false;
u32 verstart = 1;
const u32 verend = sizeof(glver) / sizeof(*glver);
const char *verstr = sysArgGetString("--gl-version");
if (verstr && *verstr) {
// user override
glver[0][2] = strstr(verstr, "core") ? SDL_GL_CONTEXT_PROFILE_CORE :
SDL_GL_CONTEXT_PROFILE_COMPATIBILITY;
sscanf(verstr, "%d.%d", &glver[0][0], &glver[0][1]);
if (glver[0][0] >= 1 && glver[0][0] <= 4 && glver[0][1] < 9) {
verstart = 0;
}
}

ctx = NULL;
u32 vmin = 0, vmaj = 0;
for (u32 i = 0; i < sizeof(glver) / sizeof(*glver); ++i) {
for (u32 i = verstart; i < verend; ++i) {
vmaj = glver[i][0];
vmin = glver[i][1];
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, vmaj);
Expand All @@ -112,14 +127,15 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s
if (!ctx) {
sysLogPrintf(LOG_WARNING, "GL: could not create GL%d.%d context: %s", vmaj, vmin, SDL_GetError());
} else {
glcore = (glver[i][2] == SDL_GL_CONTEXT_PROFILE_CORE);
break;
}
}

if (!ctx) {
sysFatalError("Could not create an OpenGL context of any supported version.\nSDL error: %s", SDL_GetError());
} else {
sysLogPrintf(LOG_NOTE, "GL: created GL%d.%d context", vmaj, vmin);
sysLogPrintf(LOG_NOTE, "GL: created GL%d.%d%s context", vmaj, vmin, glcore ? "core" : "");
}

SDL_GL_MakeCurrent(wnd, ctx);
Expand Down
16 changes: 14 additions & 2 deletions port/include/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define CONT_STICK_YNEG 0x40000
#define CONT_STICK_YPOS 0x80000

#define CONT_NUM_BUTTONS 16 // not including the stick axes
#define CONT_NUM_BUTTONS 32 // not including the stick axes

enum virtkey {
/* same order as SDL scancodes */
Expand Down Expand Up @@ -63,6 +63,18 @@ enum contkey {
CK_STICK_XPOS,
CK_STICK_YNEG,
CK_STICK_YPOS,
CK_0010,
CK_0020,
CK_0040,
CK_0080,
CK_0100,
CK_0200,
CK_0400,
CK_0800,
CK_1000,
CK_2000,
CK_4000,
CK_8000,
CK_TOTAL_COUNT
};

Expand All @@ -85,7 +97,7 @@ s32 inputControllerMask(void);
s32 inputKeyPressed(u32 vk);

// idx is controller index, contbtn is one of the CONT_ constants
s32 inputButtonPressed(s32 idx, u16 contbtn);
s32 inputButtonPressed(s32 idx, u32 contbtn);

// bind virtkey vk to n64 pad #idx's button/axis ck as represented by its contkey value
// if bind is -1, picks a bind slot automatically
Expand Down
3 changes: 2 additions & 1 deletion port/include/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ enum LogLevel {
LOG_ERROR,
};

void sysInit(s32 argc, const char **argv);
void sysInitArgs(s32 argc, const char **argv);
void sysInit(void);

s32 sysArgCheck(const char *arg);
const char *sysArgGetString(const char *arg);
Expand Down
2 changes: 1 addition & 1 deletion port/src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define CONFIG_MAX_STR 512
#define CONFIG_MAX_SECNAME 128
#define CONFIG_MAX_KEYNAME 256
#define CONFIG_MAX_SETTINGS 128
#define CONFIG_MAX_SETTINGS 256

struct configentry {
char key[CONFIG_MAX_KEYNAME + 1];
Expand Down
3 changes: 3 additions & 0 deletions port/src/crash.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ void crashInit(void)

void crashShutdown(void)
{
if (!g_CrashEnabled) {
return;
}
#ifdef PLATFORM_WIN32
if (prevExFilter) {
SetUnhandledExceptionFilter(prevExFilter);
Expand Down
53 changes: 38 additions & 15 deletions port/src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static f32 mouseSensY = 1.5f;

static f32 rumbleScale = 0.5f;

// NOTE: by default this gets inverted for 1.2
// NOTE: by default this gets inverted for 1.2: "right stick" here means left stick on your controller
static u32 axisMap[2][2] = {
{ SDL_CONTROLLER_AXIS_LEFTX, SDL_CONTROLLER_AXIS_LEFTY },
{ SDL_CONTROLLER_AXIS_RIGHTX, SDL_CONTROLLER_AXIS_RIGHTY },
Expand Down Expand Up @@ -78,6 +78,18 @@ static const char *ckNames[CK_TOTAL_COUNT] = {
"STICK_XPOS",
"STICK_YNEG",
"STICK_YPOS",
"CK_0010",
"CK_0020",
"CK_0040",
"CK_0080",
"CK_0100",
"CK_0200",
"CK_0400",
"CK_0800",
"CK_1000",
"CK_2000",
"CK_4000",
"CK_8000"
};

static const char *vkPunctNames[] = {
Expand Down Expand Up @@ -154,6 +166,8 @@ void inputSetDefaultKeyBinds(void)
{ CK_STICK_XPOS, SDL_SCANCODE_RIGHT, 0 },
{ CK_STICK_YNEG, SDL_SCANCODE_DOWN, 0 },
{ CK_STICK_YPOS, SDL_SCANCODE_UP, 0 },
{ CK_4000, SDL_SCANCODE_LSHIFT, 0 },
{ CK_2000, SDL_SCANCODE_LCTRL, 0 }
};

static const u32 joybinds[][2] = {
Expand All @@ -168,7 +182,7 @@ void inputSetDefaultKeyBinds(void)
{ CK_START, SDL_CONTROLLER_BUTTON_START },
{ CK_C_D, SDL_CONTROLLER_BUTTON_DPAD_DOWN },
{ CK_C_U, SDL_CONTROLLER_BUTTON_DPAD_UP },
{ CK_C_R, SDL_CONTROLLER_BUTTON_DPAD_RIGHT },
{ CK_8000, SDL_CONTROLLER_BUTTON_LEFTSTICK },
{ CK_C_L, SDL_CONTROLLER_BUTTON_DPAD_LEFT },
};

Expand Down Expand Up @@ -446,7 +460,7 @@ s32 inputInit(void)
stickSens[2] = configGetFloat("Input.RStickScaleX", 1.f);
stickSens[3] = configGetFloat("Input.RStickScaleY", 1.f);

stickCButtons = configGetInt("Input.StickCButtons", 1);
stickCButtons = configGetInt("Input.StickCButtons", 0);

if (configGetInt("Input.SwapSticks", 1)) {
// invert axis map
Expand Down Expand Up @@ -529,13 +543,6 @@ s32 inputReadController(s32 idx, OSContPad *npad)
rightX = inputAxisScale(rightX, deadzone[axisMap[1][0]], stickSens[axisMap[1][0]]);
rightY = inputAxisScale(rightY, deadzone[axisMap[1][1]], stickSens[axisMap[1][1]]);

if (stickCButtons) {
if (rightX < -0x4000) npad->button |= L_CBUTTONS;
if (rightX > +0x4000) npad->button |= R_CBUTTONS;
if (rightY < -0x4000) npad->button |= U_CBUTTONS;
if (rightY > +0x4000) npad->button |= D_CBUTTONS;
}

if (!npad->stick_x && leftX) {
npad->stick_x = leftX / 0x100;
}
Expand All @@ -545,9 +552,24 @@ s32 inputReadController(s32 idx, OSContPad *npad)
npad->stick_y = (stickY == 128) ? 127 : stickY;
}

stickY = -rightY / 0x100;
npad->rstick_y = (stickY == 128) ? 127 : stickY;
npad->rstick_x = rightX / 0x100;
if (stickCButtons) {
// rstick emulates C buttons
if (rightX < -0x4000) npad->button |= L_CBUTTONS;
if (rightX > +0x4000) npad->button |= R_CBUTTONS;
if (rightY < -0x4000) npad->button |= U_CBUTTONS;
if (rightY > +0x4000) npad->button |= D_CBUTTONS;
npad->rstick_x = 0;
npad->rstick_y = 0;
} else {
// rstick is an analog input
if (rightX) {
npad->rstick_x = rightX / 0x100;
}
s32 rStickY = -rightY / 0x100;
if (rStickY) {
npad->rstick_y = (rStickY == 128) ? 127 : rStickY;
}
}

return 0;
}
Expand Down Expand Up @@ -647,6 +669,7 @@ void inputKeyBind(s32 idx, u32 ck, s32 bind, u32 vk)
return;
}


if (bind < 0) {
for (s32 i = 0; i < MAX_BINDS; ++i) {
if (binds[idx][ck][i] == 0) {
Expand Down Expand Up @@ -686,7 +709,7 @@ s32 inputKeyPressed(u32 vk)
return 0;
}

static inline u16 inputContToContKey(const u16 cont)
static inline u32 inputContToContKey(const u32 cont)
{
if (cont == 0) {
return 0;
Expand All @@ -695,7 +718,7 @@ static inline u16 inputContToContKey(const u16 cont)
return 32 - __builtin_clz(cont - 1);
}

s32 inputButtonPressed(s32 idx, u16 contbtn)
s32 inputButtonPressed(s32 idx, u32 contbtn)
{
if (idx < 0 || idx >= INPUT_MAX_CONTROLLERS) {
return 0;
Expand Down
2 changes: 2 additions & 0 deletions port/src/libultra.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ void osContGetReadData(OSContPad *pad)
pad->button = 0;
pad->stick_x = 0;
pad->stick_y = 0;
pad->rstick_x = 0;
pad->rstick_y = 0;
if (inputReadController(i, pad) < 0) {
pad->errnum = CONT_NO_RESPONSE_ERROR;
} else {
Expand Down
10 changes: 8 additions & 2 deletions port/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static void gameLoadConfig(void)
g_PlayerMouseAimSpeedY = configGetFloatClamped("Game.MouseAimSpeedY", g_PlayerMouseAimSpeedY, 0.f, 10.f);
g_PlayerFovAffectsZoom = configGetIntClamped("Game.FovAffectsZoom", g_PlayerFovAffectsZoom, 0, 1);
g_PlayerFovZoomMultiplier = g_PlayerFovAffectsZoom ? g_PlayerDefaultFovY / 60.0f : 1.0f;
g_PlayerClassicCrouch = configGetIntClamped("Game.ClassicCrouch", 0, 0, 1);
g_ViShakeIntensityMult = configGetFloatClamped("Game.ScreenShakeIntensity", 1.f, 0.f, 100.f);
const s32 center = configGetIntClamped("Game.CenterHUD", 0, 0, 1);
if (center) {
Expand All @@ -85,8 +86,13 @@ static void gameLoadConfig(void)

int main(int argc, const char **argv)
{
crashInit();
sysInit(argc, argv);
sysInitArgs(argc, argv);

if (!sysArgCheck("--no-crash-handler")) {
crashInit();
}

sysInit();
fsInit();
configInit();
videoInit();
Expand Down
Loading

0 comments on commit 10baed1

Please sign in to comment.