forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[SOLVED]Weird SDL_PollEvent behaviour with SDL_JOYAXISMOTION

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Ats



Joined: 05 Dec 2005
Posts: 46
Location: Biarritz - France

PostPosted: Thu Feb 17, 2011 11:01 pm    Post subject: [SOLVED]Weird SDL_PollEvent behaviour with SDL_JOYAXISMOTION Reply with quote

Hi, today I'm working with sdl and I am experiencing some strange results.
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. Can somebody help me?

Here's the stripped down bit of code that is interesting :

Code:
SDL_Event event;
char cursorSpeedDivider = 2;
if (Rtrigger) cursorSpeedDivider = 6;

while (SDL_PollEvent (&event)) {
    switch (event.type) {
        case SDL_JOYBUTTONDOWN:  Input_ButtonPress (event.jbutton.button);    break;
        case SDL_JOYBUTTONUP:    Input_ButtonRelease (event.jbutton.button);  break;
        case SDL_JOYAXISMOTION:
            if (event.jaxis.axis == 0) motion_x = event.jaxis.value / cursorSpeedDivider;
            if (event.jaxix.axis == 1) motion_y = event.jaxis.value / cursorSpeedDivider;
        break;
        }
    }
}


And the stripped press and release functions :

Code:
void Input_ButtonPress (unsigned char button) {
   switch (button) {
      case 5:   // R trigger
         Rtrigger = TRUE;
         break;
   }
   return;
}


Code:
void Input_ButtonRelease (unsigned char button) {
   switch (button) {
      case 5:   // R trigger
         Rtrigger = FALSE;
         break;
   }
   return;
}


Rtrigger is really passing to TRUE when you press the button, and FALSE when you release it, no problem, but it isn't taken in account in the case SDL_JOYAXISMOTION...
I already tried a lot of things like...
- checking the boolean within the case SDL_JOYAXISMOTION : it's the same.
- divide motion_x and y outside the while (SDL_PollEvent (&event)) : the cursor only moves once and stops untill you change direction.
- Force Rtrigger to TRUE before checking if the button is pressed : the cursor doesn't slow down anymore when the trigger is pressed.

Does somebody know how to handle that? Do I have to pass the boolean in the SDL_PollEvent, with SDL_PushEvent maybe?

Thank you!


Last edited by Ats on Tue Apr 26, 2011 6:40 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Ats



Joined: 05 Dec 2005
Posts: 46
Location: Biarritz - France

PostPosted: Wed Apr 06, 2011 5:58 am    Post subject: Reply with quote

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 image.png you want in your folder.

With this program you can move around the "cursor" with the joystick, and slow it down by pressing the R-Trigger.
My problem is still the same: the cursor switches speed only when I change joystick direction. Otherwise, the trigger isn't taken in account.
Do somebody knows how to handle that with SDL_PollEvent?
Thank you very much!
Back to top
View user's profile Send private message Visit poster's website
Ats



Joined: 05 Dec 2005
Posts: 46
Location: Biarritz - France

PostPosted: Tue Apr 26, 2011 6:39 am    Post subject: Reply with quote

JLF65 from DCEmu helped me solving this problem. I had to use the divisor where the motion is added instead of using it when the stick changes direction:

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);
   }
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group