Created
March 19, 2023 20:57
-
-
Save W4RH4WK/e4b05b5e0396f55f192c39c48542efea to your computer and use it in GitHub Desktop.
SDL Game Controller RT/LT RB/LB
This file contains hidden or 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
cmake_minimum_required(VERSION 3.25) | |
project(sdlinputtest LANGUAGES CXX) | |
find_package(SDL2 REQUIRED) | |
add_executable(sdlinputtest sdlinputtest.cpp) | |
target_link_libraries(sdlinputtest PRIVATE SDL2::SDL2 SDL2::SDL2main) | |
if(WIN32) | |
add_custom_command(TARGET sdlinputtest POST_BUILD | |
COMMAND ${CMAKE_COMMAND} -E copy_if_different | |
$<TARGET_PROPERTY:SDL2::SDL2,IMPORTED_LOCATION> $<TARGET_FILE_DIR:sdlinputtest>) | |
endif() |
This file contains hidden or 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 <chrono> | |
#include <cstdio> | |
#include <thread> | |
#include <SDL.h> | |
using namespace std::chrono_literals; | |
void printInput(SDL_GameController* controller) | |
{ | |
printf("[%6d] ", SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT)); | |
printf("[%6d] ", SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT)); | |
if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) { | |
printf("[P1] "); | |
} | |
if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) { | |
printf("[P2] "); | |
} | |
printf("\n"); | |
} | |
int SDL_main(int, char**) | |
{ | |
SDL_Init(SDL_INIT_EVERYTHING); | |
SDL_GameController* controller; | |
for (int id = 0; id < 10; ++id) { | |
if (SDL_IsGameController(id)) { | |
printf("Controller: %d\n", id); | |
controller = SDL_GameControllerOpen(id); | |
break; | |
} | |
} | |
bool quit = false; | |
while (!quit) { | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) { | |
switch (event.type) { | |
case SDL_QUIT: | |
quit = true; | |
break; | |
} | |
} | |
printInput(controller); | |
std::this_thread::sleep_for(50ms); | |
} | |
SDL_GameControllerClose(controller); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment