Код:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Fight.h"
#include "SDL/SDL.h"
SDL_GameController *ctrl[8];
SDL_Joystick *joy[8];
SDL_Joystick *cjoy[8];
SDL_Haptic *haptic[8];
SDL_Haptic *Directhaptic[8];
int i;
int n;
FString output;
int Rumble[8];
SDL_HapticEffect effect;
int effect_id[8];
void UFight::InitDevice()
{
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC);
// Open the device
for (i = 0; i < SDL_NumJoysticks(); ++i) {
joy[i] = SDL_JoystickOpen(i);
if (joy[i]) {
Directhaptic[i] = SDL_HapticOpenFromJoystick(joy[i]);
}
ctrl[i] = SDL_GameControllerOpen(i);
Rumble[i] = -1;
if (ctrl[i]) {
cjoy[i] = SDL_GameControllerGetJoystick(ctrl[i]);
haptic[i] = SDL_HapticOpenFromJoystick(cjoy[i]);
if (haptic[i] != NULL) {
if (SDL_HapticRumbleSupported(haptic[i])) {
Rumble[i] = SDL_HapticRumbleInit(haptic[i]);
}
}
}
}
}
int UFight::CountDevice()
{
return(SDL_NumJoysticks());
}
FString UFight::NameDevice(int DeviceIndex, bool& Connect)
{
output = NULL;
i = SDL_NumJoysticks();
if (i > DeviceIndex) {
if (SDL_IsGameController(DeviceIndex)) {
output = ((FString)SDL_GameControllerNameForIndex(DeviceIndex));
Connect = false;
if (SDL_GameControllerGetAttached(ctrl[DeviceIndex])) {
Connect = true;
}
}
else {
output = ((FString)SDL_JoystickNameForIndex(DeviceIndex));
Connect = false;
if (SDL_JoystickGetAttached(joy[DeviceIndex])) {
Connect = true;
}
}
}
return output;
}
void UFight::CloseDevice()
{
n = SDL_NumJoysticks();
for (i = 0; i < n; ++i) {
if (effect_id[i] > 0) {
SDL_HapticStopAll(Directhaptic[i]);
SDL_HapticDestroyEffect(Directhaptic[i], effect_id[i]);
effect_id[i] = NULL;
}
if (Rumble[i] == 0) {
SDL_HapticRumbleStop(haptic[i]);
}
if (SDL_HapticOpened(i)) {
SDL_HapticClose(haptic[i]);
haptic[i] = NULL;
}
if (SDL_JoystickGetAttached(joy[i])) {
SDL_JoystickClose(joy[i]);
Directhaptic[i] = NULL;
Rumble[i] = -1;
joy[i] = NULL;
}
if (SDL_GameControllerGetAttached(ctrl[i])) {
SDL_GameControllerClose(ctrl[i]);
ctrl[i] = NULL;
}
}
SDL_Quit();
}
bool UFight::HapticRumblePlay(int DeviceIndex, float strength, int32 length)
{
if (Rumble[DeviceIndex] == 0) {
if (SDL_HapticRumblePlay(haptic[DeviceIndex], strength, length) == 0) {
return true;
}
else
return false;
}
return false;
}
bool UFight::HapticSawPlay(int DeviceIndex, int direction, int period, int magnitude, int length, int AttackLength, int FadeLength, float& Time)
{
if (Directhaptic[DeviceIndex] != NULL) {
if ((SDL_HapticQuery(Directhaptic[DeviceIndex]) & SDL_HAPTIC_SAWTOOTHDOWN) != 0) {
SDL_HapticStopAll(Directhaptic[DeviceIndex]);
SDL_HapticDestroyEffect(Directhaptic[DeviceIndex], effect_id[DeviceIndex]);
effect_id[DeviceIndex] = NULL;
// Create the effect
SDL_memset(&effect, 0, sizeof(SDL_HapticEffect)); // 0 is safe default
effect.type = SDL_HAPTIC_SAWTOOTHDOWN;
effect.periodic.direction.type = SDL_HAPTIC_POLAR; // Polar coordinates
effect.periodic.direction.dir[0] = direction;
effect.periodic.period = period;
effect.periodic.magnitude = magnitude;
effect.periodic.length = length;
effect.periodic.attack_length = AttackLength;
effect.periodic.fade_length = FadeLength;
// Upload the effect
effect_id[DeviceIndex] = SDL_HapticNewEffect(Directhaptic[DeviceIndex], &effect);
// Test the effect
SDL_HapticRunEffect(Directhaptic[DeviceIndex], effect_id[DeviceIndex], 1);
Time = length / 1000.0f;
return true;
}
}
return false;
}
bool UFight::InputGetState(int32 DeviceIndex, bool& DPAD_UP, bool& DPAD_DOWN, bool& DPAD_LEFT, bool& DPAD_RIGHT, TArray<bool>& BUTTONS)
{
i = SDL_NumJoysticks();
if (i > DeviceIndex) {
if (SDL_IsGameController(DeviceIndex)) {
if (SDL_GameControllerGetAttached(ctrl[DeviceIndex]) == SDL_TRUE) {
SDL_GameControllerUpdate();
DPAD_UP = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_DPAD_UP)) DPAD_UP = true;
if (SDL_GameControllerGetAxis(ctrl[DeviceIndex], SDL_CONTROLLER_AXIS_LEFTY) < -11000) DPAD_UP = true;
DPAD_DOWN = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_DPAD_DOWN)) DPAD_DOWN = true;
if (SDL_GameControllerGetAxis(ctrl[DeviceIndex], SDL_CONTROLLER_AXIS_LEFTY) > 11000) DPAD_DOWN = true;
DPAD_LEFT = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_DPAD_LEFT)) DPAD_LEFT = true;
if (SDL_GameControllerGetAxis(ctrl[DeviceIndex], SDL_CONTROLLER_AXIS_LEFTX) < -11000) DPAD_LEFT = true;
DPAD_RIGHT = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) DPAD_RIGHT = true;
if (SDL_GameControllerGetAxis(ctrl[DeviceIndex], SDL_CONTROLLER_AXIS_LEFTX) > 11000) DPAD_RIGHT = true;
BUTTONS.SetNum(14);
if (SDL_GameControllerGetAxis(ctrl[DeviceIndex], SDL_CONTROLLER_AXIS_TRIGGERLEFT) > 128) BUTTONS[6] = true;
else BUTTONS[6] = false;
if (SDL_GameControllerGetAxis(ctrl[DeviceIndex], SDL_CONTROLLER_AXIS_TRIGGERRIGHT) > 128) BUTTONS[7] = true;
else BUTTONS[7] = false;
if (cjoy[DeviceIndex]) {
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 0)) BUTTONS[2] = true;
else BUTTONS[2] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 1)) BUTTONS[1] = true;
else BUTTONS[1] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 2)) BUTTONS[3] = true;
else BUTTONS[3] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 3)) BUTTONS[0] = true;
else BUTTONS[0] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 4)) BUTTONS[4] = true;
else BUTTONS[4] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 5)) BUTTONS[5] = true;
else BUTTONS[5] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 6)) BUTTONS[8] = true;
else BUTTONS[8] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 7)) BUTTONS[9] = true;
else BUTTONS[9] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 8)) BUTTONS[10] = true;
else BUTTONS[10] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 9)) BUTTONS[11] = true;
else BUTTONS[11] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 10)) BUTTONS[12] = true;
else BUTTONS[12] = false;
if (SDL_JoystickGetButton(cjoy[DeviceIndex], 11)) BUTTONS[13] = true;
else BUTTONS[13] = false;
}
else {
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_A)) BUTTONS[2] = true;
else BUTTONS[2] = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_B)) BUTTONS[1] = true;
else BUTTONS[1] = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_X)) BUTTONS[3] = true;
else BUTTONS[3] = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_Y)) BUTTONS[0] = true;
else BUTTONS[0] = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) BUTTONS[4] = true;
else BUTTONS[4] = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) BUTTONS[5] = true;
else BUTTONS[5] = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_BACK)) BUTTONS[8] = true;
else BUTTONS[8] = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_START)) BUTTONS[9] = true;
else BUTTONS[9] = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_LEFTSTICK)) BUTTONS[10] = true;
else BUTTONS[10] = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_RIGHTSTICK)) BUTTONS[11] = true;
else BUTTONS[11] = false;
if (SDL_GameControllerGetButton(ctrl[DeviceIndex], SDL_CONTROLLER_BUTTON_GUIDE)) BUTTONS[12] = true;
else BUTTONS[12] = false;
BUTTONS[13] = false;
}
return true;
}
}
else
if (SDL_JoystickGetAttached(joy[DeviceIndex]) == SDL_TRUE) {
SDL_JoystickUpdate();
DPAD_UP = false;
DPAD_DOWN = false;
DPAD_LEFT = false;
DPAD_RIGHT = false;
if (SDL_JoystickGetHat(joy[DeviceIndex], 0) == SDL_HAT_UP) DPAD_UP = true;
if (SDL_JoystickGetHat(joy[DeviceIndex], 0) == SDL_HAT_DOWN) DPAD_DOWN = true;
if (SDL_JoystickGetHat(joy[DeviceIndex], 0) == SDL_HAT_LEFT) DPAD_LEFT = true;
if (SDL_JoystickGetHat(joy[DeviceIndex], 0) == SDL_HAT_RIGHT) DPAD_RIGHT = true;
if (SDL_JoystickGetHat(joy[DeviceIndex], 0) == SDL_HAT_RIGHTUP) {
DPAD_UP = true;
DPAD_RIGHT = true;
}
if (SDL_JoystickGetHat(joy[DeviceIndex], 0) == SDL_HAT_RIGHTDOWN) {
DPAD_DOWN = true;
DPAD_RIGHT = true;
}
if (SDL_JoystickGetHat(joy[DeviceIndex], 0) == SDL_HAT_LEFTUP) {
DPAD_UP = true;
DPAD_LEFT = true;
}
if (SDL_JoystickGetHat(joy[DeviceIndex], 0) == SDL_HAT_LEFTDOWN) {
DPAD_DOWN = true;
DPAD_LEFT = true;
}
if (SDL_JoystickGetAxis(joy[DeviceIndex], 1) < -11000) DPAD_UP = true;
if (SDL_JoystickGetAxis(joy[DeviceIndex], 1) > 11000) DPAD_DOWN = true;
if (SDL_JoystickGetAxis(joy[DeviceIndex], 0) < -11000) DPAD_LEFT = true;
if (SDL_JoystickGetAxis(joy[DeviceIndex], 0) > 11000) DPAD_RIGHT = true;
BUTTONS.SetNum(14);
if (SDL_JoystickGetButton(joy[DeviceIndex], 0)) BUTTONS[0] = true;
else BUTTONS[0] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 1)) BUTTONS[1] = true;
else BUTTONS[1] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 2)) BUTTONS[2] = true;
else BUTTONS[2] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 3)) BUTTONS[3] = true;
else BUTTONS[3] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 4)) BUTTONS[4] = true;
else BUTTONS[4] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 5)) BUTTONS[5] = true;
else BUTTONS[5] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 6)) BUTTONS[6] = true;
else BUTTONS[6] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 7)) BUTTONS[7] = true;
else BUTTONS[7] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 8)) BUTTONS[8] = true;
else BUTTONS[8] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 9)) BUTTONS[9] = true;
else BUTTONS[9] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 10)) BUTTONS[10] = true;
else BUTTONS[10] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 11)) BUTTONS[11] = true;
else BUTTONS[11] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 12)) BUTTONS[12] = true;
else BUTTONS[12] = false;
if (SDL_JoystickGetButton(joy[DeviceIndex], 13)) BUTTONS[13] = true;
else BUTTONS[13] = false;
return true;
}
}
return false;
}
bool UFight::IsXinput(int32 DeviceIndex)
{
if (Rumble[DeviceIndex] == 0) {
return true;
}
return false;
}
void UFight::HapticSawDestroy(int32 DeviceIndex)
{
SDL_HapticStopAll(Directhaptic[DeviceIndex]);
SDL_HapticDestroyEffect(Directhaptic[DeviceIndex], effect_id[DeviceIndex]);
effect_id[DeviceIndex] = NULL;
}