The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Joystick Functions working ?
Aeliss


Joined: 21 Apr 2014
Posts: 2
Hi all, I have lot of problems with all joystick functions, this is a small code I use to make tests

Code:
int main(int argc, char* argv[])
{
   if ( SDL_InitSubSystem ( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 0 )
   {
      fprintf ( stderr, "Unable to initialize Joystick: %s\n", SDL_GetError() );
      return -1;
   }

   printf ( "%i joysticks found\n", SDL_NumJoysticks () );
   
   // TODO add check
   SDL_Joystick* joy1 = SDL_JoystickOpen ( 0 );

   if ( joy1 == NULL )
      printf ( "could not open joystick\n" );

   printf ( "%s nom\n", SDL_JoystickName( 0 ) );
   printf ( "%i axes\n", SDL_JoystickNumAxes ( joy1 ) );
   printf ( "%i trackball\n", SDL_JoystickNumBalls ( joy1 ) );
   printf ( "%i heads\n", SDL_JoystickNumHats ( joy1 ) );
   printf ( "%i booton\n", SDL_JoystickNumButtons ( joy1 ) );

   SDL_JoystickEventState (SDL_ENABLE);
   // this will alter the behaviour of the event queue of the sdl system
   //SDL_JoystickEventState ( SDL_QUERY );
   SDL_Event event;


Ans here 2 codes to make test

Code:
   while ( 1 ) {
      SDL_JoystickEventState (SDL_ENABLE);
          SDL_JoystickUpdate();
         while(SDL_PollEvent(&event))
         {
            switch(event.type) {
               case SDL_JOYBUTTONUP:
                 printf("Oh! Key press\n");
                 break;

               case SDL_JOYBUTTONDOWN:
                 printf("Oh! Key up\n");
                 break;


               default:
                 printf("I don't know what this event is!\n");
            }
          }
         Sleep(100);
   }

and
Code:
   while ( 1 )
   {
      // This is needed in the even queue
      SDL_JoystickUpdate ();

      // now we query for some input
      for ( int i=0; i < SDL_JoystickNumButtons ( joy1 ); ++i )
      {
         unsigned int n = SDL_JoystickGetButton ( joy1, i );
         if ( n != 0 )
            printf ( "found you pressed button %i\n", i );
      }

      for ( int i=0; i < SDL_JoystickNumAxes ( joy1 ); ++i )
      {
         signed short a = SDL_JoystickGetAxis ( joy1, i );
         if (( a > 10 ) || (a < -10))
         {
            printf ( "axis %i is %d\n", i,a );
         }
      }
   }


The fisrt one detect only SDL_JOYBUTTONUP and never SDL_JOYBUTTONDOWN.
The second never detect button with sdl 2.0 but work with sdl 1.2 (axes are working with both sdl).

Im using VC2010 on Windows, someone tell me the first is working on linux, but I can't try it.

There is someone who are using joystick with sdl on Windows, and how ???
Aeliss


Joined: 21 Apr 2014
Posts: 2
For information, I have find how to make this function working.
Just add
SDL_SetHint("SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS", "1");

Even if the application have the focus only SDL_JOYBUTTONUP work never SDL_JOYBUTTONDOWN.
And for the second part it s still a mystery.