Skip to content

Commit

Permalink
Merge branch 'gamepad_support' into vanilla
Browse files Browse the repository at this point in the history
  • Loading branch information
Northfear committed Jun 26, 2021
2 parents 688832c + f4396d6 commit b67e055
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ https://github.com/Cpasjuste/psp2shell

Use R1 + D-Pad to create teams (1-4) and D-Pad to select them (same as Ctrl + 1-4 on keyboard). You can use DPad numbers while entering savegame names.

Left analog stick can be used for both cursor movement (good for building or precise movement) or map scrolling (useful during combat when you're giving command with a touchpad). Cursor movement mode is default. To switch between both modes use 3-finger touch on the rear touchpad.
Left analog stick can be used for both cursor movement (good for building or precise movement) or map scrolling (useful during combat when you're giving command with a touchpad). Cursor movement mode is default. To switch between both modes use R1 + L1 + START key combination.

You can change cursor movement speed by editing ```ControllerPointerSpeed``` option under ```[Vita]``` section inside ```conquer.ini```/```redalert.ini``` files.

Expand Down
3 changes: 3 additions & 0 deletions common/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SettingsClass::SettingsClass()
*/
Mouse.RawInput = true;
Mouse.Sensitivity = 100;
Mouse.ControllerPointerSpeed = 10;

/*
** Video settings
Expand Down Expand Up @@ -41,6 +42,7 @@ void SettingsClass::Load(INIClass& ini)
*/
Mouse.RawInput = ini.Get_Bool("Mouse", "RawInput", Mouse.RawInput);
Mouse.Sensitivity = ini.Get_Int("Mouse", "Sensitivity", Mouse.Sensitivity);
Mouse.ControllerPointerSpeed = ini.Get_Int("Mouse", "ControllerPointerSpeed", Mouse.ControllerPointerSpeed);

/*
** Video settings
Expand Down Expand Up @@ -82,6 +84,7 @@ void SettingsClass::Save(INIClass& ini)
*/
ini.Put_Bool("Mouse", "RawInput", Mouse.RawInput);
ini.Put_Int("Mouse", "Sensitivity", Mouse.Sensitivity);
ini.Put_Int("Mouse", "ControllerPointerSpeed", Mouse.ControllerPointerSpeed);

/*
** Video settings
Expand Down
1 change: 1 addition & 0 deletions common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SettingsClass
{
bool RawInput;
int Sensitivity;
int ControllerPointerSpeed;
} Mouse;

struct
Expand Down
2 changes: 2 additions & 0 deletions common/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ void Toggle_Video_Fullscreen();
void Reset_Video_Mode();
unsigned Get_Free_Video_Memory();
void Wait_Blit();
void Get_Game_Resolution(int& w, int& h);
void Set_Video_Mouse(int x, int y);

#ifdef VITA
#include <SDL.h>
Expand Down
19 changes: 6 additions & 13 deletions common/video_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

#include <SDL.h>

extern WWKeyboardClass* Keyboard;
static SDL_Window* window;
static SDL_Renderer* renderer;
static SDL_Palette* palette;
Expand Down Expand Up @@ -370,11 +371,11 @@ bool Set_Video_Mode(int w, int h, int bits_per_pixel)
hwcursor.Y = h / 2;
Update_HWCursor_Settings();

#ifdef VITA
/*
** Init gamepad.
*/
SDL_Init(SDL_INIT_GAMECONTROLLER);
extern WWKeyboardClass* Keyboard;
Keyboard->Open_Controller();
#endif

return true;
}
Expand Down Expand Up @@ -462,11 +463,7 @@ void Move_Video_Mouse(int xrel, int yrel)

void Get_Video_Mouse(int& x, int& y)
{
#ifdef VITA
x = hwcursor.X;
y = hwcursor.Y;
#else
if (Settings.Mouse.RawInput && (hwcursor.Clip || !Settings.Video.Windowed)) {
if (Keyboard->Is_Gamepad_Active() || (Settings.Mouse.RawInput && (hwcursor.Clip || !Settings.Video.Windowed))) {
x = hwcursor.X;
y = hwcursor.Y;
} else {
Expand All @@ -476,14 +473,14 @@ void Get_Video_Mouse(int& x, int& y)
x /= scale_x;
y /= scale_y;
}
#endif
}

#ifdef VITA
SDL_Rect Get_Render_Rect()
{
return render_dst;
}
#endif

void Get_Game_Resolution(int& w, int& h)
{
Expand All @@ -496,7 +493,6 @@ void Set_Video_Mouse(int x, int y)
hwcursor.X = x;
hwcursor.Y = y;
}
#endif

/***********************************************************************************************
* Reset_Video_Mode -- Resets video mode and deletes Direct Draw Object *
Expand Down Expand Up @@ -536,10 +532,7 @@ void Reset_Video_Mode(void)
SDL_DestroyWindow(window);
window = nullptr;

#ifdef VITA
extern WWKeyboardClass* Keyboard;
Keyboard->Close_Controller();
#endif
}

static void Update_HWCursor()
Expand Down
67 changes: 41 additions & 26 deletions common/wwkeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@ void WWKeyboardClass::Fill_Buffer_From_System(void)
break;
case SDL_MOUSEMOTION:
Move_Video_Mouse(event.motion.xrel, event.motion.yrel);
if (Is_Gamepad_Active()) {
int mousePosX;
int mousePosY;
Get_Video_Mouse(mousePosX, mousePosY);
EmulatedPointerPosX = mousePosX;
EmulatedPointerPosY = mousePosY;
}
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: {
Expand Down Expand Up @@ -595,7 +602,6 @@ void WWKeyboardClass::Fill_Buffer_From_System(void)
break;
}
break;
#ifdef VITA
case SDL_CONTROLLERDEVICEREMOVED:
if (GameController != nullptr) {
const SDL_GameController* removedController = SDL_GameControllerFromInstanceID(event.jdevice.which);
Expand All @@ -622,12 +628,11 @@ void WWKeyboardClass::Fill_Buffer_From_System(void)
case SDL_FINGERMOTION:
Handle_Touch_Event(event.tfinger);
break;
#endif
}
}
#ifdef VITA
Process_Controller_Axis_Motion();
#endif
if (Is_Gamepad_Active()) {
Process_Controller_Axis_Motion();
}
#elif defined(_WIN32)
if (!Is_Buffer_Full()) {
MSG msg;
Expand All @@ -642,7 +647,12 @@ void WWKeyboardClass::Fill_Buffer_From_System(void)
#endif
}

#ifdef VITA
#ifdef SDL2_BUILD
bool WWKeyboardClass::Is_Gamepad_Active()
{
return GameController != nullptr;
}

void WWKeyboardClass::Open_Controller()
{
for (int i = 0; i < SDL_NumJoysticks(); ++i) {
Expand All @@ -651,12 +661,11 @@ void WWKeyboardClass::Open_Controller()
}
}

int gameWidth;
int gameHeight;
Get_Game_Resolution(gameWidth, gameHeight);
EmulatedPointerPosX = gameWidth / 2;
EmulatedPointerPosY = gameHeight / 2;

int mousePosX;
int mousePosY;
Get_Video_Mouse(mousePosX, mousePosY);
EmulatedPointerPosX = mousePosX;
EmulatedPointerPosY = mousePosY;
#if SDL_VERSION_ATLEAST(2, 0, 10)
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
#endif
Expand All @@ -683,10 +692,10 @@ void WWKeyboardClass::Process_Controller_Axis_Motion()
const int16_t xSign = (ControllerLeftXAxis > 0) - (ControllerLeftXAxis < 0);
const int16_t ySign = (ControllerLeftYAxis > 0) - (ControllerLeftYAxis < 0);

EmulatedPointerPosX += pow(std::abs(ControllerLeftXAxis), CONTROLLER_AXIS_SPEEDUP) * xSign * deltaTime
* Settings.Vita.ControllerPointerSpeed / CONTROLLER_SPEED_MOD;
EmulatedPointerPosY += pow(std::abs(ControllerLeftYAxis), CONTROLLER_AXIS_SPEEDUP) * ySign * deltaTime
* Settings.Vita.ControllerPointerSpeed / CONTROLLER_SPEED_MOD;
EmulatedPointerPosX += std::pow(std::abs(ControllerLeftXAxis), CONTROLLER_AXIS_SPEEDUP) * xSign * deltaTime
* Settings.Mouse.ControllerPointerSpeed / CONTROLLER_SPEED_MOD * ControllerSpeedBoost;
EmulatedPointerPosY += std::pow(std::abs(ControllerLeftYAxis), CONTROLLER_AXIS_SPEEDUP) * ySign * deltaTime
* Settings.Mouse.ControllerPointerSpeed / CONTROLLER_SPEED_MOD * ControllerSpeedBoost;

int width;
int height;
Expand Down Expand Up @@ -738,6 +747,11 @@ void WWKeyboardClass::Handle_Controller_Axis_Event(const SDL_ControllerAxisEvent
ControllerRightYAxis = motion.value;
else
ControllerRightYAxis = 0;
} else if (motion.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) {
if (std::abs(motion.value) > CONTROLLER_TRIGGER_R_DEADZONE)
ControllerSpeedBoost = 1 + (static_cast<float>(motion.value) / 32767) * CONTROLLER_TRIGGER_SPEEDUP;
else
ControllerSpeedBoost = 1;
}

if (!AnalogStickMouse) {
Expand Down Expand Up @@ -780,17 +794,9 @@ void WWKeyboardClass::Handle_Controller_Axis_Event(const SDL_ControllerAxisEvent

void WWKeyboardClass::Handle_Touch_Event(const SDL_TouchFingerEvent& event)
{
// back touchpad
if (event.touchId == 1) {
if (event.type == SDL_FINGERDOWN) {
int backTouchNum = SDL_GetNumTouchFingers(event.touchId);
// 3 touches on back touchpad switch between left analog scroll and mouse
if (backTouchNum == 3) {
AnalogStickMouse = !AnalogStickMouse;
}
}
// ignore back touchpad
if (event.touchId != 0)
return;
}

if (event.type == SDL_FINGERDOWN) {
++NumTouches;
Expand Down Expand Up @@ -901,6 +907,15 @@ void WWKeyboardClass::Handle_Controller_Button_Event(const SDL_ControllerButtonE
Get_Video_Mouse(x, y);
Put_Mouse_Message(key, x, y, button.state == SDL_RELEASED);
}

if (button.state == SDL_PRESSED && (button.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER || button.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ||
button.button == SDL_CONTROLLER_BUTTON_START))
{
if (SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_LEFTSHOULDER) &&
SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) &&
SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_START))
AnalogStickMouse = !AnalogStickMouse;
}
}

bool WWKeyboardClass::Is_Analog_Scroll_Active()
Expand Down
17 changes: 9 additions & 8 deletions common/wwkeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#endif
#include <stdint.h>

#ifdef VITA
#ifdef SDL2_BUILD
#include <SDL.h>
#endif

Expand Down Expand Up @@ -713,7 +713,6 @@ typedef enum KeyNumType : unsigned short
KN_BUTTON = WWKEY_BTN_BIT,
} KeyNumType;

#ifdef VITA
typedef enum ScrollDirType : unsigned char
{
SDIR_N = 0,
Expand All @@ -726,7 +725,6 @@ typedef enum ScrollDirType : unsigned char
SDIR_NW = 7 << 5,
SDIR_NONE = 100
} ScrollDirType;
#endif

class WWKeyboardClass
{
Expand All @@ -742,7 +740,8 @@ class WWKeyboardClass
KeyASCIIType To_ASCII(unsigned short num);
bool Down(unsigned short key);

#ifdef VITA
#ifdef SDL2_BUILD
bool Is_Gamepad_Active();
void Open_Controller();
void Close_Controller();
bool Is_Analog_Scroll_Active();
Expand Down Expand Up @@ -798,23 +797,25 @@ class WWKeyboardClass
uint8_t DownState[0x2000]; // (UINT16_MAX / 8) + 1
int DownSkip;

#ifdef VITA
#ifdef SDL2_BUILD
void Handle_Controller_Axis_Event(const SDL_ControllerAxisEvent& motion);
void Handle_Controller_Button_Event(const SDL_ControllerButtonEvent& button);
void Handle_Touch_Event(const SDL_TouchFingerEvent& event);
void Process_Controller_Axis_Motion();

// used to convert user-friendly pointer speed values into more useable ones
const double CONTROLLER_SPEED_MOD = 2000000.0;

// bigger value correndsponds to faster pointer movement speed with bigger stick axis values
const double CONTROLLER_AXIS_SPEEDUP = 1.03;
// speedup value while the trigger is pressed
const int CONTROLLER_TRIGGER_SPEEDUP = 2;

enum
{
CONTROLLER_L_DEADZONE_SCROLL = 6000,
CONTROLLER_L_DEADZONE_MOUSE = 3000,
CONTROLLER_R_DEADZONE = 6000
CONTROLLER_R_DEADZONE = 6000,
CONTROLLER_TRIGGER_R_DEADZONE = 3000
};

SDL_GameController* GameController = nullptr;
Expand All @@ -825,7 +826,7 @@ class WWKeyboardClass
uint32_t LastControllerTime = 0;
float EmulatedPointerPosX = 0;
float EmulatedPointerPosY = 0;

float ControllerSpeedBoost = 1;
bool AnalogScrollActive = false;
ScrollDirType ScrollDirection = SDIR_NONE;

Expand Down
2 changes: 1 addition & 1 deletion redalert/scroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void ScrollClass::AI(KeyNumType& input, int x, int y)
*/
bool noscroll = false;

#ifdef VITA
#ifdef SDL2_BUILD
if (Keyboard->Is_Analog_Scroll_Active()) {
unsigned char scrollDirection = Keyboard->Get_Scroll_Direction();
int scrollDistance = (7 - Options.ScrollRate) * 20;
Expand Down
2 changes: 1 addition & 1 deletion tiberiandawn/scroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void ScrollClass::AI(KeyNumType& input, int x, int y)
noscroll = true;
}

#ifdef VITA
#ifdef SDL2_BUILD
if (Keyboard->Is_Analog_Scroll_Active()) {
unsigned char scrollDirection = Keyboard->Get_Scroll_Direction();
int scrollDistance = (7 - Options.ScrollRate) * 20;
Expand Down

0 comments on commit b67e055

Please sign in to comment.