Browse Source

SDL2: fixed event handling

Wei Mingzhi 9 years ago
parent
commit
0a3bdfeebc
5 changed files with 58 additions and 23 deletions
  1. 48 13
      input.c
  2. 1 1
      input.h
  3. 1 1
      input_PSP.c
  4. 6 6
      palette.c
  5. 2 2
      util.c

+ 48 - 13
input.c

@@ -24,7 +24,7 @@
 #include "main.h"
 #include <math.h>
 
-PALINPUTSTATE            g_InputState;
+volatile PALINPUTSTATE   g_InputState;
 static SDL_Joystick     *g_pJoy = NULL;
 BOOL                     g_fUseJoystick = TRUE;
 
@@ -875,18 +875,11 @@ PAL_TouchEventFilter(
 #endif
 }
 
-#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
 static int SDLCALL
 PAL_EventFilter(
    const SDL_Event       *lpEvent
 )
-#else
-static int SDLCALL
-PAL_EventFilter(
-   void                  *userdata,
-   SDL_Event             *lpEvent
-)
-#endif
+
 /*++
   Purpose:
 
@@ -997,13 +990,11 @@ PAL_InitInput(
 
 --*/
 {
-   memset(&g_InputState, 0, sizeof(g_InputState));
+   memset((void *)&g_InputState, 0, sizeof(g_InputState));
    g_InputState.dir = kDirUnknown;
    g_InputState.prevdir = kDirUnknown;
 #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
    SDL_SetEventFilter(PAL_EventFilter);
-#else
-   SDL_SetEventFilter(PAL_EventFilter, NULL);
 #endif
 
    //
@@ -1080,5 +1071,49 @@ PAL_ProcessEvent(
 #ifdef PAL_HAS_NATIVEMIDI
    MIDI_CheckLoop();
 #endif
-   while (SDL_PollEvent(NULL));
+   while (PAL_PollEvent(NULL));
+}
+
+
+int
+PAL_PollEvent(
+   SDL_Event *event
+)
+/*++
+  Purpose:
+
+    Poll and process one event.
+
+  Parameters:
+
+    [OUT] event - Events polled from SDL.
+
+  Return value:
+
+    Return value of PAL_PollEvent.
+
+--*/
+{
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+   //
+   // SDL2 changed the timing of event callback so we need to handle
+   // events manually when polled
+   //
+   SDL_Event evt;
+
+   int ret = SDL_PollEvent(&evt);
+   if (ret != 0)
+   {
+	  PAL_EventFilter(&evt);
+   }
+
+   if (event != NULL)
+   {
+	  *event = evt;
+   }
+
+   return ret;
+#else
+   return SDL_PollEvent(&evt);
+#endif
 }

+ 1 - 1
input.h

@@ -36,7 +36,7 @@ typedef struct tagPALINPUTSTATE
    DWORD                  dwKeyPress;
 } PALINPUTSTATE;
 
-extern PALINPUTSTATE g_InputState;
+extern volatile PALINPUTSTATE g_InputState;
 
 enum PALKEY
 {

+ 1 - 1
input_PSP.c

@@ -388,7 +388,7 @@ PAL_ProcessEvent(
 
 --*/
 {
-   while (SDL_PollEvent(NULL));
+   while (PAL_PollEvent(NULL));
    PAL_JoystickEventFilter();
 }
 

+ 6 - 6
palette.c

@@ -322,11 +322,11 @@ PAL_SceneFade(
          }
          VIDEO_SetPalette(newpalette);
 
-         while (SDL_PollEvent(NULL));
+         while (PAL_PollEvent(NULL));
 
          while (SDL_GetTicks() < time)
          {
-            while (SDL_PollEvent(NULL));
+            while (PAL_PollEvent(NULL));
             SDL_Delay(5);
          }
       }
@@ -358,11 +358,11 @@ PAL_SceneFade(
          }
          VIDEO_SetPalette(newpalette);
 
-         while (SDL_PollEvent(NULL));
+         while (PAL_PollEvent(NULL));
 
          while (SDL_GetTicks() < time)
          {
-            while (SDL_PollEvent(NULL));
+            while (PAL_PollEvent(NULL));
             SDL_Delay(5);
          }
       }
@@ -438,11 +438,11 @@ PAL_PaletteFade(
          VIDEO_UpdateScreen(NULL);
       }
 
-      while (SDL_PollEvent(NULL));
+      while (PAL_PollEvent(NULL));
 
       while (SDL_GetTicks() < time)
       {
-         while (SDL_PollEvent(NULL));
+         while (PAL_PollEvent(NULL));
          SDL_Delay(5);
       }
    }

+ 2 - 2
util.c

@@ -267,12 +267,12 @@ UTIL_Delay(
 {
    unsigned int t = SDL_GetTicks() + ms;
 
-   while (SDL_PollEvent(NULL));
+   while (PAL_PollEvent(NULL));
 
    while (SDL_GetTicks() < t)
    {
       SDL_Delay(1);
-      while (SDL_PollEvent(NULL));
+      while (PAL_PollEvent(NULL));
    }
 
 #ifdef PAL_HAS_NATIVEMIDI