本文整理汇总了C++中SDL_GameControllerOpen函数的典型用法代码示例。如果您正苦于以下问题:C++ SDL_GameControllerOpen函数的具体用法?C++ SDL_GameControllerOpen怎么用?C++ SDL_GameControllerOpen使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SDL_GameControllerOpen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int
main(int argc, char *argv[])
{
const char *name;
int i;
int nController = 0;
SDL_GameController *gamecontroller;
SDL_SetHint( SDL_HINT_GAMECONTROLLERCONFIG, "341a3608000000000000504944564944,Aferglow PS3 Controller,a:b1,b:b2,y:b3,x:b0,start:b9,guide:b12,back:b8,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7" );
/* Initialize SDL (Note: video is required to start event loop) */
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
/* Print information about the controller */
for (i = 0; i < SDL_NumJoysticks(); ++i) {
if ( SDL_IsGameController(i) )
{
nController++;
name = SDL_GameControllerNameForIndex(i);
printf("Game Controller %d: %s\n", i, name ? name : "Unknown Controller");
}
}
printf("There are %d game controllers attached\n", nController);
if (argv[1]) {
int nreportederror = 0;
SDL_Event event;
gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
while ( s_ForceQuit == SDL_FALSE ) {
if (gamecontroller == NULL) {
if ( nreportederror == 0 ) {
printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
nreportederror = 1;
}
} else {
nreportederror = 0;
WatchGameController(gamecontroller);
SDL_GameControllerClose(gamecontroller);
}
gamecontroller = NULL;
SDL_WaitEvent( &event );
if ( event.type == SDL_JOYDEVICEADDED )
gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
}
}
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER );
return (0);
}
示例2: refresh_controllers
void refresh_controllers()
{
for (s32 i = 0; i < MAX_GAMEPADS; i++)
{
if (haptics[i])
{
SDL_HapticClose(haptics[i]);
haptics[i] = nullptr;
}
if (controllers[i])
{
SDL_GameControllerClose(controllers[i]);
controllers[i] = nullptr;
}
}
for (s32 i = 0; i < SDL_NumJoysticks(); i++)
{
if (SDL_IsGameController(i))
{
controllers[i] = SDL_GameControllerOpen(i);
SDL_Joystick* joystick = SDL_GameControllerGetJoystick(controllers[i]);
if (SDL_JoystickIsHaptic(joystick))
{
haptics[i] = SDL_HapticOpenFromJoystick(joystick);
if (SDL_HapticRumbleInit(haptics[i])) // failed
{
SDL_HapticClose(haptics[i]);
haptics[i] = nullptr;
}
}
}
}
}
示例3: close
bool GameController::open(int c)
{
if (sdl_device)
{
close();
}
if (c < 0 || c >= SDL_NumJoysticks())
{
return false;
}
if (!SDL_IsGameController(c))
{
return false;
}
sdl_device = SDL_GameControllerOpen(c);
if (sdl_device == nullptr)
{
printlog("Error: Failed to open game controller! SDL Error: %s\n", SDL_GetError());
}
else
{
id = c;
printlog("Successfully initialized game controller!\n");
name = (SDL_GameControllerNameForIndex(c));
printlog("Controller name is \"%s\"", name.c_str());
}
return (sdl_device != nullptr);
}
示例4: _openJoysticks
JoystickScriptingInterface::JoystickScriptingInterface() :
#ifdef HAVE_SDL2
_openJoysticks(),
#endif
_isInitialized(false)
{
#ifdef HAVE_SDL2
bool initSuccess = (SDL_Init(SDL_INIT_GAMECONTROLLER) == 0);
if (initSuccess) {
int joystickCount = SDL_NumJoysticks();
for (int i = 0; i < joystickCount; i++) {
SDL_GameController* controller = SDL_GameControllerOpen(i);
if (controller) {
SDL_JoystickID id = getInstanceId(controller);
Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller);
_openJoysticks[id] = joystick;
}
}
_isInitialized = true;
} else {
qDebug() << "Error initializing SDL";
}
#endif
}
示例5: list_joysticks
void list_joysticks()
{
int num_joysticks = SDL_NumJoysticks();
if (num_joysticks == 0)
{
printf("No joysticks were found\n");
}
else
{
printf("Found %d joystick(s)\n\n", num_joysticks);
for(int joy_idx = 0; joy_idx < num_joysticks; ++joy_idx)
{
SDL_Joystick* joy = SDL_JoystickOpen(joy_idx);
if (!joy)
{
fprintf(stderr, "Unable to open joystick %d\n", joy_idx);
}
else
{
SDL_GameController* gamepad = SDL_GameControllerOpen(joy_idx);
print_joystick_info(joy_idx, joy, gamepad);
if (gamepad)
{
SDL_GameControllerClose(gamepad);
}
SDL_JoystickClose(joy);
}
}
}
}
示例6: SDL_Init
Game::Game(){
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
this->mainWindow = SDL_CreateWindow("Highway Crossing Frog", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, this->width, this->height, SDL_WINDOW_SHOWN);
this->mainRenderer = SDL_CreateRenderer(this->mainWindow, -1, SDL_RENDERER_ACCELERATED);
if(SDL_GameControllerAddMapping(GASIA_PS2_MAP) == 1){
printf("Managed to add silly PS2 adapter thing.\n");
}
if(SDL_GameControllerAddMapping(GASIA_PS2_LIN) == 1){
printf("Managed to add silly PS2 adapter thing on Linux.\n");
}
if(SDL_NumJoysticks() > 0){
for (int i = 0; i < SDL_NumJoysticks(); i++) {
if(SDL_IsGameController(i)){
this->gamePad = SDL_GameControllerOpen(i);
if(this->gamePad){
printf("Opened controller %i\n", i);
} else {
printf("Failed to open controller %i\n", i);
}
}
}
} else {
printf("No controllers found.\n");
}
this->gWorld = new GameWorld(this->mainRenderer);
this->running = true;
}
示例7: SDL_NumJoysticks
void SDL2Manager::init() {
#ifdef HAVE_SDL2
bool initSuccess = (SDL_Init(SDL_INIT_GAMECONTROLLER) == 0);
if (initSuccess) {
int joystickCount = SDL_NumJoysticks();
for (int i = 0; i < joystickCount; i++) {
SDL_GameController* controller = SDL_GameControllerOpen(i);
if (controller) {
SDL_JoystickID id = getInstanceId(controller);
if (!_openJoysticks.contains(id)) {
Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller);
_openJoysticks[id] = joystick;
auto userInputMapper = DependencyManager::get<UserInputMapper>();
joystick->registerToUserInputMapper(*userInputMapper);
joystick->assignDefaultInputMapping(*userInputMapper);
emit joystickAdded(joystick);
}
}
}
_isInitialized = true;
}
else {
qDebug() << "Error initializing SDL2 Manager";
}
#endif
}
示例8: closeController
void Joystick::openController() {
closeController();
if (!SDL_IsGameController(m_deviceIndex)) {
return;
}
m_controllerHandle = SDL_GameControllerOpen(m_deviceIndex);
}
示例9: SDL_GameControllerOpen
// Opens up to 4 currently connected controllers
void InputEngine::openControllers()
{
for (int i = 0; i < SDL_NumJoysticks() && i < MAX_NUM_CONTROLLERS; i++)
{
controllers[i] = SDL_GameControllerOpen(i);
}
std::cout << "NUM CONTROLLERS: " << SDL_NumJoysticks() << std::endl;
}
示例10: SDL_JoystickOpen
void InputDaemon::addInputDevice(int index)
{
SDL_Joystick *joystick = SDL_JoystickOpen(index);
if (joystick)
{
SDL_JoystickID tempJoystickID = SDL_JoystickInstanceID(joystick);
if (!joysticks->contains(tempJoystickID))
{
QSettings *settings = new QSettings(PadderCommon::configFilePath, QSettings::IniFormat);
settings->beginGroup("Mappings");
QString temp;
SDL_JoystickGUID tempGUID = SDL_JoystickGetGUID(joystick);
char guidString[65] = {'0'};
SDL_JoystickGetGUIDString(tempGUID, guidString, sizeof(guidString));
temp = QString(guidString);
bool disableGameController = settings->value(QString("%1Disable").arg(temp), false).toBool();
if (SDL_IsGameController(index) && !disableGameController)
{
SDL_GameController *controller = SDL_GameControllerOpen(index);
if (controller)
{
SDL_Joystick *sdlStick = SDL_GameControllerGetJoystick(controller);
SDL_JoystickID tempJoystickID = SDL_JoystickInstanceID(sdlStick);
if (!joysticks->contains(tempJoystickID))
{
GameController *damncontroller = new GameController(controller, index, this);
joysticks->insert(tempJoystickID, damncontroller);
trackcontrollers.insert(tempJoystickID, damncontroller);
// Force close of settings file.
settings->endGroup();
delete settings;
settings = 0;
emit deviceAdded(damncontroller);
}
}
}
else
{
Joystick *curJoystick = new Joystick(joystick, index, this);
joysticks->insert(tempJoystickID, curJoystick);
trackjoysticks.insert(tempJoystickID, curJoystick);
// Force close of settings file.
settings->endGroup();
delete settings;
settings = 0;
emit deviceAdded(curJoystick);
}
}
}
}
示例11: initInput
static void initInput(SDLInputContext* sdlIC) {
for (int i = 0; i < MAX_SDL_CONTROLLERS; i++) {
if (SDL_IsGameController(i)) {
if(!(sdlIC->controllers[i] = SDL_GameControllerOpen(i))){
printSDLErrorAndExit();
}
}
}
}
示例12: m_Ctrl
SDLController::SDLController(int j_idx) : m_Ctrl(0)
{
m_Ctrl = SDL_GameControllerOpen(j_idx);
if (!m_Ctrl)
{
throw Wobbly("Couldn't open controller: %s", SDL_GetError());
}
//const char *name = SDL_GameControllerNameForIndex(j_idx);
}
示例13: IN_StartupJoystick
void IN_StartupJoystick (void)
{
int i;
int nummappings;
char controllerdb[MAX_OSPATH];
SDL_GameController *gamecontroller;
if (COM_CheckParm("-nojoy"))
return;
if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == -1 )
{
Con_Warning("could not initialize SDL Game Controller\n");
return;
}
// Load additional SDL2 controller definitions from gamecontrollerdb.txt
q_snprintf (controllerdb, sizeof(controllerdb), "%s/gamecontrollerdb.txt", com_basedir);
nummappings = SDL_GameControllerAddMappingsFromFile(controllerdb);
if (nummappings > 0)
Con_Printf("%d mappings loaded from gamecontrollerdb.txt\n", nummappings);
// Also try host_parms->userdir
if (host_parms->userdir != host_parms->basedir)
{
q_snprintf (controllerdb, sizeof(controllerdb), "%s/gamecontrollerdb.txt", host_parms->userdir);
nummappings = SDL_GameControllerAddMappingsFromFile(controllerdb);
if (nummappings > 0)
Con_Printf("%d mappings loaded from gamecontrollerdb.txt\n", nummappings);
}
for (i = 0; i < SDL_NumJoysticks(); i++)
{
const char *joyname = SDL_JoystickNameForIndex(i);
if ( SDL_IsGameController(i) )
{
const char *controllername = SDL_GameControllerNameForIndex(i);
gamecontroller = SDL_GameControllerOpen(i);
if (gamecontroller)
{
Con_Printf("detected controller: %s\n", controllername != NULL ? controllername : "NULL");
joy_active_instaceid = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller));
joy_active_controller = gamecontroller;
break;
}
else
{
Con_Warning("failed to open controller: %s\n", controllername != NULL ? controllername : "NULL");
}
}
else
{
Con_Warning("joystick missing controller mappings: %s\n", joyname != NULL ? joyname : "NULL" );
}
}
}
示例14: get_controller
void get_controller(void){
if(SDL_NumJoysticks() == 1){
printf("Dualshock3 connected, initializing...\n");
controller = SDL_GameControllerOpen(0);
}
else {
printf("Dualshock3 not connected.\n");
controller = NULL;
}
}
示例15: SDL_GameControllerNameForIndex
void Renderer::detect_gamepads(){
if(SDL_NumJoysticks() >= 1){
std::cout << "Found one or more gamepads! Using '" << SDL_GameControllerNameForIndex(0) << "'" << std::endl;
gamepad = SDL_GameControllerOpen(0);
}
else {
std::cout << "No gamepads found. You will be able to control the visualization with mouse & keyboard." << std::endl;
gamepad = NULL;
}
}