Add basic controller Support

Fix more memory leaks (seeing a trend here)
This commit is contained in:
Bram Verhulst
2024-04-24 21:38:14 +02:00
parent 1b90f222a4
commit f5d352239c
14 changed files with 2267 additions and 166 deletions

View File

@@ -40,7 +40,7 @@ void BaseGame::InitializeGameEngine()
#endif
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO /*| SDL_INIT_AUDIO*/) < 0)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC/*| SDL_INIT_AUDIO*/) < 0)
{
std::cerr << "BaseGame::Initialize( ), error when calling SDL_Init: " << SDL_GetError() << std::endl;
return;
@@ -145,8 +145,27 @@ void BaseGame::InitializeGameEngine()
std::cerr << "BaseGame::Initialize( ), error when calling Mix_OpenAudio: " << Mix_GetError() << std::endl;
return;
}
//Initialize controller
m_pGameController = nullptr;
if (SDL_NumJoysticks() < 1)
{
std::cout << "Warning: No controller connected!" << std::endl;
}
else
{
//Load joystick
m_pGameController = SDL_GameControllerOpen(0);
if (m_pGameController == nullptr)
{
std::cout << "Warning: Unable to open game controller! SDL Error: " << SDL_GetError() << std::endl;
}
}
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
m_Initialized = true;
}
@@ -200,6 +219,12 @@ void BaseGame::Run()
case SDL_MOUSEWHEEL:
this->ProcessMouseWheelEvent(e.wheel);
break;
case SDL_CONTROLLERBUTTONDOWN:
std::cout << "Button " << int(e.jbutton.button) << " down on controller " << int(e.jbutton.which) << std::endl;
if(e.cbutton.button == SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_X) {
std::cout << "X button pressed" << std::endl;
}
break;
}
}