Last active
October 13, 2020 12:16
-
-
Save ZachMassia/7651947 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1>c:\users\zachary\documents\visual studio 2013\projects\ogre-final-project\ogre-final-project\xboxcontroller.h(58): error C2146: syntax error : missing ';' before identifier 'state' | |
1>c:\users\zachary\documents\visual studio 2013\projects\ogre-final-project\ogre-final-project\xboxcontroller.h(58): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int | |
1>c:\users\zachary\documents\visual studio 2013\projects\ogre-final-project\ogre-final-project\xboxcontroller.h(58): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <exception> | |
#include <iostream> | |
#include <utility> | |
#include <string> | |
#include <vector> | |
#include <set> | |
#include <array> | |
#include <sstream> | |
#include <functional> | |
#include <algorithm> | |
#include <ctime> | |
#include <cstdlib> | |
#include <OIS/OIS.h> | |
#include <OGRE/Ogre.h> | |
#include <OGRE/ExampleApplication.h> | |
#include <SdkTrays.h> | |
#include <SdkCameraMan.h> | |
#include "Event.h" | |
#include "XboxController.h" | |
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
#include <XInput.h> | |
#pragma comment(lib, "XInput.lib") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "XboxController.h" | |
XboxController::XboxController(PAD_ID pad) : | |
id(pad) | |
{ | |
} | |
XboxController::~XboxController() | |
{ | |
if (connected) { | |
vibrate(0.0f, 0.0f); | |
} | |
} | |
bool XboxController::isConnected() const | |
{ | |
return connected; | |
} | |
void XboxController::update() | |
{ | |
// Clean the state. | |
memset(&state, 0, sizeof(XINPUT_STATE)); | |
// Get the current state. | |
DWORD result = XInputGetState((DWORD)id, &state); | |
connected = result == ERROR_SUCCESS; | |
// Only transfer the state to our own state if connected. | |
if (!connected) | |
return; | |
// Right Trigger. | |
int rTrigger = state.Gamepad.bRightTrigger; | |
if (rTrigger && rTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD) { | |
currentState.rightTrigger = (float)rTrigger / 255.0f; | |
} | |
// Left Trigger. | |
int lTrigger = state.Gamepad.bRightTrigger; | |
if (lTrigger && lTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD) { | |
currentState.leftTrigger = (float)lTrigger / 255.0f; | |
} | |
// Get all button states out of the bit-mask. | |
WORD btns = state.Gamepad.wButtons; | |
currentState.buttons[DPAD_UP] = btns & XINPUT_GAMEPAD_DPAD_UP; | |
currentState.buttons[DPAD_DOWN] = btns & XINPUT_GAMEPAD_DPAD_DOWN; | |
currentState.buttons[DPAD_LEFT] = btns & XINPUT_GAMEPAD_DPAD_LEFT; | |
currentState.buttons[DPAD_RIGHT] = btns & XINPUT_GAMEPAD_DPAD_RIGHT; | |
currentState.buttons[START] = btns & XINPUT_GAMEPAD_START; | |
currentState.buttons[BACK] = btns & XINPUT_GAMEPAD_BACK; | |
currentState.buttons[LEFT_THUMB] = btns & XINPUT_GAMEPAD_LEFT_THUMB; | |
currentState.buttons[RIGHT_THUMB] = btns & XINPUT_GAMEPAD_RIGHT_THUMB; | |
currentState.buttons[LEFT_SHOULDER] = btns & XINPUT_GAMEPAD_LEFT_SHOULDER; | |
currentState.buttons[RIGHT_SHOULDER] = btns & XINPUT_GAMEPAD_RIGHT_SHOULDER; | |
currentState.buttons[A] = btns & XINPUT_GAMEPAD_A; | |
currentState.buttons[B] = btns & XINPUT_GAMEPAD_B; | |
currentState.buttons[X] = btns & XINPUT_GAMEPAD_X; | |
currentState.buttons[Y] = btns & XINPUT_GAMEPAD_Y; | |
// Convenience to make code more readable. | |
SHORT thumbLX = state.Gamepad.sThumbLX; | |
SHORT thumbLY = state.Gamepad.sThumbLY; | |
SHORT thumbRX = state.Gamepad.sThumbRX; | |
SHORT thumbRY = state.Gamepad.sThumbRY; | |
static const int L_DEADZONE = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE; | |
static const int R_DEADZONE = XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE; | |
static const auto bothInRange = [](int a, int b, int min, int max) { | |
return (a > min) && (a < max) && | |
(b > min) && (a < max); | |
}; | |
// Left Stick. | |
if (bothInRange(thumbLX, thumbLY, -L_DEADZONE, L_DEADZONE)) { | |
// Left stick in dead zone. | |
currentState.leftStick = Ogre::Vector2::ZERO; | |
} | |
else { | |
// Get the left stick into a normalized Ogre::Vector3. | |
currentState.leftStick.x = thumbLX; | |
currentState.leftStick.y = thumbLY; | |
currentState.leftStick.normalise(); | |
} | |
// Right Stick. | |
if (bothInRange(thumbRX, thumbRY, -R_DEADZONE, R_DEADZONE)) { | |
// Left stick in dead zone. | |
currentState.rightStick = Ogre::Vector2::ZERO; | |
} | |
else { | |
// Get the left stick into a normalized Ogre::Vector3. | |
currentState.rightStick.x = thumbLX; | |
currentState.rightStick.y = thumbLY; | |
currentState.rightStick.normalise(); | |
} | |
} | |
void XboxController::vibrate(float leftMotor, float rightMotor) | |
{ | |
XINPUT_VIBRATION vib; | |
memset(&vib, 0, sizeof(XINPUT_VIBRATION)); | |
vib.wLeftMotorSpeed = (int)leftMotor * 65535; | |
vib.wRightMotorSpeed = (int)rightMotor * 65535; | |
XInputSetState((DWORD)id, &vib); | |
} | |
const XboxController::GamePadState& XboxController::getState() const | |
{ | |
return currentState; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "StdAfx.h" | |
class XboxController | |
{ | |
public: | |
enum BUTTON_ID { | |
DPAD_UP, | |
DPAD_DOWN, | |
DPAD_LEFT, | |
DPAD_RIGHT, | |
START, | |
BACK, | |
LEFT_THUMB, | |
RIGHT_THUMB, | |
LEFT_SHOULDER, | |
RIGHT_SHOULDER, | |
A, | |
B, | |
X, | |
Y, | |
BTN_MAX | |
}; | |
enum PAD_ID { | |
PLAYER_ONE, | |
PLAYER_TWO, | |
PLAYER_THREE, | |
PLAYER_FOUR | |
}; | |
struct GamePadState | |
{ | |
// Buttons | |
std::array<bool, BTN_MAX> buttons; | |
// Sticks | |
Ogre::Vector2 leftStick; | |
Ogre::Vector2 rightStick; | |
// Triggers | |
float leftTrigger; | |
float rightTrigger; | |
GamePadState() : | |
leftStick(Ogre::Vector2::ZERO), rightStick(Ogre::Vector2::ZERO), | |
leftTrigger(0.0f), rightTrigger(0.0f) | |
{ | |
} | |
}; | |
private: | |
// The exported state. | |
GamePadState currentState; | |
// The internal XInput state of the controller. | |
XINPUT_STATE state; | |
// Corresponds to the illuminated quadrant on the controller. | |
PAD_ID id; | |
bool connected; | |
public: | |
XboxController(PAD_ID pad); | |
~XboxController(); | |
bool isConnected() const; | |
void update(); | |
void vibrate(float leftMotor = 0.0f, float rightMotor = 0.0f); | |
const GamePadState& getState() const; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment