PDA

View Full Version : SDL control Woes



Ian_micheal
May 13th, 2004, 16:22
|Ive posted this at dcemu but not every one vists there.



Im looking at the SDL Dc port for why perfecly good control code in neogeo cd works fine but on dc both joysticks are acting as one.

From SDL_sysjoystick.c


const int sdl_buttons[] = {
CONT_A,
CONT_B,
CONT_X,
CONT_Y,
CONT_START,
CONT_C,
CONT_D,
CONT_Z,
};

Also what is CONT_D, CONT_Z just looking at it seems like the triggers but i thought they were axis 3 and 4. Here it makes them look like buttons

Example


/* Player 1 */
keyup[SDLK_UP] =P1UP;
keyup[SDLK_DOWN] =P1DOWN;
keyup[SDLK_LEFT] =P1LEFT;
keyup[SDLK_RIGHT] =P1RIGHT;
keyup[SDLK_LCTRL] =P1A; joyup[0][0]=P1A;
keyup[SDLK_LALT] =P1B; joyup[0][1]=P1B;
keyup[SDLK_SPACE] =P1C; joyup[0][2]=P1C;
keyup[SDLK_LSHIFT]=P1D; joyup[0][3]=P1D;
keyup[SDLK_1] =P1START; joyup[0][4]=P1START;
keyup[SDLK_5] =P1SEL; joyup[0][5]=P1SEL;

Dc has 5 buttons. Not counting the triggers. The numbers 0 to 5 Number 5 what does that refer to? It does not work. Can i combine keys Like number 4+3, With this above code ive try with out luck, i thought i had fixed it and broke the start button so thats no good get select remove start. Now the Start button = 4 in sdl dc. Does not seem to be able to be done well in SDL combing buttons using the above code as a example prolly lost the plot.

Using the above code how could i combine 2 keys so i can use the sel, Im at a lost because ive tryed a lot of ways with out luck.

Also 0 is default joystick so player 1 =0 player 2 is =1. Why looking at the code below are both joysticks responding as player 1 even thou it's detecting 2 joysticks here it is in full


/**************************************
**** INPUT.C - Input devices ****
**************************************/

/*-- Include Files
---------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include "../neocd.h"


/* Joystick definitions */
#define NUMJOYSTICKS 2
/* axes */
#define NUMAXES 2
#define AXISMIN 0
#define AXISMAX 256
#define AXISCENTRE AXISMAX /2
#define AXISTHRESHOLD AXISCENTRE / 20 /* 10% joystick threshold */
/* buttons */
#define NUMJOYBUTTONS 8


/*--------------------------------------------------------------------------*/
#define P1UP 0x00000001
#define P1DOWN 0x00000002
#define P1LEFT 0x00000004
#define P1RIGHT 0x00000008
#define P1A 0x00000010
#define P1B 0x00000020
#define P1C 0x00000040
#define P1D 0x00000080

#define P2UP 0x00000100
#define P2DOWN 0x00000200
#define P2LEFT 0x00000400
#define P2RIGHT 0x00000800
#define P2A 0x00001000
#define P2B 0x00002000
#define P2C 0x00004000
#define P2D 0x00008000

#define P1START 0x00010000
#define P1SEL 0x00020000
#define P2START 0x00040000
#define P2SEL 0x00080000

#define SPECIAL 0x01000000


/*--------------------------------------------------------------------------*/
Uint32 keys =~0;
Uint32 keyup [SDLK_LAST];
Uint32 keydown[SDLK_LAST];

SDL_Joystick *joystick[NUMJOYSTICKS];
Uint32 joydown[NUMJOYSTICKS][NUMJOYBUTTONS];
Uint32 joyup [NUMJOYSTICKS][NUMJOYBUTTONS];

Uint32 joymask[NUMJOYSTICKS][NUMAXES][AXISMAX];
Uint32 joyset [NUMJOYSTICKS][NUMAXES][AXISMAX];

Uint32 joyhat[NUMJOYSTICKS][256];

Uint32 joyhatcentre[NUMJOYSTICKS];


/*--------------------------------------------------------------------------*/
void input_init(void) {
int i;

SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);

/* Player 1 */
keyup[SDLK_UP] =P1UP;
keyup[SDLK_DOWN] =P1DOWN;
keyup[SDLK_LEFT] =P1LEFT;
keyup[SDLK_RIGHT] =P1RIGHT;
keyup[SDLK_LCTRL] =P1A; joyup[0][0]=P1A;
keyup[SDLK_LALT] =P1B; joyup[0][1]=P1B;
keyup[SDLK_SPACE] =P1C; joyup[0][2]=P1C;
keyup[SDLK_LSHIFT]=P1D; joyup[0][3]=P1D;
keyup[SDLK_1] =P1START; joyup[0][4]=P1START;
keyup[SDLK_5] =P1SEL; joyup[0][4]=P1SEL;

/* Player 2 */
keyup[SDLK_r] =P2UP;
keyup[SDLK_f] =P2DOWN;
keyup[SDLK_d] =P2LEFT;
keyup[SDLK_g] =P2RIGHT;
keyup[SDLK_a] =P2A; joyup[1][0]=P2A;
keyup[SDLK_s] =P2B; joyup[1][1]=P2B;
keyup[SDLK_q] =P2C; joyup[1][2]=P2C;
keyup[SDLK_w] =P2D; joyup[1][3]=P2D;
keyup[SDLK_2] =P2START; joyup[1][4]=P2START;
keyup[SDLK_6] =P2SEL; joyup[1][5]=P2SEL;

/* Special */
keyup[SDLK_F1] =SPECIAL;
keyup[SDLK_F2] =SPECIAL;
keyup[SDLK_F3] =SPECIAL;
keyup[SDLK_F4] =SPECIAL;
keyup[SDLK_F12] =SPECIAL;
keyup[SDLK_ESCAPE]=SPECIAL;

/* set key down mask */
for(i=0;i<SDLK_LAST;i++) {
keydown[i]=~keyup[i];
}

/* set joy button down mask */
for(i=0; i<NUMJOYBUTTONS; i++) {
joydown[0][i]=~joyup[0][i];
joydown[1][i]=~joyup[0][i];
}


/* configure joystick hat */
/* (Dreamcast D-Pad) */
joyhatcentre[0]=P1UP|P1DOWN|P1LEFT|P1RIGHT;
joyhatcentre[1]=P2UP|P2DOWN|P2LEFT|P2RIGHT;

joyhat[0][SDL_HAT_CENTERED]=~0;
joyhat[1][SDL_HAT_CENTERED]=~0;

joyhat[0][SDL_HAT_UP]=~P1UP;
joyhat[1][SDL_HAT_UP]=~P2UP;
joyhat[0][SDL_HAT_DOWN]=~P1DOWN;
joyhat[1][SDL_HAT_DOWN]=~P2DOWN;
joyhat[0][SDL_HAT_LEFT]=~P1LEFT;
joyhat[1][SDL_HAT_LEFT]=~P2LEFT;
joyhat[0][SDL_HAT_RIGHT]=~P1RIGHT;
joyhat[1][SDL_HAT_RIGHT]=~P2RIGHT;

joyhat[0][SDL_HAT_RIGHTUP]=~(P1RIGHT|P1UP);
joyhat[1][SDL_HAT_RIGHTUP]=~(P2RIGHT|P2UP);
joyhat[0][SDL_HAT_RIGHTDOWN]=~(P1RIGHT|P1DOWN);
joyhat[1][SDL_HAT_RIGHTDOWN]=~(P2RIGHT|P2DOWN);
joyhat[0][SDL_HAT_LEFTUP]=~(P1LEFT|P1UP);
joyhat[1][SDL_HAT_LEFTUP]=~(P2LEFT|P2UP);
joyhat[0][SDL_HAT_LEFTDOWN]=~(P1LEFT|P1DOWN);
joyhat[1][SDL_HAT_LEFTDOWN]=~(P2LEFT|P2DOWN);



/* configure joystick axes */
/* left and up */
for(i=AXISMIN; i<AXISCENTRE-AXISTHRESHOLD; i++) {
joymask[0][0][i]=~P1LEFT;
joymask[1][0][i]=~P2LEFT;
joymask[0][1][i]=~P1UP;
joymask[1][1][i]=~P2UP;
joyset[0][0][i]=P1RIGHT;
joyset[1][0][i]=P2RIGHT;
joyset[0][1][i]=P1DOWN;
joyset[1][1][i]=P2DOWN;
}

/* centre */
for(i=AXISCENTRE-AXISTHRESHOLD; i<AXISCENTRE+AXISTHRESHOLD; i++) {
joymask[0][0][i]=~0;
joymask[1][0][i]=~0;
joymask[0][1][i]=~0;
joymask[1][1][i]=~0;
joyset[0][0][i]=P1RIGHT|P1LEFT;
joyset[1][0][i]=P2RIGHT|P2LEFT;
joyset[0][1][i]=P1DOWN|P1UP;
joyset[1][1][i]=P2DOWN|P2UP;
}

/* right and down */
for(i=AXISCENTRE+AXISTHRESHOLD; i<AXISMAX; i++) {
joymask[0][0][i]=~P1RIGHT;
joymask[1][0][i]=~P2RIGHT;
joymask[0][1][i]=~P1DOWN;
joymask[1][1][i]=~P2DOWN;
joyset[0][0][i]=P1LEFT;
joyset[1][0][i]=P2LEFT;
joyset[0][1][i]=P1UP;
joyset[1][1][i]=P2UP;
}


/* open joysticks */
for(i=0; i<SDL_NumJoysticks() && i<NUMJOYSTICKS; i++) {
joystick[i]=SDL_JoystickOpen(i);
if(joystick[i]!=NULL) {
printf("\nOpened Joystick %d\n",i);
printf("Name: %s\n", SDL_JoystickName(i));
printf("Number of Axes: %d\n",
SDL_JoystickNumAxes(joystick[i]));
printf("Number of Buttons: %d\n",
SDL_JoystickNumButtons(joystick[i]));
}
}
}


void input_shutdown(void) {
int i;

/* Close joysticks */
for(i=0; i<NUMJOYSTICKS; i++) {
if(joystick[i]!=NULL) {
SDL_JoystickClose(joystick[i]);
joystick[i]=NULL;
}
}
}


INLINE void specialKey (SDLKey key) {
switch(key) {
case SDLK_F4: sound_toggle(); break;
case SDLK_ESCAPE: exit(0); break;
default:
break;
}
}


INLINE void keyDown (SDLKey key) {
if(keyup[key]&SPECIAL) {
specialKey(key);
} else {
keys &= keydown[key];
}
}


INLINE void keyUp (SDLKey key) {
keys |= keyup[key];
}


INLINE void joyDown (int which, int button) {
if (which<NUMJOYSTICKS && button<NUMJOYBUTTONS) {
keys &= joydown[which][button];
}
}


INLINE void joyUp (int which, int button) {
if (which<NUMJOYSTICKS && button<NUMJOYBUTTONS) {
keys |= joyup[which][button];
}
}


INLINE void joyMotion (int which, int axis, int value) {
value+=AXISCENTRE;
if (which<NUMJOYSTICKS && axis <NUMAXES && value<AXISMAX) {
keys &= joymask[which][axis][value];
keys |= joyset[which][axis][value];
}
}

INLINE void joyHat (int which, int value) {
if (which<NUMJOYSTICKS) {
/* centre joypad */
keys |= joyhatcentre[which];
/* apply direction */
keys &= joyhat[which][value];
}
}


void processEvents(void) {
SDL_Event event;

while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN: keyDown(event.key.keysym.sym); break;
case SDL_KEYUP: keyUp(event.key.keysym.sym); break;

case SDL_JOYBUTTONDOWN: joyDown(event.jbutton.which,
event.jbutton.button); break;
case SDL_JOYBUTTONUP: joyUp(event.jbutton.which,
event.jbutton.button); break;

case SDL_JOYAXISMOTION: joyMotion(event.jaxis.which,
event.jaxis.axis, event.jaxis.value); break;

case SDL_JOYHATMOTION: joyHat(event.jhat.which,
event.jhat.value); break;

case SDL_QUIT: exit(0); break;

default:
break;
}
}
}

/*--------------------------------------------------------------------------*/
unsigned char read_player1(void) {
return keys&0xff;
}

/*--------------------------------------------------------------------------*/
unsigned char read_player2(void) {
return (keys>>8)&0xff;
}

/*--------------------------------------------------------------------------*/
unsigned char read_pl12_startsel(void) {
return (keys>>16)&0x0f;
}

Hope you can help out on it We all want 2 player puzzle bubble :D

Ian_micheal
May 13th, 2004, 17:12
Ok i just made it so Select is the anlog stick held down that works well. only problem left is the 2player both controlers acting as 1 even throu there being detected as 2 joysticks.


Any way select problem fixed.


So i fixed it helps to post it out some times still the last problem left 2 controls actiong as one.

GPF
May 13th, 2004, 18:43
The other buttons in the define are for 3rd party controllers that have more buttons, I have a madcatz controller that has 6 buttons, plus the start button.

Troy

Ian_micheal
May 13th, 2004, 19:08
Ahh Thanks Still dont know why both joysticks are acting as one even thou there being detected right. I remember you had a problem in a game and found the problem but i cant find your post on it.

GPF
May 13th, 2004, 19:52
I ended up using different functions to get it to work. Someone had recommend trying the event.jaxis.which variable to determine which joystick was generating an event, but I used a different method.

What I recommend it adding a printf("%d",which); to the joyMotion function and see if the second joystick is generating a 1 or not.

Troy

Ian_micheal
May 14th, 2004, 14:23
Troy i did that and the controls started working just from added the printf. Hmmmmmm confused ???

Ian_micheal
May 14th, 2004, 14:47
2nd player dpad worked fine the buttons are acting as player 1 buttons. I dont get it.

GPF
May 14th, 2004, 16:32
I saw foster post, so its a bug in the PC version too :)

Troy

The_Fallen_One
June 1st, 2004, 18:46
Hey, I'm new, just starting in the DC programming scene, but I'm really interested. This is my first post. ;)

But anyways, to the point. I just took a quick look over the code, and noticed one small problem. I might just not be thinking straight at the moment, but I think I know what causes your problem.

In input_init, shouldn't:

/* set joy button down mask */
for(i=0; i<NUMJOYBUTTONS; i++) {
joydown[0][i]=~joyup[0][i];
joydown[1][i]=~joyup[0][i];
}

be:

/* set joy button down mask */
for(i=0; i<NUMJOYBUTTONS; i++) {
joydown[0][i]=~joyup[0][i];
joydown[1][i]=~joyup[1][i];
}


...?

That might fix your problems.

Hope it helps! :D

quzar
June 1st, 2004, 22:02
hehe that is exactly what it was but it was solved 2 weeks ago. good job on the keen observation though.

wraggster
June 1st, 2004, 22:07
And welcome to our great scene :)

we hope to hear more fro you ;)

The_Fallen_One
June 1st, 2004, 22:27
Yeah, I noticed about a minute after I posted that the topic was a few weeks old. Oops. ;)

Thanks. Hopefully I can contribute something useful to the scene sometime in the future. DC is and always will be my favourite console, and it still has great potential... It just has to be used. :) I'm just teaching myself some assembly (I'm already very proficient with C/C++) and then I'll be seeing what I can manage. Any suggestions for a newbie like me? I've already looked over most of the "DC Development Links" in the sticky at the top of this thread, but I didn't find too much of use besides how to set up Dev C++/Cygwin properly and some vague references to KOS (KallistiOS, correct?). I'm gonna check out KallistiOS on SourceForge right now, but any other tips would be greatly appreciated.

Thanks in advance! :)