Friday, 15 June 2012

c++ - SDL_Jostick is sending weird ints -



c++ - SDL_Jostick is sending weird ints -

i'm having issues trying utilize sdl (version 2.0.3) handling multiple game controllers in sdl/opengl/c++ program.

i'm updating events using sdl_pollevent(&m_events) loop , looking sdl_joybuttondown (or sdl_joybuttonup/sdl_joyaxismotion/sdl_hatmotion) events switch(m_events.type). want utilize values of m_events.jbutton.which , m_events.jbutton.button (or equivalents axes , hat) update arrays containing state of controllers.

i'm using windows 7 pro 64bit , code::blocks 13.12 though it's not supposed alter anything.

here (debug) code, tried maintain relevant parts :

main.cpp

#include "sceneopengl.h" int main(int argc, char *argv[]) { sceneopengl scene(/* args window size */); if(!scene.initwindow()) homecoming -1; scene.mainloop(); homecoming 0; }

sceneopengl.h

#ifndef sceneopengl_h_included #define sceneopengl_h_included #include <iostream> #include "input.h" #include <sdl2/sdl.h> class sceneopengl { public: sceneopengl(/* args 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(/* args 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(); homecoming false; } /* **** creation of sdl window , glcontext **** */ homecoming 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(); // other loop update 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 first 4 joysicks bool m_terminate; // used ending programme };

input.cpp

#include "input.h" input::input() : m_numjoysticks(0), m_terminate(false) {} input::~input() { for(int i(0); < sdl_numjoysticks(); i++) sdl_joystickclose(m_joysticks[i]); // closes joysticks before exiting } void input::openjoysticks() { m_numjoysticks = sdl_numjoysticks; // counts connected joysticks if(m_numjoysticks > 4) m_numjoysticks = 4; // sets maximum joysticks 4 for(int i(0); < m_numjoysticks; i++) { m_joysticks[i] = sdl_joystickopen(0) // open existing joysticks std::cout << "joystick #" << << " ok" << std::endl; } } void input::updateevents() { while(sdl_pollevent(&m_events)) { switch(m_events.type) { /* ... keyboard , 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 sdl_joybuttonup, sdl_joyaxismotion, sdl_joyhatmotion */ case sdl_windowevent: if(m_events.window.event == sdl_windowevent_close) m_terminate = true; // end programme break; default: break; } } } bool input::terminate() const { homecoming m_terminate; }

and here console output when press a, b, x, y on x360 controller (= buttons 0, 1, 2 & 3) :

joystick #0 ok joybuttondown type : 1539 : 65536 button : § state : Ý joybuttondown type : 1539 : 1996554496 button : state : joybuttondown type : 1539 : 1996554752 button : state : joybuttondown type : 1539 : 1996555008 button : state : process returned 0 (0x0) execution time : 7.437 s press key continue.

as can see doesn't seem ok. different values same input (like here first button pressed), noticed difference between "which" values 256 it's not totally random. , values same if restart programme or press same button twice. seems "event.jbutton.which" contains informations of button index (which supposed uint8)

i don't know if i'm doing wrong or if it's bug in sdl2.

your numbers appear bitwise or of variety of flag fields, perchance value field.

examining them in hex create obvious:

1996554752 = 0x77010200 1996554496 = 0x77010100 1996555008 = 0x77010300

it looks "button number" might in bits 15-8, ie, values 2, 1, 3

this certainly covered in appropriate documentation

c++ sdl sdl-2 joystick

No comments:

Post a Comment