Results 1 to 5 of 5

Thread: [SOLVED] Weird SDL_PollEvent behaviour with SDL_JOYAXISMOTION

                  
   
  1. #1

    Default [SOLVED] Weird SDL_PollEvent behaviour with SDL_JOYAXISMOTION

    Hi everyone,

    From time to time, I'm working on my Frontier Elite 2 PSP port, and this is one of those times.
    As the game now works faster than before, I had to add a R-Trigger shortcut in order to slowdown the mouse cursor for better aim during space dogfight.

    I handle the PSP keys with SDL_PollEvent, and this is where I have a little problem that maybe somebody knows about: I want the cursor moving slowlier when I press the Right trigger. My code is working, but the Rtrigger boolean is only taken in account when I change direction with the joystick.

    I wrote down a little test program to illustrate my problem for anyone who is willing to help me moving the cursor the way I want.

    Here it is:
    Code:
    #include <pspkernel.h> 
    #include <pspdebug.h> 
     
    #include <SDL/SDL.h> 
    #include <SDL/SDL_image.h> 
     
    PSP_MODULE_INFO("SDL_event", 0, 1, 0); 
    PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU); 
     
    int SetupCallbacks(void); 
    int exit_callback(int arg1, int arg2, void *common); 
    int CallbackThread(SceSize args, void *argp); 
     
    int main() 
    { 
       SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); 
     
       SDL_Joystick *joystick = NULL; 
       SDL_Surface *screen = NULL; 
       SDL_Surface *cursor = IMG_Load("image.png"); 
       SDL_Event event; 
           
       int continuer = 1; 
       int Rtrigger = 0; 
           
       SDL_JoystickEventState(SDL_ENABLE); 
       joystick = SDL_JoystickOpen(0); 
            
       screen = SDL_SetVideoMode(480, 272, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); 
     
       SDL_Rect position; 
       position.x = 25; 
       position.y = 84; 
           
       int motion_x = 0; 
       int motion_y = 0; 
           
       pspDebugScreenInit(); 
       SetupCallbacks(); 
            
       SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); 
       SDL_Flip(screen); 
            
       while(continuer) 
       {               
          char cursorSpeedDivider = 2; 
          if (Rtrigger) cursorSpeedDivider = 6; 
     
          while (SDL_PollEvent (&event)) { 
             switch (event.type) { 
                case SDL_JOYBUTTONDOWN:    
                   if (event.jbutton.button == 5) Rtrigger = 1; 
                   if (event.jbutton.button == 11) continuer = 0; 
                break; 
                case SDL_JOYBUTTONUP: 
                   if (event.jbutton.button == 5) Rtrigger = 0; 
                break; 
                case SDL_JOYAXISMOTION: 
                   switch (event.jaxis.axis) { 
                      case 0: 
                         motion_x = abs(event.jaxis.value) * event.jaxis.value / (100000000 * cursorSpeedDivider); 
                      break; 
                      case 1: 
                         motion_y = abs(event.jaxis.value) * event.jaxis.value / (100000000 * cursorSpeedDivider); 
                      break; 
                   } 
                break; 
             } 
          } 
              
          position.x += motion_x; 
          if (position.x < 1) position.x = 1; 
          if (position.x > 470) position.x = 470; 
          position.y += motion_y; 
          if (position.y < 2) position.y = 2; 
          if (position.y > 260) position.y = 260; 
              
          SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); 
          SDL_BlitSurface(cursor, NULL, screen, &position); 
          SDL_Flip(screen); 
              
          SDL_Delay(10); 
       } 
            
       SDL_JoystickClose(joystick); 
       SDL_Quit(); 
       sceKernelExitGame(); 
       return 0; 
    } 
     
    int exit_callback(int arg1, int arg2, void *common) 
    { 
       sceKernelExitGame(); 
       return 0; 
    } 
    int CallbackThread(SceSize args, void *argp) 
    { 
       int cbid; 
       cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL); 
       sceKernelRegisterExitCallback(cbid); 
       sceKernelSleepThreadCB(); 
       return 0; 
    } 
    int SetupCallbacks(void) 
    { 
       int thid = 0; 
       thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0); 
       if(thid >= 0)   sceKernelStartThread(thid, 0, 0); 
       return thid; 
    }
    And the makefile :
    Code:
    TARGET = SDL_PollEvent 
    PSPSDK = $(shell psp-config --pspsdk-path) 
    PSPBIN = $(PSPSDK)/../bin 
    SDL_CONFIG = $(PSPBIN)/sdl-config 
     
    OBJS = main.o 
     
    DEFAULT_CFLAGS = -I/usr/local/pspdev/psp/include/SDL 
    CFLAGS = $(DEFAULT_CFLAGS) -G4 -Wall -O3 
    CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti 
    ASFLAGS = $(CFLAGS) 
     
    PSPSDK = $(shell psp-config --pspsdk-path) 
    PSPBIN = $(PSPSDK)/../bin 
    SDL_CONFIG = $(PSPBIN)/sdl-config 
     
    LIBDIR = 
    LDFLAGS = 
    STDLIBS= -lSDLmain -lSDL_image -lSDL -lGL -lpng -ljpeg -lz -lm\ 
    -lstdc++ -lpspgu -lpspvfpu -lpsprtc -lpsphprm -lpspaudio -lpspirkeyb -lpsppower 
    LIBS=$(STDLIBS)$(YOURLIBS) 
     
    EXTRA_TARGETS = EBOOT.PBP 
    PSP_EBOOT_TITLE = SDL_PollEvent 
     
    BUILD_PRX = 1 
    PSP_LARGE_MEMORY = 1 
    PSP_FW_VERSION = 371 
    include $(PSPSDK)/lib/build.mak
    As you see, it is quite simple. Not optimised at all, but still...
    You also need some whatever little image.png you want to move around inside your folder.

    As explained before, with this program you can move around the "cursor" with the joystick, and slow it down by pressing the R-Trigger. But the problem is that the cursor switches speed only when you change joystick direction. Otherwise, the trigger isn't taken in account...

    Do somebody knows how to get rid of that problem?
    Thank you very much!
    Last edited by Atien; April 25th, 2011 at 22:35. Reason: problem solved, changed post title

  2. #2
    DCEmu Coder
    Join Date
    Aug 2006
    Posts
    571
    Rep Power
    67

    Default

    It seems to me your problem is you only use the divisor when the stick changes direction. Instead, use it where the motion is added, like this:

    Code:
       while(continuer) 
       {               
          char cursorSpeedDivider = 2; 
          if (Rtrigger) cursorSpeedDivider = 6; 
     
          while (SDL_PollEvent (&event)) { 
             switch (event.type) { 
                case SDL_JOYBUTTONDOWN:    
                   if (event.jbutton.button == 5) Rtrigger = 1; 
                   if (event.jbutton.button == 11) continuer = 0; 
                break; 
                case SDL_JOYBUTTONUP: 
                   if (event.jbutton.button == 5) Rtrigger = 0; 
                break; 
                case SDL_JOYAXISMOTION: 
                   switch (event.jaxis.axis) { 
                      case 0: 
                         motion_x = abs(event.jaxis.value) * event.jaxis.value / (100000000); 
                      break; 
                      case 1: 
                         motion_y = abs(event.jaxis.value) * event.jaxis.value / (100000000); 
                      break; 
                   } 
                break; 
             } 
          } 
              
          position.x += motion_x / cursorSpeedDivider;
          if (position.x < 1) position.x = 1; 
          if (position.x > 470) position.x = 470; 
          position.y += motion_y / cursorSpeedDivider;
          if (position.y < 2) position.y = 2; 
          if (position.y > 260) position.y = 260; 
              
          SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); 
          SDL_BlitSurface(cursor, NULL, screen, &position); 
          SDL_Flip(screen); 
              
          SDL_Delay(10); 
       }
    EDIT: Faster code would only do the divide when it needed to be, like this:

    Code:
       while(continuer) 
       {
          while (SDL_PollEvent (&event)) { 
             switch (event.type) { 
                case SDL_JOYBUTTONDOWN:    
                   if (event.jbutton.button == 5) {
                       motion_x = motion_xx / 6; 
                       motion_y = motion_yy / 6;
                       Rtrigger = 1;
                   }
                   if (event.jbutton.button == 11) continuer = 0; 
                break; 
                case SDL_JOYBUTTONUP: 
                   if (event.jbutton.button == 5) {
                       motion_x = motion_xx / 2; 
                       motion_y = motion_yy / 2;
                       Rtrigger = 0;
                   }
                break; 
                case SDL_JOYAXISMOTION: 
                   switch (event.jaxis.axis) { 
                      case 0: 
                         motion_xx = abs(event.jaxis.value) * event.jaxis.value / (100000000); 
                         motion_x = motion_xx / (Rtrigger ? 6 : 2);
                      break; 
                      case 1: 
                         motion_yy = abs(event.jaxis.value) * event.jaxis.value / (100000000); 
                         motion_y = motion_yy / (Rtrigger ? 6 : 2);
                      break; 
                   } 
                break; 
             } 
          } 
              
          position.x += motion_x;
          if (position.x < 1) position.x = 1; 
          if (position.x > 470) position.x = 470; 
          position.y += motion_y;
          if (position.y < 2) position.y = 2; 
          if (position.y > 260) position.y = 260; 
              
          SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); 
          SDL_BlitSurface(cursor, NULL, screen, &position); 
          SDL_Flip(screen); 
              
          SDL_Delay(10); 
       }
    Last edited by JLF65; April 21st, 2011 at 02:09. Reason: more code

  3. #3

    Default

    Thank you JLF65, I just saw your response and it's working like a charm!
    I'm adding this to Frontier right away
    Last edited by Atien; April 26th, 2011 at 11:47.

  4. #4
    DCEmu Coder
    Join Date
    Aug 2006
    Posts
    571
    Rep Power
    67

    Default

    No problem. Glad to help; it's always good to see updates to ports.

  5. #5

    Default

    tnx

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Study links violent behaviour to individual personalities
    By wraggster in forum DCEmu General Gaming and Current Affairs News forum
    Replies: 5
    Last Post: June 15th, 2010, 02:25
  2. Trion: Telara MMO will feature "emergent behaviour"
    By wraggster in forum PC News Forum
    Replies: 0
    Last Post: July 7th, 2009, 20:40
  3. Unusual Control Behaviour
    By ojdon in forum DCEmu Gaming & General Discussion Forum
    Replies: 5
    Last Post: November 27th, 2008, 13:43
  4. Weird Dosbox behaviour (Burntime)
    By KBB in forum DCEmu Gaming & General Discussion Forum
    Replies: 0
    Last Post: November 5th, 2008, 05:29
  5. Weird, really weird brick.
    By penileartery in forum DCEmu Gaming & General Discussion Forum
    Replies: 25
    Last Post: January 6th, 2007, 07:31

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
  •