Sdl Joystick Problems


namco

Member
Joined
Mar 22, 2006
Messages
410
Age
41
Location
Manchester, UK
Website
www.stupendous-stuff.com
I'm trying to port Kill 'em and I'm having joystick problems.

When I change this code:

CODE

if (key_oncepressed[SDLK_UP] == PRESSED) {
if (menu_cursor > 0) menu_cursor --;
else menu_cursor = menuoption_count - 1;
RenderMenu();
}

if (key_oncepressed[SDLK_DOWN] == PRESSED) {
if (menu_cursor < menuoption_count - 1) menu_cursor ++;
else menu_cursor = 0;
RenderMenu();
}



to:

CODE

if (key_oncepressed[SDLK_UP] == PRESSED || GP2X_BUTTON_UP) {
if (menu_cursor > 0) menu_cursor --;
else menu_cursor = menuoption_count - 1;
RenderMenu();
}

if (key_oncepressed[SDLK_DOWN] == PRESSED || GP2X_BUTTON_DOWN) {
if (menu_cursor < menuoption_count - 1) menu_cursor ++;
else menu_cursor = 0;
RenderMenu();
}



The menu cursor goes "mad" and just goes up and down of its own accord, yet when I change it to this:

CODE

if (key_oncepressed[SDLK_UP] == PRESSED || jbut.type == SDL_JOYBUTTONDOWN && GP2X_BUTTON_UP) {
if (menu_cursor > 0) menu_cursor --;
else menu_cursor = menuoption_count - 1;
RenderMenu();
}
if (key_oncepressed[SDLK_DOWN] == PRESSED || jbut.type == SDL_JOYBUTTONDOWN && GP2X_BUTTON_DOWN) {
if (menu_cursor < menuoption_count - 1) menu_cursor ++;
else menu_cursor = 0;
RenderMenu();
}



the menu cursor does nothing when I move the joystick up and down.

Oh and jbut is defined as follows:

SDL_JoyButtonEvent jbut;

within the procedure.
 
if (key_oncepressed[SDLK_UP] == PRESSED || GP2X_BUTTON_UP)

That always is true because GP2X_BUTTON_UP is just a number (!= 0). I guess you will have to set key_oncepressed[SDLK_UP] as PRESSED when GP2X_BUTTON_UP is pressed in a joystick event.
 
Try this:

CODE
SDL_Joystick* joystick = SDL_JoystickOpen(0);

...

if (key_oncepressed[SDLK_UP] == PRESSED || SDL_JoystickGetButton(joystick, GP2X_BUTTON_UP)) {

...


Of course, you can assign SDL_JoystickGetButton to a nice series of int variables for all buttons.
 
Back
Top