Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: SDL control Woes

                  
   
  1. #1
    DCEmu Regular
    Join Date
    Apr 2004
    Location
    Posts
    351
    Rep Power
    75

    Default SDL control Woes

    |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
    Code:
    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

    Code:
      /* 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

    Code:
    /**************************************
    ****  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

  2. #2
    DCEmu Regular
    Join Date
    Apr 2004
    Location
    Posts
    351
    Rep Power
    75

    Default Re: SDL control Woes

    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.

  3. #3
    DCEmu Coder GPF's Avatar
    Join Date
    Apr 2004
    Location
    Texas
    Age
    52
    Posts
    796
    Rep Power
    78

    Default Re: SDL control Woes

    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

  4. #4
    DCEmu Regular
    Join Date
    Apr 2004
    Location
    Posts
    351
    Rep Power
    75

    Default Re: SDL control Woes

    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.

  5. #5
    DCEmu Coder GPF's Avatar
    Join Date
    Apr 2004
    Location
    Texas
    Age
    52
    Posts
    796
    Rep Power
    78

    Default Re: SDL control Woes

    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

  6. #6
    DCEmu Regular
    Join Date
    Apr 2004
    Location
    Posts
    351
    Rep Power
    75

    Default Re: SDL control Woes


    Troy i did that and the controls started working just from added the printf. Hmmmmmm confused ???

  7. #7
    DCEmu Regular
    Join Date
    Apr 2004
    Location
    Posts
    351
    Rep Power
    75

    Default Re: SDL control Woes

    2nd player dpad worked fine the buttons are acting as player 1 buttons. I dont get it.

  8. #8
    DCEmu Coder GPF's Avatar
    Join Date
    Apr 2004
    Location
    Texas
    Age
    52
    Posts
    796
    Rep Power
    78

    Default Re: SDL control Woes

    I saw foster post, so its a bug in the PC version too

    Troy

  9. #9

    Default Re: SDL control Woes

    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!

  10. #10
    Dream Coder
    Join Date
    Apr 2004
    Location
    Miami, FL
    Age
    37
    Posts
    4,675
    Rep Power
    50

    Default Re: SDL control Woes

    hehe that is exactly what it was but it was solved 2 weeks ago. good job on the keen observation though.
    If anyone is looking to buy, sell, trade games and support a developer directly at the same time, consider joining Goozex. Enjoy!

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •