2019-09-28 14:29:44 +01:00
|
|
|
#include "pch.h"
|
|
|
|
#include "GameController.h"
|
|
|
|
#include "Game.h"
|
|
|
|
|
|
|
|
namespace Gaming {
|
|
|
|
|
|
|
|
GameController::GameController(int index)
|
2019-10-01 23:54:48 +01:00
|
|
|
: m_index(index) {
|
2019-09-28 14:29:44 +01:00
|
|
|
open();
|
|
|
|
}
|
|
|
|
|
|
|
|
GameController::~GameController() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameController::open() {
|
|
|
|
SDL_assert(::SDL_NumJoysticks() > 0);
|
|
|
|
if (::SDL_IsGameController(m_index)) {
|
2019-10-01 23:54:48 +01:00
|
|
|
m_gameController.reset(::SDL_GameControllerOpen(m_index), ::SDL_GameControllerClose);
|
|
|
|
if (m_gameController == nullptr)
|
|
|
|
SDLWrapper::throwSDLException("Unable to open game controller: ");
|
2019-09-28 14:29:44 +01:00
|
|
|
openHapticController();
|
2019-10-01 23:54:48 +01:00
|
|
|
auto name = ::SDL_GameControllerName(m_gameController.get());
|
2019-09-28 14:29:44 +01:00
|
|
|
::SDL_Log("Game controller name: %s", name);
|
2019-10-01 23:54:48 +01:00
|
|
|
} else {
|
2019-09-28 14:29:44 +01:00
|
|
|
::SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Joystick is not a game controller!!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameController::openHapticController() {
|
2019-10-01 23:54:48 +01:00
|
|
|
m_hapticController.reset(::SDL_HapticOpen(m_index), ::SDL_HapticClose);
|
|
|
|
if (m_hapticController == nullptr)
|
|
|
|
SDLWrapper::throwSDLException("Unable to open haptic controller: ");
|
|
|
|
SDLWrapper::verifySDLCall(::SDL_HapticRumbleInit(m_hapticController.get()), "Unable to initialise haptic controller: ");
|
|
|
|
m_hapticRumbleSupported = ::SDL_HapticRumbleSupported(m_hapticController.get()) != SDL_FALSE;
|
2019-09-28 14:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameController::closeHapticController() noexcept {
|
2019-10-01 23:54:48 +01:00
|
|
|
m_hapticController.reset();
|
2019-09-28 14:29:44 +01:00
|
|
|
m_hapticRumbleSupported = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameController::close() noexcept {
|
2019-10-01 23:54:48 +01:00
|
|
|
m_gameController.reset();
|
2019-09-28 14:29:44 +01:00
|
|
|
closeHapticController();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameController::startRumble() noexcept {
|
|
|
|
if (m_hapticRumbleSupported) {
|
2019-10-01 23:54:48 +01:00
|
|
|
if (::SDL_HapticRumblePlay(m_hapticController.get(), 1.0, 1000) < 0)
|
2019-09-28 14:29:44 +01:00
|
|
|
::SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Unable to start haptic rumble: %s", ::SDL_GetError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameController::stopRumble() noexcept {
|
|
|
|
if (m_hapticRumbleSupported) {
|
2019-10-01 23:54:48 +01:00
|
|
|
if (::SDL_HapticRumbleStop(m_hapticController.get()) < 0)
|
2019-09-28 14:29:44 +01:00
|
|
|
::SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Unable to stop haptic rumble: %s", ::SDL_GetError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|