Browse Source

audio & video & input modify for SDL & SDL2 compatibility

LouYihua 7 years ago
parent
commit
9ed8b65201
8 changed files with 42 additions and 74 deletions
  1. 3 1
      android/jni/src/pal_config.h
  2. 0 2
      audio.c
  3. 7 20
      common.h
  4. 27 30
      input.c
  5. 3 1
      ios/pal_config.h
  6. 0 11
      main.c
  7. 2 5
      win32/pal_config.h
  8. 0 4
      win32/win32.cpp

+ 3 - 1
android/jni/src/pal_config.h

@@ -13,12 +13,14 @@
 #  define PAL_VIDEO_INIT_FLAGS  (SDL_HWSURFACE | SDL_FULLSCREEN)
 # endif
 
-#define PAL_SDL_INIT_FLAGS	(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK)
+# define PAL_SDL_INIT_FLAGS	(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK)
 
 # define PAL_PLATFORM         "Android"
 # define PAL_CREDIT           "Rikku2000"
 # define PAL_PORTYEAR         "2013"
 
+# define PAL_IS_VALID_JOYSTICK(s)  (strcmp((s), "Android Accelerometer") != 0)
+
 LPCSTR
 UTIL_BasePath(
    VOID

+ 0 - 2
audio.c

@@ -120,9 +120,7 @@ AUDIO_FillBuffer(
 
 --*/
 {
-#if SDL_VERSION_ATLEAST(2,0,0)
    memset(stream, 0, len);
-#endif
 
    gAudioDevice.cvt.buf = stream;
    gAudioDevice.cvt.len = len;

+ 7 - 20
common.h

@@ -47,29 +47,12 @@ extern "C"
 #include "SDL.h"
 #include "SDL_endian.h"
 
-#if SDL_VERSION_ATLEAST(2,0,0)
-
-# define SDLK_KP1     SDLK_KP_1
-# define SDLK_KP2     SDLK_KP_2
-# define SDLK_KP3     SDLK_KP_3
-# define SDLK_KP4     SDLK_KP_4
-# define SDLK_KP5     SDLK_KP_5
-# define SDLK_KP6     SDLK_KP_6
-# define SDLK_KP7     SDLK_KP_7
-# define SDLK_KP8     SDLK_KP_8
-# define SDLK_KP9     SDLK_KP_9
-# define SDLK_KP0     SDLK_KP_0
-
-#else
-
-# ifndef PAL_FATAL_OUTPUT
-#  define PAL_FATAL_OUTPUT(s)
-# endif
-
+#ifndef PAL_FATAL_OUTPUT
+# define PAL_FATAL_OUTPUT(s)
 #endif
 
 #if !defined(fmax) || !defined(fmin)
-#include <math.h>
+# include <math.h>
 #endif
 
 #ifndef max
@@ -218,6 +201,10 @@ typedef const WCHAR        *LPCWSTR;
 #define SDL_TICKS_PASSED(A, B)  ((Sint32)((B) - (A)) <= 0)
 #endif
 
+#ifndef PAL_IS_VALID_JOYSTICK
+# define PAL_IS_VALID_JOYSTICK(s)  TRUE
+#endif
+
 typedef enum tagCODEPAGE {
 	CP_MIN = 0,
 	CP_BIG5 = 0,

+ 27 - 30
input.c

@@ -28,9 +28,22 @@ volatile PALINPUTSTATE   g_InputState;
 #if PAL_HAS_JOYSTICKS
 static SDL_Joystick     *g_pJoy = NULL;
 #endif
-#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
-#define SDL_JoystickNameForIndex SDL_JoystickName
+
+#if !SDL_VERSION_ATLEAST(2,0,0)
+# define SDLK_KP_1     SDLK_KP1
+# define SDLK_KP_2     SDLK_KP2
+# define SDLK_KP_3     SDLK_KP3
+# define SDLK_KP_4     SDLK_KP4
+# define SDLK_KP_5     SDLK_KP5
+# define SDLK_KP_6     SDLK_KP6
+# define SDLK_KP_7     SDLK_KP7
+# define SDLK_KP_8     SDLK_KP8
+# define SDLK_KP_9     SDLK_KP9
+# define SDLK_KP_0     SDLK_KP0
+
+# define SDL_JoystickNameForIndex SDL_JoystickName
 #endif
+
 BOOL                     g_fUseJoystick = TRUE;
 
 static void _default_init_filter() {}
@@ -89,7 +102,7 @@ PAL_KeyboardEventFilter(
       switch (lpEvent->key.keysym.sym)
       {
       case SDLK_UP:
-      case SDLK_KP8:
+      case SDLK_KP_8:
          if (gpGlobals->fInBattle || g_InputState.dir != kDirNorth)
          {
             g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
@@ -99,7 +112,7 @@ PAL_KeyboardEventFilter(
          break;
 
       case SDLK_DOWN:
-      case SDLK_KP2:
+      case SDLK_KP_2:
          if (gpGlobals->fInBattle || g_InputState.dir != kDirSouth)
          {
             g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
@@ -109,7 +122,7 @@ PAL_KeyboardEventFilter(
          break;
 
       case SDLK_LEFT:
-      case SDLK_KP4:
+      case SDLK_KP_4:
          if (gpGlobals->fInBattle || g_InputState.dir != kDirWest)
          {
             g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
@@ -119,7 +132,7 @@ PAL_KeyboardEventFilter(
          break;
 
      case SDLK_RIGHT:
-     case SDLK_KP6:
+     case SDLK_KP_6:
          if (gpGlobals->fInBattle || g_InputState.dir != kDirEast)
          {
             g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
@@ -132,7 +145,7 @@ PAL_KeyboardEventFilter(
       case SDLK_INSERT:
       case SDLK_LALT:
       case SDLK_RALT:
-      case SDLK_KP0:
+      case SDLK_KP_0:
          g_InputState.dwKeyPress |= kKeyMenu;
          break;
 
@@ -144,12 +157,12 @@ PAL_KeyboardEventFilter(
          break;
 
       case SDLK_PAGEUP:
-      case SDLK_KP9:
+      case SDLK_KP_9:
          g_InputState.dwKeyPress |= kKeyPgUp;
          break;
 
       case SDLK_PAGEDOWN:
-      case SDLK_KP3:
+      case SDLK_KP_3:
          g_InputState.dwKeyPress |= kKeyPgDn;
          break;
 
@@ -213,7 +226,7 @@ PAL_KeyboardEventFilter(
       switch (lpEvent->key.keysym.sym)
       {
       case SDLK_UP:
-      case SDLK_KP8:
+      case SDLK_KP_8:
          if (g_InputState.dir == kDirNorth)
          {
             g_InputState.dir = g_InputState.prevdir;
@@ -222,7 +235,7 @@ PAL_KeyboardEventFilter(
          break;
 
       case SDLK_DOWN:
-      case SDLK_KP2:
+      case SDLK_KP_2:
          if (g_InputState.dir == kDirSouth)
          {
             g_InputState.dir = g_InputState.prevdir;
@@ -231,7 +244,7 @@ PAL_KeyboardEventFilter(
          break;
 
       case SDLK_LEFT:
-      case SDLK_KP4:
+      case SDLK_KP_4:
          if (g_InputState.dir == kDirWest)
          {
             g_InputState.dir = g_InputState.prevdir;
@@ -240,7 +253,7 @@ PAL_KeyboardEventFilter(
          break;
 
       case SDLK_RIGHT:
-      case SDLK_KP6:
+      case SDLK_KP_6:
          if (g_InputState.dir == kDirEast)
          {
             g_InputState.dir = g_InputState.prevdir;
@@ -848,7 +861,6 @@ PAL_EventFilter(
       }
       break;
 
-#ifdef __IOS__
    case SDL_APP_WILLENTERBACKGROUND:
       g_bRenderPaused = TRUE;
       break;
@@ -857,10 +869,7 @@ PAL_EventFilter(
       g_bRenderPaused = FALSE;
       VIDEO_UpdateScreen(NULL);
       break;
-#endif
-
 #else
-           
    case SDL_VIDEORESIZE:
       //
       // resized the window
@@ -941,10 +950,7 @@ PAL_InitInput(
       int i;
 	  for (i = 0; i < SDL_NumJoysticks(); i++)
       {
-         //
-         // HACKHACK: applesmc and Android Accelerometer shouldn't be considered as real joysticks
-         //
-         if (strcmp(SDL_JoystickNameForIndex(i), "applesmc") != 0 && strcmp(SDL_JoystickNameForIndex(i), "Android Accelerometer") != 0)
+         if (PAL_IS_VALID_JOYSTICK(SDL_JoystickNameForIndex(i)))
          {
             g_pJoy = SDL_JoystickOpen(i);
             break;
@@ -985,20 +991,11 @@ PAL_ShutdownInput(
 --*/
 {
 #if 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
    input_shutdown_filter();
 }

+ 3 - 1
ios/pal_config.h

@@ -13,12 +13,14 @@
 #  define PAL_VIDEO_INIT_FLAGS  (SDL_HWSURFACE | SDL_FULLSCREEN)
 # endif
 
-#define PAL_SDL_INIT_FLAGS	(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK)
+# define PAL_SDL_INIT_FLAGS	(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK)
 
 # define PAL_PLATFORM         "Apple iOS"
 # define PAL_CREDIT           "(Unknown)"
 # define PAL_PORTYEAR         "2015"
 
+# define PAL_IS_VALID_JOYSTICK(s)  (strcmp((s), "applesmc") != 0)
+
 LPCSTR
 UTIL_BasePath(
    VOID

+ 0 - 11
main.c

@@ -63,18 +63,7 @@ PAL_Init(
    //
    if (SDL_Init(PAL_SDL_INIT_FLAGS) == -1)
    {
-#if defined (_WIN32) && SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
-      //
-      // Try the WINDIB driver if DirectX failed.
-      //
-      putenv("SDL_VIDEODRIVER=windib");
-      if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK) == -1)
-      {
-         TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
-      }
-#else
       TerminateOnError("Could not initialize SDL: %s.\n", SDL_GetError());
-#endif
    }
 
    //

+ 2 - 5
win32/pal_config.h

@@ -4,11 +4,6 @@
 # define PAL_HAS_JOYSTICKS    1
 #endif
 
-#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
-# define PAL_ALLOW_KEYREPEAT   1
-# define PAL_HAS_SDLCD         1
-#endif
-
 #define PAL_PREFIX            "./"
 #define PAL_SAVE_PREFIX       "./"
 
@@ -21,6 +16,8 @@
 #else
 # define PAL_VIDEO_INIT_FLAGS  (SDL_HWSURFACE | SDL_RESIZABLE | (gConfig.fFullScreen ? SDL_FULLSCREEN : 0))
 # define PAL_FATAL_OUTPUT(s)   MessageBoxA(0, (s), "FATAL ERROR", MB_ICONERROR)
+# define PAL_ALLOW_KEYREPEAT   1
+# define PAL_HAS_SDLCD         1
 #endif
 
 #define PAL_SDL_INIT_FLAGS	(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE | SDL_INIT_JOYSTICK)

+ 0 - 4
win32/win32.cpp

@@ -260,10 +260,6 @@ INT_PTR CALLBACK LauncherDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPAR
 
 extern "C" int UTIL_Platform_Init(int argc, char* argv[])
 {
-#if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
-	putenv("SDL_VIDEODRIVER=directx");
-#endif
-
 	g_hInstance = GetModuleHandle(nullptr);
 #if !defined(__MINGW32__) || _WIN32_WINNT > _WIN32_WINNT_WS03 // compile time switch; use `make CCFLAGS=-D_WIN32_WINNT=_WIN32_WINNT_VISTA` for vista+ only automatic language detection
 	g_wLanguage = GetThreadUILanguage();