Sdl Problem Reading Diagonal Input


Dave18

Member
Joined
Mar 16, 2003
Messages
352
Age
49
I'm using a standard SDL input polling routine to parse all events until the buffer is empty. Problem is that if a diagonal is pressed SDL does not appear to poll two events, one for each direction, but instead polls one event according to the diagram below.


1 0 7
2 + 6
3 4 5

I'm sure all other versions of SDL I've used, including the GP2X could read diagonals by generating two events (eg up/left would generate both an up and a left event rather than a single diagonal event).

I am trying to read the dpad using the following code but it doesn't handle transition between the four main directions and the diagonals very well. Anyone have any suggestions on the best way to do this on the Wiz?

Code:
#define MY_LEFT                     (1)
#define MY_RIGHT                     (2)
#define MY_UP                     (4)
#define MY_DOWN                     (8)


int get_key()
{
    int keydata=0;
    SDL_Event event;
	while( SDL_PollEvent( &event ) )
		{

		if (event.type==SDL_JOYBUTTONUP)
		{
			switch(event.jbutton.button)
			{
                        case GP2X_BUTTON_DOWNLEFT:
                            DOWNDOWN=0;
                            LEFTDOWN=0;
                            break;
                        case GP2X_BUTTON_DOWNRIGHT:
                            DOWNDOWN=0;
                            RIGHTDOWN=0;
                            break;
                        case GP2X_BUTTON_UPLEFT:
                            UPDOWN=0;
                            LEFTDOWN=0;
                            break;
                        case GP2X_BUTTON_UPRIGHT:
                            UPDOWN=0;
                            RIGHTDOWN=0;
                            break;
                        case GP2X_BUTTON_UP:
  			    UPDOWN=0;
			    break;
			case GP2X_BUTTON_LEFT:
	    		    LEFTDOWN=0;
			    break;
			case GP2X_BUTTON_RIGHT:
			    RIGHTDOWN=0;
   			    break;
                        case GP2X_BUTTON_DOWN:
 			    DOWNDOWN=0;
			    break;
		        default:
			    break;
                }
        }

        if (event.type== SDL_JOYBUTTONDOWN)
        {																	// GP2X buttons

            switch( event.jbutton.button )
            {
			case GP2X_BUTTON_UP:
 			    UPDOWN=1;
			    break;
			case GP2X_BUTTON_LEFT:
			    LEFTDOWN=1;
			    break;
			case GP2X_BUTTON_RIGHT:
			    RIGHTDOWN=1;
			    break;
                        case GP2X_BUTTON_DOWN:
			    DOWNDOWN=1;
			    break;
                        case GP2X_BUTTON_DOWNLEFT:
                            DOWNDOWN=1;
                            LEFTDOWN=1;
                            break;
                        case GP2X_BUTTON_DOWNRIGHT:
                            DOWNDOWN=1;
                            RIGHTDOWN=1;
                            break;
                        case GP2X_BUTTON_UPLEFT:
                            UPDOWN=1;
                            LEFTDOWN=1;
                            break;
                        case GP2X_BUTTON_UPRIGHT:
                            UPDOWN=1;
                            RIGHTDOWN=1;
                            break;
			default:
			    break;
            }
        }



			}

	if (UPDOWN) keydata|=MY_UP;
	if (LEFTDOWN) keydata|=MY_LEFT;
	if (RIGHTDOWN) keydata|=MY_RIGHT;
	if (DOWNDOWN) keydata|=MY_DOWN;

        return keydata;
}
 
The Wiz is like the GP2X and treats the diagonals as separate buttons. So, if you were to somehow hit the UPLEFT diagonal perfectly from a resting position it would generate one event, a JOYBUTTONDOWN event of that UPLEFT diagonal, not the combined directions UP+LEFT. If you want to support users that have a DPAD-modded GP2X, however, you must also account for their systems now generating that combined UP+LEFT in place of the UPLEFT it would normally generate.

That said, I always find it easier to turn off all event handling for joysticks in SDL entirely and instead use a series of calls to the function SDL_JoystickGetButton to read all of the buttons the GP2X/Wiz provide and update an internal array containing the state of all the buttons each time the game loops. You must disable joystick events using this function. Before you update your internal button array, make a call to this function

BTW, you can find a ton of source code on the file archive for the GP2X and the Wiz (code for either should apply to your problems, as coding SDL for one is just like coding SDL for the other except for a few tiny details. That is how I solve a lot of my problems is looking at how others did things.
 
Back
Top