I'm having some issues trying to use SDL (version 2.0.3) for handling multiple game controllers in a SDL/OpenGL/C++ program.
I'm updating events using a SDL_PollEvent(&m_events) loop and looking for SDL_JOYBUTTONDOWN (or SDL_JOYBUTTONUP/SDL_JOYAXISMOTION/SDL_HATMOTION) events with switch(m_events.type). What I want is to use the values of m_events.jbutton.which and m_events.jbutton.button (or the equivalents for axes and hat) to update arrays containing the state of my controllers.
I'm using Windows 7 Pro 64bit and Code::Blocks 13.12 though it's not supposed to change anything.
Here is my (debug) code, I tried to keep just the relevant parts :
main.cpp
#include "SceneOpenGL.h"
int main(int argc, char *argv[])
{
SceneOpenGL scene(/* some args for window size */);
if(!scene.initWindow())
return -1;
scene.mainLoop();
return 0;
}
SceneOpenGL.h
#ifndef SCENEOPENGL_H_INCLUDED
#define SCENEOPENGL_H_INCLUDED
#include <iostream>
#include "Input.h"
#include <SDL2/SDL.h>
class SceneOpenGL
{
public:
SceneOpenGL(/* some args for window size */);
~SceneOpenGL();
bool initWindow();
void mainLoop();
private:
SDL_Window* m_window;
SDL_Event m_event;
Input m_input;
bool m_useJoysticks;
};
#endif
SceneOpenGL.cpp
SceneOpenGL::SceneOpenGL(/* some args for window size */) :
m_window(0), m_input()
{
if(SDL_NumJoysticks())
m_useJoysticks = true;
}
bool SceneOpenGL::initWindow()
{
if(SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) == -1)
{
std::cout << "Error while initializing SDL : " << SDL_GetError() << std::endl;
SDL_Quit();
return false;
}
/*
**** Creation of SDL Window and GLContext ****
*/
return true;
}
void SceneOpenGL::mainLoop()
{
if(m_useJoysticks)
{
m_input.openJoysticks();
SDL_JoystickEventState(SDL_ENABLE);
}
while(!m_input.terminate()) // m_input.terminate() sends true when SDL receives SDL_WINDOWEVENT_CLOSE event
{
m_input.updateEvents(); // this is the other loop where I update the joystick, keyboard & mouse state
/* ...
**** moving opengl objects & render ****
*/
}
}
Input.h
#ifndef INPUT_H_INCLUDED
#define INPUT_H_INCLUDED
#include <SDL2/SDL.h>
#include <iostream>
class Input
{
public:
Input();
~Input();
void updateEvents();
bool terminate() const;
void openJoysticks();
private:
SDL_Event m_events;
int m_numJoysticks;
SDL_Joysticks* m_joysticks[4]; // Array containing the first 4 joysicks
bool m_terminate; // used for ending program
};
Input.cpp
#include "Input.h"
Input::Input() :
m_numJoysticks(0), m_terminate(false)
{}
Input::~Input()
{
for(int i(0); i < SDL_NumJoysticks(); i++)
SDL_JoystickClose(m_joysticks[i]); // Closes joysticks before exiting
}
void Input::openJoysticks()
{
m_numJoysticks = SDL_NumJoysticks; // Counts the connected joysticks
if(m_numJoysticks > 4)
m_numJoysticks = 4; // Sets maximum joysticks to 4
for(int i(0); i < m_numJoysticks; i++)
{
m_joysticks[i] = SDL_JoystickOpen(0) // Open existing joysticks
std::cout << "Joystick #" << i << " OK" << std::endl;
}
}
void Input::updateEvents()
{
while(SDL_PollEvent(&m_events))
{
switch(m_events.type)
{
/* ... Keyboard and mouse events, no problem there */
case SDL_JOYBUTTONDOWN:
std::cout << "JOYBUTTONDOWN" << std::endl;
std::cout << "type : " << m_events.jbutton.type << std::endl;
std::cout << "which : " << m_events.jbutton.which << std::endl;
std::cout << "button : " << m_events.jbutton.button << std::endl;
std::cout << "state : " << m_events.jbutton.state << std:: endl << std::endl;
break;
/* ... Same thing for SDL_JOYBUTTONUP, SDL_JOYAXISMOTION, SDL_JOYHATMOTION */
case SDL_WINDOWEVENT:
if(m_events.window.event == SDL_WINDOWEVENT_CLOSE)
m_terminate = true; // end program
break;
default:
break;
}
}
}
bool Input::terminate() const
{
return m_terminate;
}
And so here is the console output when I press A, B, X, Y on my x360 controller (= buttons 0, 1, 2 & 3) :
Joystick #0 OK
JOYBUTTONDOWN
type : 1539
which : 65536
button : §
state : Ý
JOYBUTTONDOWN
type : 1539
which : 1996554496
button :
state :
JOYBUTTONDOWN
type : 1539
which : 1996554752
button :
state :
JOYBUTTONDOWN
type : 1539
which : 1996555008
button :
state :
Process returned 0 (0x0) execution time : 7.437 s
Press any key to continue.
As you can see it doesn't seem really OK. I even sometimes get different values for the same input (like here with the first button pressed), but I noticed that the difference between "which" values is 256 so it's not totally random. And the values are mostly the same if I restart the program or press the same button twice. Well it seems like "event.jbutton.which" contains the informations of the button index (which is supposed to be a Uint8)
I don't know if I'm doing it wrong or if it's a bug in SDL2.
Your numbers appear to be the bitwise OR of a variety of flag fields, possibly with a value field.
Examining them in hex will make this obvious:
1996554752 = 0x77010200
1996554496 = 0x77010100
1996555008 = 0x77010300
It looks like your "button number" might be in bits 15-8, ie, the values 2, 1, 3
This is almost surely covered in the appropriate documentation