Beispiel #1
0
VOID
PAL_ProcessEvent(
   VOID
)
/*++
  Purpose:

    Process all events.

  Parameters:

    None.

  Return value:

    None.

--*/
{
#if PAL_HAS_JOYSTICKS
   g_InputState.joystickNeedUpdate = FALSE;
#endif
   while (PAL_PollEvent(NULL));

   PAL_UpdateKeyboardState();
#if PAL_HAS_JOYSTICKS
   if(g_InputState.joystickNeedUpdate)
      PAL_UpdateJoyStickState();
#endif
#if PAL_HAS_TOUCH
   PAL_TouchRepeatCheck();
#endif
}
Beispiel #2
0
void
UTIL_Delay(
   unsigned int ms
)
{
   unsigned int t = SDL_GetTicks() + ms;
   while (PAL_PollEvent(NULL));
   while (SDL_GetTicks() < t)
   {
		SDL_Delay(1);
		while (PAL_PollEvent(NULL));
   }

#ifdef PAL_HAS_NATIVEMIDI
   MIDI_CheckLoop();
#endif
}
Beispiel #3
0
/**
 * Handle joystick events.
 *
 * @param[in] lpEvent - pointer to the event.
 * @return None
 */
static VOID PAL_JoystickEventFilter(const SDL_Event *lpEvent)
{
#ifdef PAL_HAS_JOYSTICKS
    switch (lpEvent->type) {
#if defined (GEKKO)
        case SDL_JOYHATMOTION:
           switch (lpEvent->jhat.value)
           {
           case SDL_HAT_LEFT:
             g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
             g_InputState.dir = kDirWest;
             g_InputState.dwKeyPress = kKeyLeft;
             break;

           case SDL_HAT_RIGHT:
             g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
             g_InputState.dir = kDirEast;
             g_InputState.dwKeyPress = kKeyRight;
             break;

           case SDL_HAT_UP:
             g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
             g_InputState.dir = kDirNorth;
             g_InputState.dwKeyPress = kKeyUp;
             break;

           case SDL_HAT_DOWN:
             g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
             g_InputState.dir = kDirSouth;
             g_InputState.dwKeyPress = kKeyDown;
             break;
           }
           break;
#else
        case SDL_JOYAXISMOTION:
            //
            // Moved an axis on joystick
            //
            switch (lpEvent->jaxis.axis) {
                case 0:
                    //
                    // X axis
                    //
#if defined(GPH)
                    if (lpEvent->jaxis.value > MAX_DEADZONE) {
                       g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
                       g_InputState.dir = kDirEast;
                       g_InputState.dwKeyPress = kKeyRight;
                    } else if (lpEvent->jaxis.value < MIN_DEADZONE) {
                       g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
                       g_InputState.dir = kDirWest;
                       g_InputState.dwKeyPress = kKeyLeft;
                    } else {
                       g_InputState.dir = kDirUnknown;
                    }
#else
                    if (lpEvent->jaxis.value > 20000) {
                        if (g_InputState.dir != kDirEast) {
                            g_InputState.dwKeyPress |= kKeyRight;
                        }
                        g_InputState.prevdir = g_InputState.dir;
                        g_InputState.dir = kDirEast;
                    } else if (lpEvent->jaxis.value < -20000) {
                        if (g_InputState.dir != kDirWest) {
                            g_InputState.dwKeyPress |= kKeyLeft;
                        }
                        g_InputState.prevdir = g_InputState.dir;
                        g_InputState.dir = kDirWest;
                    } else {
                        if (g_InputState.prevdir != kDirEast &&
                                g_InputState.prevdir != kDirWest) {
                            g_InputState.dir = g_InputState.prevdir;
                        }
                        g_InputState.prevdir = kDirUnknown;
                    }
#endif
                    break;

                case 1:
                    //
                    // Y axis
                    //
#if defined(GPH)
                    if (lpEvent->jaxis.value > MAX_DEADZONE) {
                       g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
                       g_InputState.dir = kDirSouth;
                       g_InputState.dwKeyPress = kKeyDown;
                    } else if (lpEvent->jaxis.value < MIN_DEADZONE) {
                       g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
                       g_InputState.dir = kDirNorth;
                       g_InputState.dwKeyPress = kKeyUp;
                    } else {
                       g_InputState.dir = kDirUnknown;
                    }
#else
                    if (lpEvent->jaxis.value > 20000) {
                        if (g_InputState.dir != kDirSouth) {
                            g_InputState.dwKeyPress |= kKeyDown;
                        }
                        g_InputState.prevdir = g_InputState.dir;
                        g_InputState.dir = kDirSouth;
                    } else if (lpEvent->jaxis.value < -20000) {
                        if (g_InputState.dir != kDirNorth) {
                            g_InputState.dwKeyPress |= kKeyUp;
                        }
                        g_InputState.prevdir = g_InputState.dir;
                        g_InputState.dir = kDirNorth;
                    } else {
                        if (g_InputState.prevdir != kDirNorth &&
                                g_InputState.prevdir != kDirSouth) {
                            g_InputState.dir = g_InputState.prevdir;
                        }
                        g_InputState.prevdir = kDirUnknown;
                    }
#endif
                    break;
            }
            break;
#endif

        case SDL_JOYBUTTONDOWN:
            //
            // Pressed the joystick button
            //
#if defined(GPH)
            switch (lpEvent->jbutton.button)
            {
#if defined(GP2XWIZ)
            case 14:
#elif defined(CAANOO)
            case 3:
#endif
               g_InputState.dwKeyPress = kKeyMenu;
               break;

#if defined(GP2XWIZ)
            case 13:
#elif defined(CAANOO)
            case 2:
#endif
               g_InputState.dwKeyPress = kKeySearch;
               break;
#else
#if defined(GEKKO)
            switch (lpEvent->jbutton.button)
            {
            case 2:
               g_InputState.dwKeyPress |= kKeyMenu;
               break;

            case 3:
               g_InputState.dwKeyPress |= kKeySearch;
               break;
#else
            switch (lpEvent->jbutton.button & 1) {
                case 0:
                    g_InputState.dwKeyPress |= kKeyMenu;
                    break;

                case 1:
                    g_InputState.dwKeyPress |= kKeySearch;
                    break;
#endif
#endif
            }
            break;
    }
#endif
}

#ifdef PAL_HAS_TOUCH

#define  TOUCH_NONE     0
#define    TOUCH_UP      1
#define    TOUCH_DOWN      2
#define    TOUCH_LEFT      3
#define    TOUCH_RIGHT   4
#define    TOUCH_BUTTON1   5
#define    TOUCH_BUTTON2   6
#define  TOUCH_BUTTON3  7
#define  TOUCH_BUTTON4  8

static int
PAL_GetTouchArea(
   float X,
   float Y
)
{
   if (Y < 0.5)
   {
      //
      // Upper area
      //
      return TOUCH_NONE;
   }
   else if (X < 1.0 / 3)
   {
      if (Y - 0.5 < (1.0 / 6 - fabs(X - 1.0 / 3 / 2)) * (0.5 / (1.0 / 3)))
      {
         return TOUCH_UP;
      }
      else if (Y - 0.75 > fabs(X - 1.0 / 3 / 2) * (0.5 / (1.0 / 3)))
      {
         return TOUCH_DOWN;
      }
      else if (X < 1.0 / 3 / 2 && fabs(Y - 0.75) < 0.25 - X * (0.5 / (1.0 / 3)))
      {
         return TOUCH_LEFT;
      }
      else
      {
         return TOUCH_RIGHT;
      }
   }
   else if (X > 1.0 - 1.0 / 3)
   {
      if (X < 1.0 - (1.0 / 3 / 2))
      {
         if (Y < 0.75)
         {
            return TOUCH_BUTTON1;
         }
         else
         {
            return TOUCH_BUTTON3;
         }
      }
      else
      {
         if (Y < 0.75)
         {
            return TOUCH_BUTTON2;
         }
         else
         {
            return TOUCH_BUTTON4;
         }
      }
   }

   return TOUCH_NONE;
}

static VOID
PAL_SetTouchAction(
  int area
)
{
   switch (area)
   {
   case TOUCH_UP:
      g_InputState.dir = kDirNorth;
      g_InputState.dwKeyPress |= kKeyUp;
      break;

   case TOUCH_DOWN:
      g_InputState.dir = kDirSouth;
      g_InputState.dwKeyPress |= kKeyDown;
      break;

   case TOUCH_LEFT:
      g_InputState.dir = kDirWest;
      g_InputState.dwKeyPress |= kKeyLeft;
      break;

   case TOUCH_RIGHT:
      g_InputState.dir = kDirEast;
      g_InputState.dwKeyPress |= kKeyRight;
      break;

   case TOUCH_BUTTON1:
      if (gpGlobals->fInBattle)
      {
         g_InputState.dwKeyPress |= kKeyRepeat;
      }
      else
      {
         g_InputState.dwKeyPress |= kKeyForce;
      }
      break;

   case TOUCH_BUTTON2:
      g_InputState.dwKeyPress |= kKeyMenu;
      break;

   case TOUCH_BUTTON3:
      g_InputState.dwKeyPress |= kKeyUseItem;
      break;

   case TOUCH_BUTTON4:
      g_InputState.dwKeyPress |= kKeySearch;
      break;
   }
}

static VOID
PAL_UnsetTouchAction(
  int area
)
{
   switch (area)
   {
   case TOUCH_UP:
   case TOUCH_DOWN:
   case TOUCH_LEFT:
   case TOUCH_RIGHT:
      g_InputState.dir = kDirUnknown;
      break;
   }
}
#endif

/**
 * Handle touch events.
 *
 * @param[in] lpEvent - pointer to the event.
 * @return None
 */
static VOID PAL_TouchEventFilter(const SDL_Event *lpEvent)
{
#ifdef PAL_HAS_TOUCH
    static SDL_TouchID finger1 = -1, finger2 = -1;
    static int prev_touch1 = TOUCH_NONE;
    static int prev_touch2 = TOUCH_NONE;

    switch (lpEvent->type)
    {
    case SDL_FINGERDOWN:
      if (finger1 == -1)
      {
         int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);

         finger1 = lpEvent->tfinger.fingerId;
         prev_touch1 = area;
         PAL_SetTouchAction(area);
      }
      else if (finger2 == -1)
      {
         int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);

         finger2 = lpEvent->tfinger.fingerId;
         prev_touch2 = area;
         PAL_SetTouchAction(area);
      }
      break;

    case SDL_FINGERUP:
      if (lpEvent->tfinger.fingerId == finger1)
      {
         PAL_UnsetTouchAction(prev_touch1);
         finger1 = -1;
         prev_touch1 = TOUCH_NONE;
      }
      else if (lpEvent->tfinger.fingerId == finger2)
      {
         PAL_UnsetTouchAction(prev_touch2);
         finger2 = -1;
         prev_touch2 = TOUCH_NONE;
      }
      break;

    case SDL_FINGERMOTION:
       if (lpEvent->tfinger.fingerId == finger1)
       {
          int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
          if (prev_touch1 != area && area != TOUCH_NONE)
          {
             PAL_UnsetTouchAction(prev_touch1);
             prev_touch1 = area;
             PAL_SetTouchAction(area);
          }
       }
       else if (lpEvent->tfinger.fingerId == finger2)
       {
          int area = PAL_GetTouchArea(lpEvent->tfinger.x, lpEvent->tfinger.y);
          if (prev_touch2 != area && area != TOUCH_NONE)
          {
             PAL_UnsetTouchAction(prev_touch2);
             prev_touch2 = area;
             PAL_SetTouchAction(area);
          }
       }
       break;
    }
#endif
}

/**
 * SDL event filter function. A filter to process all events.
 *
 * @param[in] lpEvent - pointer to the event.
 * @retval 1 = the event will be added to the internal queue.
 * @retval 0 = the event will be dropped from the queue.
 */
static int SDLCALL PAL_EventFilter(const SDL_Event *lpEvent)
{
    switch (lpEvent->type) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
        case SDL_WINDOWEVENT:
            if (lpEvent->window.event == SDL_WINDOWEVENT_RESIZED) {
                //
                // resized the window
                //
                VIDEO_Resize(lpEvent->window.data1, lpEvent->window.data2);
            }
            break;

#ifdef __IOS__
        case SDL_APP_WILLENTERBACKGROUND:
           g_bRenderPaused = TRUE;
           break;

        case SDL_APP_DIDENTERFOREGROUND:
           g_bRenderPaused = FALSE;
           VIDEO_UpdateScreen(NULL);
           break;
#endif

#else

        case SDL_VIDEORESIZE:
           //
           // resized the window
           //
           VIDEO_Resize(lpEvent->resize.w, lpEvent->resize.h);
           break;
#endif

        case SDL_QUIT:
            //
            // clicked on the close button of the window. Quit immediately.
            //
            PAL_Shutdown();
            exit(0);
    }

    PAL_KeyboardEventFilter(lpEvent);
    PAL_MouseEventFilter(lpEvent);
    PAL_JoystickEventFilter(lpEvent);
    PAL_TouchEventFilter(lpEvent);

    //
    // All events are handled here; don't put anything to the internal queue
    //
    return 0;
}

/**
 * Clear the record of pressed keys.

 */
VOID PAL_ClearKeyState(VOID)
{
    g_InputState.dwKeyPress = 0;
}

/**
 * Initialize the input subsystem.
 */
VOID PAL_InitInput(VOID)
{
    memset((void *) &g_InputState, 0, sizeof(g_InputState));
    g_InputState.dir = kDirUnknown;
    g_InputState.prevdir = kDirUnknown;

    //
    // Check for joystick
    //
#ifdef PAL_HAS_JOYSTICKS
    if (SDL_NumJoysticks() > 0 && g_fUseJoystick) {
        g_pJoy = SDL_JoystickOpen(0);

        //
        // HACKHACK: applesmc and Android Accelerometer shouldn't be considered as real joysticks
        //
        if (strcmp(SDL_JoystickName(g_pJoy), "applesmc") == 0 || strcmp(SDL_JoystickName(g_pJoy), "Android Accelerometer") == 0) {
            SDL_JoystickClose(g_pJoy);

            if (SDL_NumJoysticks() > 1) {
                g_pJoy = SDL_JoystickOpen(1);
            } else {
                g_pJoy = NULL;
            }
        }

        if (g_pJoy != NULL) {
            SDL_JoystickEventState(SDL_ENABLE);
        }
    }
#endif

#ifdef PAL_ALLOW_KEYREPEAT
    SDL_EnableKeyRepeat(0, 0);
#endif
}

/**
 * Shutdown the input subsystem.
 */
VOID PAL_ShutdownInput(VOID)
{
#ifdef PAL_HAS_JOYSTICKS
#if SDL_VERSION_ATLEAST(2, 0, 0)
    if (g_pJoy != NULL) {
        SDL_JoystickClose(g_pJoy);
        g_pJoy = NULL;
    }
#else
    if (SDL_JoystickOpened(0))
    {
       assert(g_pJoy != NULL);
       SDL_JoystickClose(g_pJoy);
       g_pJoy = NULL;
    }
#endif
#endif
}


/**
 * Process all events.
 */
VOID PAL_ProcessEvent(VOID)
{
#ifdef PAL_HAS_NATIVEMIDI
    MIDI_CheckLoop();
#endif
    while (PAL_PollEvent(NULL));
}

/**
 * Poll and process one event.
 *
 * @param[out] event - Events polled from SDL.
 * @return value of PAL_PollEvent
 */
int PAL_PollEvent(SDL_Event *event)
{
    SDL_Event evt;

    int ret = SDL_PollEvent(&evt);
    if (ret != 0) {
        PAL_EventFilter(&evt);
    }

    if (event != NULL) {
        *event = evt;
    }

    return ret;
}
Beispiel #4
0
VOID
PAL_SceneFade(
   INT         iPaletteNum,
   BOOL        fNight,
   INT         iStep
)
/*++
  Purpose:

    Fade in or fade out the screen. Update the scene during the process.

  Parameters:

    [IN]  iPaletteNum - number of the palette.

    [IN]  fNight - whether use the night palette or not.

    [IN]  iStep - positive to fade in, nagative to fade out.

  Return value:

    None.

--*/
{
   SDL_Color            *palette, newpalette[256];
   int                   i, j;
   DWORD                 time;

   palette = PAL_GetPalette(iPaletteNum, fNight);

   if (palette == NULL)
   {
      return;
   }

   if (iStep == 0)
   {
      iStep = 1;
   }

   gpGlobals->fNeedToFadeIn = FALSE;

   if (iStep > 0)
   {
      for (i = 0; i < 64; i += iStep)
      {
         time = SDL_GetTicks() + 100;

         //
         // Generate the scene
         //
         PAL_ClearKeyState();
         g_InputState.dir = kDirUnknown;
         g_InputState.prevdir = kDirUnknown;
         PAL_GameUpdate(FALSE);
         PAL_MakeScene();
         VIDEO_UpdateScreen(NULL);

         //
         // Calculate the current palette...
         //
         for (j = 0; j < 256; j++)
         {
            newpalette[j].r = (palette[j].r * i) >> 6;
            newpalette[j].g = (palette[j].g * i) >> 6;
            newpalette[j].b = (palette[j].b * i) >> 6;
         }
         VIDEO_SetPalette(newpalette);

         while (PAL_PollEvent(NULL));

         while (SDL_GetTicks() < time)
         {
            while (PAL_PollEvent(NULL));
            SDL_Delay(5);
         }
      }
   }
   else
   {
      for (i = 63; i >= 0; i += iStep)