Re : New user, question about joystick

Hello Mark,

Just a stupid remark:

They say in the SDL wiki that SDL_InitSubSystem must be called after
SDL_Init():
http://www.libsdl.org/cgi/docwiki.cgi/SDL_InitSubSystem

I don’t see any call to SDL_Init() in your code. Check out if it changes something …

Julien.

----- Message d’origine ----De : Mark
? : sdl at libsdl.org
Envoy? le : Vendredi, 18 Juillet 2008, 15h33mn 07s
Objet : [SDL] New user, question about joystick.

Newbie here, trying to make my joystick work in a simple console application.
I can’t get out my axis values, it just repeats 0.0000. Any thoughts? Do I need
to check for events?
It goes into a loop I know…

#include <stdlib.h>
#include <SDL.h>

int main(int argc, char *argv[])
{
SDL_Joystick *joy;
// Initialize the joystick subsystem
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
// Check for joystick
if(SDL_NumJoysticks()>0){
// Open joystick
joy=SDL_JoystickOpen(0);

if(joy)
{
printf(“Opened Joystick 0\n”);
printf(“Name: %s\n”, SDL_JoystickName(0));
printf(“Number of Axes: %d\n”, SDL_JoystickNumAxes(joy));
printf(“Number of Buttons: %d\n”, SDL_JoystickNumButtons(joy));
printf(“Number of Balls: %d\n”, SDL_JoystickNumBalls(joy));
}
else
printf(“Couldn’t open Joystick 0\n”);

int SDL_JoystickEventState(true);

while(1) {
        printf("%f",SDL_JoystickGetAxis(joy, 0));
}
return 0;

}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

  _____________________________________________________________________________ 

Envoyez avec Yahoo! Mail. Une boite mail plus intelligente http://mail.yahoo.fr

Actually it returns a Sint16 … but the implicit cast to float (%f) should work isn’t it ?
Anyway, better using %d for that purpose, to make sure …

Just a remark, if you say SDL_Init(SDL_INIT_JOYSTICK) then, if I’m correct,
SDL_InitSubSystem(SDL_INIT_JOYSTICK) is implicitely called, so you don’t need it.

However, you’d do:

SDL_Init(); /* no args */
SDL_InitSubSystem(SDL_INIT_JOYSTICK);

Correct me if I’m wrong

Julien

----- Message d’origine ----De : Peter Mackay <mackay.pete+sdl at gmail.com>
? : A list for developers using the SDL library. (includes SDL-announce)
Envoy? le : Vendredi, 18 Juillet 2008, 15h59mn 03s
Objet : Re: [SDL] New user, question about joystick.

Hmm one more thing. Does SDL_JoystickAxis return a float? I thought it
was a Uint16 or something like that.

Pete

2008/7/18 Mark :

Thanks guys
Made changes you suggested but not working.
(It does find it)

#include <stdlib.h>
#include <SDL.h>

void SDL_JoystickUpdate(void);
int main(int argc, char *argv[])
{
SDL_Joystick *joy;
SDL_Init(SDL_INIT_JOYSTICK);
// Initialize the joystick subsystem
SDL_InitSubSystem(SDL_INIT_JOYSTICK);

// Check for joystick
if(SDL_NumJoysticks()>0){
// Open joystick
joy=SDL_JoystickOpen(0);

if(joy)
{
printf(“Opened Joystick 0\n”);
printf(“Name: %s\n”, SDL_JoystickName(0));
printf(“Number of Axes: %d\n”, SDL_JoystickNumAxes(joy));
printf(“Number of Buttons: %d\n”, SDL_JoystickNumButtons(joy));
printf(“Number of Balls: %d\n”, SDL_JoystickNumBalls(joy));
}
else
printf(“Couldn’t open Joystick 0\n”);

   int SDL_JoystickEventState(true);

   while(1) {
                   SDL_JoystickUpdate();
                   printf("%f",SDL_JoystickGetAxis(joy, 0));
   }

// Close if opened
if(SDL_JoystickOpened(0))
SDL_JoystickClose(joy);
}

   //Return success!
   return 0;

}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

  _____________________________________________________________________________ 

Envoyez avec Yahoo! Mail. Une boite mail plus intelligente http://mail.yahoo.fr

Hm It didn’t take zero args. Passed in a null for see but didn’t work.
julien CLEMENT <clementj2005 yahoo.fr> writes:

Actually it returns a Sint16 … but the implicit cast to float (%f) should
work isn’t it ?Anyway, better using %d for that purpose, to make sure …Just a
remark, if you say SDL_Init(SDL_INIT_JOYSTICK) then, if I’m
correct,SDL_InitSubSystem(SDL_INIT_JOYSTICK) is implicitely called, so you
don’t need it.However, you’d do:SDL_Init();? /* no args */SDL_InitSubSystem
(SDL_INIT_JOYSTICK);Correct me if I’m wrongJulien

----- Message d’origine ----De : Peter Mackay <mackay.pete+sdl
gmail.com>? : A list for developers using the SDL library. (includes SDL-
announce)
<sdl lists.libsdl.org>Envoy? le : Vendredi, 18 Juillet 2008, 15h59mn
03sObjet?: Re: [SDL] New user, question about joystick.Hmm one more thing. Does
SDL_JoystickAxis return a float? I thought itwas a Uint16 or something like
that.Pete2008/7/18 Mark <marktheguy ymail.com>:> Thanks guys> Made changes
you suggested but not working.> (It does find it)>> #include <stdlib.h>>
#include <SDL.h>>> void SDL_JoystickUpdate(void);> int main(int argc, char *argv
[])> {> SDL_Joystick *joy;> SDL_Init(SDL_INIT_JOYSTICK);> // Initialize the
joystick subsystem> SDL_InitSubSystem(SDL_INIT_JOYSTICK);>> // Check for
joystick> if(SDL_NumJoysticks()>0){>? // Open joystick>?
joy=SDL_JoystickOpen(0);>>? if(joy)>? {>? ? printf(“Opened Joystick 0
\n”);>? ? printf(“Name: %s\n”, SDL_JoystickName(0));>? ? printf(“Number of
Axes: %d\n”, SDL_JoystickNumAxes(joy));>? ? printf(“Number of Buttons: %d\n”,
SDL_JoystickNumButtons(joy));>? ? printf(“Number of Balls: %d\n”,
SDL_JoystickNumBalls(joy));>? }>? else>? ? printf(“Couldn’t open Joystick 0
\n”);>>? ? ? ? int SDL_JoystickEventState(true);>>? ? ? ? while(1)
{>? ? ? ? ? ? ? ? ? ? ? ? SDL_JoystickUpdate();>? ? ? ? ? ? ? ? ? ? ? ? printf
("%f",SDL_JoystickGetAxis(joy, 0));>? ? ? ? }>? // Close if
opened>? if(SDL_JoystickOpened(0))>? ? SDL_JoystickClose(joy);>? ? ? ? }

? ? ? ? //Return success!>? ? ? ? return 0;> }>>
_______________________________________________> SDL mailing list> SDL
lists.libsdl.org> http://lists.libsdl.org/listinfo.cgi/sdl-
libsdl.org>_______________________________________________SDL mailing listSDL
lists.libsdl.orghttp://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org>

Envoy? avec Yahoo! Mail.Une boite mail plus intelligente.


SDL mailing list
SDL lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Actually it returns a Sint16 … but the implicit cast to float (%f) should
work isn’t it ?

I think it means that printf will just misinterpret the Sint16’s
representation in memory as a float, giving strange values.

Anyway, better using %d for that purpose, to make sure …

That sounds like a good thing to do. Mark, if you do that, you should
probably cast the result of SDL_JoystickAxis to an int just so the
types match up exactly.

Peter

Hello !

SDL_Init(SDL_INIT_JOYSTICK);
// Initialize the joystick subsystem
SDL_InitSubSystem(SDL_INIT_JOYSTICK);

This is wrong, with SDL_Init (SDL_INIT_JOYSTICK)
the Joystick Subsystem is already intialized.
At best the next SDL_InitSubSystem (SDL_INIT_JOYSTICK) might
do nothing or it can cause problems, so just delete that line.

CU

Actually it returns a Sint16 … but the implicit cast to float (%f) should
work isn’t it ?

Nope, because it’s not a cast, not even indirect – printf blindly trusts you and interprets the argument bits as a float.

If you were to leave the %f, you could do this:

printf("%f", ((float) arg));

Ettore