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;

}

Hello,

One thing that’s missing - you need to call SDL_JoystickUpdate() once each loop.

Hope this helps,
Pete

2008/7/18 Mark :> 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

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;

}

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

Bang on target

%d was the thing

Thanks all for helping sorting out my code!

might i suggest you fix your indenting and curly bracing ? this looks
copied and pasted from 2 different sources, very difficult to read.On Fri, 18 Jul 2008, Mark wrote:

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