Skip to content

Commit

Permalink
input: merge tr1 and tr2 inputs code
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Oct 31, 2024
1 parent d22d495 commit 698915a
Show file tree
Hide file tree
Showing 61 changed files with 1,402 additions and 1,255 deletions.
4 changes: 4 additions & 0 deletions data/tr2/ship/cfg/TR2X_gameflow.json5
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@
},

"game_strings": {
"CONTROL_CUSTOM_1": "Custom Scheme 1",
"CONTROL_CUSTOM_2": "Custom Scheme 2",
"CONTROL_CUSTOM_3": "Custom Scheme 3",
"CONTROL_DEFAULT_KEYS": "Default Keys",
"MISC_OFF": "Off",
"MISC_ON": "On",
"OSD_AMBIGUOUS_INPUT_2": "Ambiguous input: %s and %s",
Expand Down
23 changes: 23 additions & 0 deletions docs/tr2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
## [Unreleased](https://github.com/LostArtefacts/TRX/compare/tr2-0.5...develop) - ××××-××-××
- changed the inputs backend from DirectX to SDL (#1695)
- improved controller support to match TR1X
- changed the number of custom layouts to 3
- changed default key bindings according to the following table:
| Key | Old binding | New binding | Reason
| ----------------------------- | ----------- | ------------ | -----
| Flare | Comma (,) | Period (.) | To maintain forward compatibility with TR3
| Screenshot | S | Print Screen | To maintain compatibility with TR1X
| Toggle bilinear filter | F8 | F3 | To maintain compatibility with TR1X
| Toggle perspective filter | Shift+F8 | F4 | To maintain compatibility with TR1X
| Toggle z-buffer | F7 | F7 | Likely to be permanently enabled in the future
| Toggle triple buffering | Shift+F7 | **Removed** | Obscure setting, will be either removed or available via the ingame UI at some point
| Toggle dither | F11 | **Removed** | Obscure setting, will be either removed or available via the ingame UI at some point
| Toggle fullscreen | F12 | Alt-Enter | To maintain compatibility with TR1X
| Toggle rendering mode | Shift+F12 | F12 | No more conflict to require Shift
| Decrease resolution | F1 | Shift+F1 | F3 and F4 are already taken
| Increase resolution | F2 | F1 | F3 and F4 are already taken
| Decrease internal screen size | F3 | Shift+F2 | F3 and F4 are already taken
| Increase internal screen size | F4 | F2 | F3 and F4 are already taken
- removed "falling through" to the default layout, with the exception of keyboard arrows (matching TR1X behavior)
- removed hardcoded Shift+F7 key binding for toggling triple buffering
- removed hardcoded `0` key binding for flares
- removed hardcoded cooldown of 15 frames for medipacks
- improved FMV mode appearance - removed black scanlines (#1729)
- improved FMV mode behavior - stopped switching screen resolutions (#1729)
- improved screenshots: now saved in the screenshots/ directory with level titles and timestamps as JPG or PNG, similar to TR1X (#1773)
Expand Down
56 changes: 28 additions & 28 deletions docs/tr2/progress.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 27 additions & 27 deletions docs/tr2/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -379,20 +379,20 @@ typedef struct __unaligned {
} VERTEX_INFO;

typedef enum { // decompiled
INPUT_ROLE_FORWARD = 0,
INPUT_ROLE_BACK = 1,
INPUT_ROLE_LEFT = 2,
INPUT_ROLE_RIGHT = 3,
INPUT_ROLE_STEP_LEFT = 4,
INPUT_ROLE_STEP_RIGHT = 5,
INPUT_ROLE_SLOW = 6,
INPUT_ROLE_JUMP = 7,
INPUT_ROLE_ACTION = 8,
INPUT_ROLE_DRAW_WEAPON = 9,
INPUT_ROLE_FLARE = 10,
INPUT_ROLE_LOOK = 11,
INPUT_ROLE_ROLL = 12,
INPUT_ROLE_OPTION = 13,
INPUT_ROLE_FORWARD = 0,
INPUT_ROLE_BACK = 1,
INPUT_ROLE_LEFT = 2,
INPUT_ROLE_RIGHT = 3,
INPUT_ROLE_STEP_L = 4,
INPUT_ROLE_STEP_R = 5,
INPUT_ROLE_SLOW = 6,
INPUT_ROLE_JUMP = 7,
INPUT_ROLE_ACTION = 8,
INPUT_ROLE_DRAW = 9,
INPUT_ROLE_USE_FLARE = 10,
INPUT_ROLE_LOOK = 11,
INPUT_ROLE_ROLL = 12,
INPUT_ROLE_OPTION = 13,
} INPUT_ROLE;

typedef struct __unaligned {
Expand Down Expand Up @@ -3865,19 +3865,19 @@ typedef enum {
0x004471F0 0x0022 +R bool __cdecl DInputCreate(void);
0x00447220 0x001A +R void __cdecl DInputRelease(void);
0x00447240 0x005A +R void __cdecl WinInReadKeyboard(LPVOID lpInputData);
0x004472A0 0x00F3 -R DWORD __cdecl WinInReadJoystick(int32_t *x, int32_t *y);
0x004472A0 0x00F3 +R DWORD __cdecl WinInReadJoystick(int32_t *x, int32_t *y);
0x004473A0 0x0005 x sub_4473A0
0x004473B0 0x007F -R bool __cdecl WinInputInit(void);
0x00447430 0x0024 -R bool __cdecl DInputEnumDevices(JOYSTICK_LIST *joystickList);
0x00447460 0x00E8 -R BOOL __stdcall DInputEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);
0x004473B0 0x007F +R bool __cdecl WinInputInit(void);
0x00447430 0x0024 +R bool __cdecl DInputEnumDevices(JOYSTICK_LIST *joystickList);
0x00447460 0x00E8 +R BOOL __stdcall DInputEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);
0x00447550 0x001F + void __thiscall S_FlaggedString_Create(STRING_FLAGGED *string, int32_t size);
0x00447570 0x004E -R JOYSTICK_NODE *__cdecl GetJoystick(GUID *lpGuid);
0x004475C0 0x00C9 -R void __cdecl DInputKeyboardCreate(void);
0x00447690 0x0029 -R void __cdecl DInputKeyboardRelease(void);
0x004476C0 0x00E4 -R bool __cdecl DInputJoystickCreate(void);
0x004477B0 0x002D -R void __cdecl WinInStart(void);
0x004477E0 0x000F -R void __cdecl WinInFinish(void);
0x004477F0 0x0017 -R void __cdecl WinInRunControlPanel(HWND hWnd);
0x00447570 0x004E +R JOYSTICK_NODE *__cdecl GetJoystick(GUID *lpGuid);
0x004475C0 0x00C9 +R void __cdecl DInputKeyboardCreate(void);
0x00447690 0x0029 +R void __cdecl DInputKeyboardRelease(void);
0x004476C0 0x00E4 +R bool __cdecl DInputJoystickCreate(void);
0x004477B0 0x002D +R void __cdecl WinInStart(void);
0x004477E0 0x000F +R void __cdecl WinInFinish(void);
0x004477F0 0x0017 +R void __cdecl WinInRunControlPanel(HWND hWnd);
0x00447810 0x0062 *R void __cdecl IncreaseScreenSize(void);
0x00447880 0x0062 *R void __cdecl DecreaseScreenSize(void);
0x004478F0 0x009F *R void __cdecl setup_screen_size(void);
Expand Down Expand Up @@ -4455,7 +4455,7 @@ typedef enum {
0x0051A108 - uint8_t g_DIKeys[256];
0x0051A208 + int32_t g_Input;
0x0051A20C - int8_t g_IsVidModeLock;
0x0051A210 - int32_t g_JoyKeys;
0x0051A210 + int32_t g_JoyKeys;
0x0051A214 + int32_t g_JoyXPos;
0x0051A218 + int32_t g_JoyYPos;
0x0051A220 + int32_t g_MediPackCooldown;
Expand Down Expand Up @@ -4511,7 +4511,7 @@ typedef enum {
0x00526314 - int16_t g_CineFrameIdx;
0x00526320 - CAMERA_INFO g_Camera;
0x005263CC - BOX_INFO *g_Boxes;
0x004D855C - LPDIRECTINPUT g_DInput;
0x004D855C + LPDIRECTINPUT g_DInput;
0x004D8560 - LPDIRECTINPUTDEVICE IDID_SysKeyboard;
0x0051BDA0 - BOOL g_IsTitleLoaded;
0x004D7980 - int32_t g_Inv_ExtraData[8];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "game/input/backends/controller.h"

#include "game/input/backends/internal.h"

#include <libtrx/log.h>
#include "log.h"

#include <SDL2/SDL.h>
#include <SDL2/SDL_gamecontroller.h>
Expand Down Expand Up @@ -31,6 +30,8 @@ static BUILTIN_CONTROLLER_LAYOUT m_BuiltinLayout[] = {
{ role, { button_type, { bind }, axis_dir } },
#if TR_VERSION == 1
#include "game/input/backends/controller_tr1.def"
#elif TR_VERSION == 2
#include "game/input/backends/controller_tr2.def"
#endif
// guard
{ -1, { 0, { 0 }, 0 } },
Expand Down Expand Up @@ -383,7 +384,7 @@ static bool M_CustomUpdate(INPUT_STATE *const result, const INPUT_LAYOUT layout)
if (m_Controller == NULL) {
return false;
}
#if TR_VERSION == 1
#if TR_VERSION == 1 || TR_VERSION == 2
result->menu_back |= M_JoyBtn(SDL_CONTROLLER_BUTTON_Y);
#endif
return true;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ typedef struct {
SDL_Scancode scancode;
} BUILTIN_KEYBOARD_LAYOUT;

const Uint8 *m_KeyboardState;
const Uint8 *m_KeyboardState = NULL;
static bool m_Conflicts[INPUT_LAYOUT_NUMBER_OF][INPUT_ROLE_NUMBER_OF] = { 0 };

static BUILTIN_KEYBOARD_LAYOUT m_BuiltinLayout[] = {
// clang-format off
#define INPUT_KEYBOARD_ASSIGN(role, key) { role, key },
#if TR_VERSION == 1
#include "keyboard_tr1.def"
#include "game/input/backends/keyboard_tr1.def"
#elif TR_VERSION == 2
#include "game/input/backends/keyboard_tr2.def"
#endif
{ -1, SDL_SCANCODE_UNKNOWN },
// clang-format on
Expand Down Expand Up @@ -381,6 +383,10 @@ static bool M_CustomUpdate(INPUT_STATE *const result, const INPUT_LAYOUT layout)
// we only do this for keyboard input
#if TR_VERSION == 1
result->menu_confirm |= result->action;
#elif TR_VERSION == 2
result->menu_confirm |= result->action;
result->toggle_fullscreen =
KEY_DOWN(SDL_SCANCODE_RETURN) && KEY_DOWN(SDL_SCANCODE_LALT);
#endif
return true;
}
Expand Down
165 changes: 165 additions & 0 deletions src/libtrx/game/input/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include "game/input/common.h"

#include "game/clock.h"
#include "game/game_string.h"
#include "game/input/backends/controller.h"
#include "game/input/backends/keyboard.h"

#include <stdint.h>

INPUT_STATE g_Input = { 0 };
INPUT_STATE g_InputDB = { 0 };
INPUT_STATE g_OldInputDB = { 0 };

static bool m_ListenMode = false;

static bool m_IsRoleHardcoded[INPUT_ROLE_NUMBER_OF] = {
0,
#if TR_VERSION == 1
[INPUT_ROLE_UNBIND_KEY] = 1,
[INPUT_ROLE_RESET_BINDINGS] = 1,
[INPUT_ROLE_PERSPECTIVE] = 1,
#endif
[INPUT_ROLE_MENU_CONFIRM] = 1,
[INPUT_ROLE_MENU_BACK] = 1,
[INPUT_ROLE_MENU_LEFT] = 1,
[INPUT_ROLE_MENU_RIGHT] = 1,
[INPUT_ROLE_MENU_UP] = 1,
[INPUT_ROLE_MENU_DOWN] = 1,
};

static const GAME_STRING_ID m_LayoutMap[INPUT_LAYOUT_NUMBER_OF] = {
[INPUT_LAYOUT_DEFAULT] = GS_ID(CONTROL_DEFAULT_KEYS),
[INPUT_LAYOUT_CUSTOM_1] = GS_ID(CONTROL_CUSTOM_1),
[INPUT_LAYOUT_CUSTOM_2] = GS_ID(CONTROL_CUSTOM_2),
[INPUT_LAYOUT_CUSTOM_3] = GS_ID(CONTROL_CUSTOM_3),
};

static INPUT_BACKEND_IMPL *M_GetBackend(INPUT_BACKEND backend);

static INPUT_BACKEND_IMPL *M_GetBackend(const INPUT_BACKEND backend)
{
switch (backend) {
case INPUT_BACKEND_KEYBOARD:
return &g_Input_Keyboard;
case INPUT_BACKEND_CONTROLLER:
return &g_Input_Controller;
}
return NULL;
}

void Input_Init(void)
{
if (g_Input_Keyboard.init != NULL) {
g_Input_Keyboard.init();
}
if (g_Input_Controller.init != NULL) {
g_Input_Controller.init();
}
}

void Input_Shutdown(void)
{
if (g_Input_Keyboard.shutdown != NULL) {
g_Input_Keyboard.shutdown();
}
if (g_Input_Controller.shutdown != NULL) {
g_Input_Controller.shutdown();
}
}

void Input_InitController(void)
{
if (g_Input_Controller.init != NULL) {
g_Input_Controller.init();
}
}

void Input_ShutdownController(void)
{
if (g_Input_Controller.shutdown != NULL) {
g_Input_Controller.shutdown();
}
}

bool Input_IsRoleRebindable(const INPUT_ROLE role)
{
return !m_IsRoleHardcoded[role];
}

bool Input_IsPressed(
const INPUT_BACKEND backend, const INPUT_LAYOUT layout,
const INPUT_ROLE role)
{
return M_GetBackend(backend)->is_pressed(layout, role);
}

bool Input_IsKeyConflicted(
const INPUT_BACKEND backend, const INPUT_LAYOUT layout,
const INPUT_ROLE role)
{
return M_GetBackend(backend)->is_role_conflicted(layout, role);
}

bool Input_ReadAndAssignRole(
const INPUT_BACKEND backend, const INPUT_LAYOUT layout,
const INPUT_ROLE role)
{
return M_GetBackend(backend)->read_and_assign(layout, role);
}

void Input_UnassignRole(
const INPUT_BACKEND backend, const INPUT_LAYOUT layout,
const INPUT_ROLE role)
{
M_GetBackend(backend)->unassign_role(layout, role);
}

const char *Input_GetKeyName(
const INPUT_BACKEND backend, const INPUT_LAYOUT layout,
const INPUT_ROLE role)
{
return M_GetBackend(backend)->get_name(layout, role);
}

void Input_ResetLayout(const INPUT_BACKEND backend, const INPUT_LAYOUT layout)
{
return M_GetBackend(backend)->reset_layout(layout);
}

void Input_EnterListenMode(void)
{
m_ListenMode = true;
}

void Input_ExitListenMode(void)
{
m_ListenMode = false;
Input_Update();
g_OldInputDB.any = g_Input.any;
g_InputDB.any = g_Input.any;
}

bool Input_IsInListenMode(void)
{
return m_ListenMode;
}

bool Input_AssignFromJSONObject(
const INPUT_BACKEND backend, const INPUT_LAYOUT layout,
JSON_OBJECT *const bind_obj)
{
return M_GetBackend(backend)->assign_from_json_object(layout, bind_obj);
}

bool Input_AssignToJSONObject(
const INPUT_BACKEND backend, const INPUT_LAYOUT layout,
JSON_OBJECT *const bind_obj, const INPUT_ROLE role)
{
return M_GetBackend(backend)->assign_to_json_object(layout, bind_obj, role);
}

const char *Input_GetLayoutName(const INPUT_LAYOUT layout)
{
return GameString_Get(m_LayoutMap[layout]);
}
4 changes: 4 additions & 0 deletions src/libtrx/include/libtrx/game/game_string.def
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ GS_DEFINE(OSD_AMBIGUOUS_INPUT_2, "Ambiguous input: %s and %s")
GS_DEFINE(OSD_AMBIGUOUS_INPUT_3, "Ambiguous input: %s, %s, ...")
GS_DEFINE(OSD_UI_ON, "UI enabled")
GS_DEFINE(OSD_UI_OFF, "UI disabled")
GS_DEFINE(CONTROL_DEFAULT_KEYS, "Default Keys")
GS_DEFINE(CONTROL_CUSTOM_1, "Custom Scheme 1")
GS_DEFINE(CONTROL_CUSTOM_2, "Custom Scheme 2")
GS_DEFINE(CONTROL_CUSTOM_3, "Custom Scheme 3")
3 changes: 1 addition & 2 deletions src/libtrx/include/libtrx/game/input.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once

extern void Input_EnterListenMode(void);
extern void Input_ExitListenMode(void);
#include "input/common.h"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "game/input/common.h"
#include "../common.h"

#include <stdbool.h>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "game/input/backends/base.h"
#include "base.h"

extern INPUT_BACKEND_IMPL g_Input_Controller;
15 changes: 15 additions & 0 deletions src/libtrx/include/libtrx/game/input/backends/controller_tr2.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_UP, BT_BUTTON, SDL_CONTROLLER_BUTTON_DPAD_UP, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_DOWN, BT_BUTTON, SDL_CONTROLLER_BUTTON_DPAD_DOWN, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_LEFT, BT_BUTTON, SDL_CONTROLLER_BUTTON_DPAD_LEFT, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_RIGHT, BT_BUTTON, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_STEP_L, BT_AXIS, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 1)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_STEP_R, BT_AXIS, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 1)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_SLOW, BT_BUTTON, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_JUMP, BT_BUTTON, SDL_CONTROLLER_BUTTON_X, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_ACTION, BT_BUTTON, SDL_CONTROLLER_BUTTON_A, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_DRAW, BT_BUTTON, SDL_CONTROLLER_BUTTON_Y, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_LOOK, BT_BUTTON, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_ROLL, BT_BUTTON, SDL_CONTROLLER_BUTTON_B, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_OPTION, BT_BUTTON, SDL_CONTROLLER_BUTTON_BACK, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_MENU_CONFIRM, BT_BUTTON, SDL_CONTROLLER_BUTTON_A, 0)
INPUT_CONTROLLER_ASSIGN(INPUT_ROLE_MENU_BACK, BT_BUTTON, SDL_CONTROLLER_BUTTON_B, 0)
Loading

0 comments on commit 698915a

Please sign in to comment.