rnd-20000806-1-src
authorHolger Schemel <info@artsoft.org>
Sun, 6 Aug 2000 16:37:52 +0000 (18:37 +0200)
committerHolger Schemel <info@artsoft.org>
Sat, 30 Aug 2014 08:34:45 +0000 (10:34 +0200)
14 files changed:
src/Makefile
src/events.c
src/events.h
src/init.c
src/joystick.c
src/joystick.h
src/main.c
src/screens.c
src/sdl.h
src/sound.c
src/sound.h
src/system.c
src/system.h
src/tools.c

index 17615f9017d8ba7bc57e3dd235d7b6fc74b647e1..f9a71038c907129b52d791d5baec70bdac2bf3b5 100644 (file)
@@ -35,12 +35,12 @@ EXTRA_LIBS = -lnsl -lsocket -R$(XLIB_PATH)
 endif
 
 USE_SDL = true
-USE_SDL_OLD_LIBS = true
+USE_SDL_OLD_LIBS = false
 
 ifeq ($(USE_SDL_OLD_LIBS),true)
-SDL_EXTRA_LIBS = -lIMG
+SDL_EXTRA_LIBS = -lIMG -lmixer
 else
-SDL_EXTRA_LIBS = -lSDL_image
+SDL_EXTRA_LIBS = -lSDL_image -lSDL_mixer
 endif
 
 SDL_CFLAGS := -DUSE_SDL_LIBRARY $(shell sdl-config --cflags)
index a2b346c01eb57f02ee3270b63e2bb2689d0eab65..e2f9db8ec530d9f93614a2ae27fb82cbfc39e2ba 100644 (file)
 #define KEY_RELEASED           FALSE
 #define KEY_PRESSED            TRUE
 
+
+/* event filter especially needed for SDL event filtering due to
+   delay problems with lots of mouse motion events when mouse
+   button not pressed */
+
+int EventFilter(const Event *event)
+{
+  if (event->type != EVENT_MOTIONNOTIFY)
+    return 1;
+
+  /* get mouse motion events without pressed button only in level editor */
+  if (button_status == MB_RELEASED && game_status != LEVELED)
+    return 0;
+  else
+    return 1;
+}
+
 void EventLoop(void)
 {
   while(1)
@@ -38,25 +55,28 @@ void EventLoop(void)
 
       NextEvent(&event);
 
-      switch(event.type)
+      if (EventFilter(&event))
       {
-       case EVENT_BUTTONPRESS:
-       case EVENT_BUTTONRELEASE:
-         HandleButtonEvent((ButtonEvent *) &event);
-         break;
-
-       case EVENT_MOTIONNOTIFY:
-         HandleMotionEvent((MotionEvent *) &event);
-         break;
-
-       case EVENT_KEYPRESS:
-       case EVENT_KEYRELEASE:
-         HandleKeyEvent((KeyEvent *) &event);
-         break;
-
-       default:
-         HandleOtherEvents(&event);
-         break;
+       switch(event.type)
+       {
+         case EVENT_BUTTONPRESS:
+         case EVENT_BUTTONRELEASE:
+           HandleButtonEvent((ButtonEvent *) &event);
+           break;
+  
+         case EVENT_MOTIONNOTIFY:
+           HandleMotionEvent((MotionEvent *) &event);
+           break;
+  
+         case EVENT_KEYPRESS:
+         case EVENT_KEYRELEASE:
+           HandleKeyEvent((KeyEvent *) &event);
+           break;
+  
+         default:
+           HandleOtherEvents(&event);
+           break;
+       }
       }
     }
 
@@ -101,6 +121,14 @@ void HandleOtherEvents(Event *event)
       HandleClientMessageEvent((ClientMessageEvent *) event);
       break;
 
+#ifdef USE_SDL_LIBRARY
+    case SDL_JOYAXISMOTION:
+    case SDL_JOYBUTTONDOWN:
+    case SDL_JOYBUTTONUP:
+      HandleJoystickEvent(event);
+      break;
+#endif
+
     default:
       break;
   }
@@ -227,17 +255,17 @@ void HandleButtonEvent(ButtonEvent *event)
 
 void HandleMotionEvent(MotionEvent *event)
 {
-  int win_x, win_y;
-
-  if (!QueryPointer(window, &win_x, &win_y))
+  if (!PointerInWindow(window))
     return;    /* window and pointer are on different screens */
 
-  if (!button_status && game_status != LEVELED)
+#if 0
+  if (button_status == MB_RELEASED && game_status != LEVELED)
     return;
+#endif
 
   motion_status = TRUE;
 
-  HandleButton(win_x, win_y, button_status);
+  HandleButton(event->x, event->y, button_status);
 }
 
 void HandleKeyEvent(KeyEvent *event)
index 931aa7cee9220faa4613cb8603cafbdb41fc571b..a1388b2cf747cafae2a33f9a9446d1104e02b361 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "main.h"
 
+int EventFilter(const Event *);
 void EventLoop(void);
 void HandleOtherEvents(Event *);
 void ClearEventQueue(void);
index ab51dd9fef4ce8c6ffc1ddacf9df2da289f99fc4..92f52c568d337d766e2a69e22956caefdf048290 100644 (file)
@@ -14,6 +14,7 @@
 #include <signal.h>
 
 #include "init.h"
+#include "events.h"
 #include "misc.h"
 #include "sound.h"
 #include "screens.h"
@@ -146,6 +147,31 @@ void InitSound()
   if (sound_status == SOUND_OFF)
     return;
 
+#ifdef USE_SDL_LIBRARY
+  /* initialize SDL audio */
+
+  if (SDL_Init(SDL_INIT_AUDIO) < 0)
+  {
+    Error(ERR_WARN, "SDL_Init() failed: %s\n", SDL_GetError());
+    sound_status = SOUND_OFF;
+    return;
+  }
+
+  if (Mix_OpenAudio(22050, AUDIO_S16, 2, 256) < 0)
+  {
+    Error(ERR_WARN, "Mix_OpenAudio() failed: %s\n", SDL_GetError());
+    sound_status = SOUND_OFF;
+    return;
+  }
+
+  Mix_Volume(-1, SDL_MIX_MAXVOLUME / 4);
+  Mix_VolumeMusic(SDL_MIX_MAXVOLUME / 4);
+
+  sound_status = SOUND_AVAILABLE;
+  sound_loops_allowed = TRUE;
+
+#else /* !USE_SDL_LIBRARY */
+
 #ifndef MSDOS
   if (access(sound_device_name, W_OK) != 0)
   {
@@ -180,6 +206,7 @@ void InitSound()
   */
 
 #endif /* MSDOS */
+#endif /* !USE_SDL_LIBRARY */
 
   for(i=0; i<NUM_SOUNDS; i++)
   {
@@ -187,13 +214,26 @@ void InitSound()
     sprintf(sound_name[i], "%d", i + 1);
 #endif
 
+#ifdef USE_SDL_LIBRARY
+    {
+      char *str = getStringCopy(sound_name[i]);
+      sprintf(str, "%d", i + 1);
+      Sound[i].name = str;
+    }
+#else
     Sound[i].name = sound_name[i];
+#endif
     if (!LoadSound(&Sound[i]))
     {
       sound_status = SOUND_OFF;
       return;
     }
   }
+
+#if 0
+  sound_status = SOUND_OFF;
+#endif
+
 }
 
 void InitSoundServer()
@@ -201,6 +241,10 @@ void InitSoundServer()
   if (sound_status == SOUND_OFF)
     return;
 
+#ifdef USE_SDL_LIBRARY
+  return;
+#endif
+
 #ifndef MSDOS
 
   if (pipe(sound_pipe)<0)
@@ -236,6 +280,10 @@ void InitSoundServer()
 
 void InitJoysticks()
 {
+#ifdef USE_SDL_LIBRARY
+  static boolean sdl_joystick_subsystem_initialized = FALSE;
+#endif
+
   int i;
 
   if (global_joystick_status == JOYSTICK_OFF)
@@ -243,6 +291,48 @@ void InitJoysticks()
 
   joystick_status = JOYSTICK_OFF;
 
+#ifdef USE_SDL_LIBRARY
+
+  if (!sdl_joystick_subsystem_initialized)
+  {
+    sdl_joystick_subsystem_initialized = TRUE;
+
+    if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
+    {
+      Error(ERR_EXIT, "SDL_Init() failed: %s\n", SDL_GetError());
+      return;
+    }
+  }
+
+  for (i=0; i<MAX_PLAYERS; i++)
+  {
+    char *device_name = setup.input[i].joy.device_name;
+    int joystick_nr = getJoystickNrFromDeviceName(device_name);
+
+    if (joystick_nr >= SDL_NumJoysticks())
+      joystick_nr = -1;
+
+    /* misuse joystick file descriptor variable to store joystick number */
+    stored_player[i].joystick_fd = joystick_nr;
+
+    /* this allows subsequent calls to 'InitJoysticks' for re-initialization */
+    if (Check_SDL_JoystickOpened(joystick_nr))
+      Close_SDL_Joystick(joystick_nr);
+
+    if (!setup.input[i].use_joystick)
+      continue;
+
+    if (!Open_SDL_Joystick(joystick_nr))
+    {
+      Error(ERR_WARN, "cannot open joystick %d", joystick_nr);
+      continue;
+    }
+
+    joystick_status = JOYSTICK_AVAILABLE;
+  }
+
+#else /* !USE_SDL_LIBRARY */
+
 #ifndef MSDOS
   for (i=0; i<MAX_PLAYERS; i++)
   {
@@ -296,12 +386,14 @@ void InitJoysticks()
     stored_player[i].joystick_fd = joystick_nr;
   }
 #endif
+
+#endif /* !USE_SDL_LIBRARY */
 }
 
 void InitDisplay()
 {
 #ifdef USE_SDL_LIBRARY
-  /* initialize SDL */
+  /* initialize SDL video */
   if (SDL_Init(SDL_INIT_VIDEO) < 0)
     Error(ERR_EXIT, "SDL_Init() failed: %s\n", SDL_GetError());
 
@@ -367,6 +459,9 @@ void InitWindow(int argc, char *argv[])
   /* set window and icon title */
   SDL_WM_SetCaption(WINDOW_TITLE_STRING, WINDOW_TITLE_STRING);
 
+  /* set event filter to filter out certain mouse motion events */
+  SDL_SetEventFilter(EventFilter);
+
 #else /* !USE_SDL_LIBRARY */
 
   unsigned int border_width = 4;
index 3aeb8e9d6163bc6b7dd3ae72e75d3d9c74a06185..c069289a6beea6b674f3a6b4646104b0b46a800a 100644 (file)
 #include "joystick.h"
 #include "misc.h"
 
+#ifndef MSDOS
+static int JoystickPosition(int middle, int margin, int actual)
+{
+  long range, pos;
+  int percentage;
+
+  if (margin < middle && actual > middle)
+    return 0;
+  if (margin > middle && actual < middle)
+    return 0;
+
+  range = ABS(margin - middle);
+  pos = ABS(actual - middle);
+  percentage = (int)(pos * 100 / range);
+
+  if (percentage > 100)
+    percentage = 100;
+
+  return percentage;
+}
+#endif
+
+#ifdef USE_SDL_LIBRARY
+
+static SDL_Joystick *sdl_joystick[MAX_PLAYERS] = { NULL, NULL, NULL, NULL };
+static int sdl_js_axis[MAX_PLAYERS][2]   = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
+static int sdl_js_button[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
+
+SDL_Joystick *Get_SDL_Joystick(int nr)
+{
+  return sdl_joystick[nr];
+}
+
+boolean Open_SDL_Joystick(int nr)
+{
+  if (nr < 0 || nr > MAX_PLAYERS)
+    return FALSE;
+
+  return ((sdl_joystick[nr] = SDL_JoystickOpen(nr)) == NULL ? FALSE : TRUE);
+}
+
+void Close_SDL_Joystick(int nr)
+{
+  if (nr < 0 || nr > MAX_PLAYERS)
+    return;
+
+  SDL_JoystickClose(sdl_joystick[nr]);
+}
+
+boolean Check_SDL_JoystickOpened(int nr)
+{
+  if (nr < 0 || nr > MAX_PLAYERS)
+    return FALSE;
+
+  return (SDL_JoystickOpened(nr) ? TRUE : FALSE);
+}
+
+void HandleJoystickEvent(Event *event)
+{
+  switch(event->type)
+  {
+    case SDL_JOYAXISMOTION:
+      if (event->jaxis.axis < 2)
+      {
+       sdl_js_axis[event->jaxis.which][event->jaxis.axis]= event->jaxis.value;
+
+#if 0
+       printf("js_%d %s-axis: %d\n",
+              event->jaxis.which,
+              (event->jaxis.axis == 0 ? "x" : "y"),
+              event->jaxis.value);
+#endif
+      }
+      break;
+
+    case SDL_JOYBUTTONDOWN:
+      if (event->jbutton.button < 2)
+      {
+       sdl_js_button[event->jbutton.which][event->jbutton.button] = TRUE;
+
+#if 0
+        printf("js_%d button %d: pressed\n",
+              event->jbutton.which,
+              event->jbutton.button);
+#endif
+      }
+      break;
+
+    case SDL_JOYBUTTONUP:
+      if (event->jbutton.button < 2)
+      {
+       sdl_js_button[event->jbutton.which][event->jbutton.button] = FALSE;
+
+#if 0
+        printf("js_%d button %d: released\n",
+              event->jbutton.which,
+              event->jbutton.button);
+#endif
+      }
+      break;
+
+    default:
+      break;
+  }
+}
+
+int Get_SDL_Joystick_Axis(int nr, int axis)
+{
+  if (nr < 0 || nr > MAX_PLAYERS)
+    return 0;
+
+  if (axis < 0 || axis > 1)
+    return 0;
+
+  return sdl_js_axis[nr][axis];
+}
+
+void CheckJoystickData()
+{
+}
+
+int Joystick(int player_nr)
+{
+  int joystick_nr = stored_player[player_nr].joystick_fd;
+  int js_x,js_y, js_b1,js_b2;
+  int left, right, up, down;
+  int result = 0;
+
+  if (joystick_status == JOYSTICK_OFF)
+    return 0;
+
+  if (game_status == SETUPINPUT)
+    return 0;
+
+  if (!setup.input[player_nr].use_joystick ||
+      !Check_SDL_JoystickOpened(joystick_nr))
+    return 0;
+
+  js_x  = sdl_js_axis[joystick_nr][0];
+  js_y  = sdl_js_axis[joystick_nr][1];
+
+  js_b1 = sdl_js_button[joystick_nr][0];
+  js_b2 = sdl_js_button[joystick_nr][1];
+
+
+
+#if 0
+  printf("JOYSTICK %d: js_x == %d, js_y == %d, js_b1 == %d, js_b2 == %d\n",
+        joystick_nr, js_x, js_y, js_b1, js_b2);
+#endif
+
+
+
+  left  = JoystickPosition(setup.input[player_nr].joy.xmiddle,
+                          setup.input[player_nr].joy.xleft,  js_x);
+  right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
+                          setup.input[player_nr].joy.xright, js_x);
+  up    = JoystickPosition(setup.input[player_nr].joy.ymiddle,
+                          setup.input[player_nr].joy.yupper, js_y);
+  down  = JoystickPosition(setup.input[player_nr].joy.ymiddle,
+                          setup.input[player_nr].joy.ylower, js_y);
+
+  if (left > JOYSTICK_PERCENT)
+    result |= JOY_LEFT;
+  else if (right > JOYSTICK_PERCENT)
+    result |= JOY_RIGHT;
+  if (up > JOYSTICK_PERCENT)
+    result |= JOY_UP;
+  else if (down > JOYSTICK_PERCENT)
+    result |= JOY_DOWN;
+
+  if (js_b1)
+    result |= JOY_BUTTON_1;
+  if (js_b2)
+    result |= JOY_BUTTON_2;
+
+
+
+#if 0
+  printf("result == 0x%08x\n", result);
+#endif
+
+
+
+  return result;
+}
+
+#else /* !USE_SDL_LIBRARY */
+
 void CheckJoystickData()
 {
   int i;
@@ -42,28 +231,6 @@ void CheckJoystickData()
   }
 }
 
-#ifndef MSDOS
-static int JoystickPosition(int middle, int margin, int actual)
-{
-  long range, pos;
-  int percentage;
-
-  if (margin < middle && actual > middle)
-    return 0;
-  if (margin > middle && actual < middle)
-    return 0;
-
-  range = ABS(margin - middle);
-  pos = ABS(actual - middle);
-  percentage = (int)(pos * 100 / range);
-
-  if (percentage > 100)
-    percentage = 100;
-
-  return percentage;
-}
-#endif
-
 #ifndef MSDOS
 int Joystick(int player_nr)
 {
@@ -196,6 +363,8 @@ int Joystick(int player_nr)
 }
 #endif /* MSDOS */
 
+#endif /* !USE_SDL_LIBRARY */
+
 int JoystickButton(int player_nr)
 {
   static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
index a48c2bb39f2d54d212fc4bb25f41aab51ad54249..64fd00690bf2e106baa661f10de0619a67d82fdd 100644 (file)
 
 /* get these values from the program 'js' from the joystick package, */
 /* set JOYSTICK_PERCENT to a threshold appropriate for your joystick */
+
+#ifdef USE_SDL_LIBRARY
+#define JOYSTICK_XLEFT         -32767
+#define JOYSTICK_XMIDDLE       0
+#define JOYSTICK_XRIGHT                32767
+#define JOYSTICK_YUPPER                -32767
+#define JOYSTICK_YMIDDLE       0
+#define JOYSTICK_YLOWER                32767
+#else
 #define JOYSTICK_XLEFT         30
 #define JOYSTICK_XMIDDLE       530
 #define JOYSTICK_XRIGHT                1250
 #define JOYSTICK_YUPPER                40
 #define JOYSTICK_YMIDDLE       680
 #define JOYSTICK_YLOWER                1440
+#endif
 
 #define JOYSTICK_PERCENT       25
 
 #endif
 
 
+#ifdef USE_SDL_LIBRARY
+SDL_Joystick *Get_SDL_Joystick(int);
+boolean Open_SDL_Joystick(int);
+void Close_SDL_Joystick(int);
+boolean Check_SDL_JoystickOpened(int);
+void HandleJoystickEvent(Event *);
+int Get_SDL_Joystick_Axis(int, int);
+#endif
+
 void CheckJoystickData(void);
 int Joystick(int);
 int JoystickButton(int);
@@ -70,3 +89,4 @@ int AnyJoystick(void);
 int AnyJoystickButton(void);
 
 #endif
+
index 86b7eb179e4668d08a7a03590c041cbd36455783..7c51c2b6810683cee41230f8c2dc72f1691a2835 100644 (file)
@@ -747,6 +747,193 @@ void TEST_SDL_EVENT_LOOP()
   SDL_Quit();
 }
 
+#define SCREEN_WIDTH   640
+#define SCREEN_HEIGHT  480
+
+void WatchJoysticks()
+{
+       SDL_Surface *screen;
+       const char *name;
+       int i, done;
+       SDL_Event event;
+       int x, y, draw;
+       SDL_Rect axis_area[2];
+       int joystick_nr = 0;
+       SDL_Joystick *joystick = Get_SDL_Joystick(joystick_nr);
+
+       /* Set a video mode to display joystick axis position */
+       screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
+       if ( screen == NULL ) {
+               fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError());
+               return;
+       }
+
+       /* Print info about the joysticks we are watching */
+       for (i=0; i<2; i++)
+       {
+         joystick = Get_SDL_Joystick(i);
+
+         name = SDL_JoystickName(i);
+         printf("Watching joystick %d: (%s)\n", i,
+                name ? name : "Unknown Joystick");
+         printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
+                SDL_JoystickNumAxes(joystick),
+                SDL_JoystickNumHats(joystick),
+                SDL_JoystickNumBalls(joystick),
+                SDL_JoystickNumButtons(joystick));
+       }
+
+       /* Initialize drawing rectangles */
+       memset(axis_area, 0, (sizeof axis_area));
+       draw = 0;
+
+       /* Loop, getting joystick events! */
+       done = 0;
+       while ( ! done ) {
+               while ( SDL_PollEvent(&event) ) {
+                       switch (event.type) {
+                           case SDL_JOYAXISMOTION:
+                             joystick_nr = event.jaxis.which;
+                               printf("Joystick %d axis %d value: %d\n",
+                                      event.jaxis.which,
+                                      event.jaxis.axis,
+                                      event.jaxis.value);
+                               break;
+                           case SDL_JOYHATMOTION:
+                             joystick_nr = event.jaxis.which;
+                               printf("Joystick %d hat %d value:",
+                                      event.jhat.which,
+                                      event.jhat.hat);
+                               if ( event.jhat.value == SDL_HAT_CENTERED )
+                                       printf(" centered");
+                               if ( event.jhat.value & SDL_HAT_UP )
+                                       printf(" up");
+                               if ( event.jhat.value & SDL_HAT_RIGHT )
+                                       printf(" right");
+                               if ( event.jhat.value & SDL_HAT_DOWN )
+                                       printf(" down");
+                               if ( event.jhat.value & SDL_HAT_LEFT )
+                                       printf(" left");
+                               printf("\n");
+                               break;
+                           case SDL_JOYBALLMOTION:
+                             joystick_nr = event.jaxis.which;
+                               printf("Joystick %d ball %d delta: (%d,%d)\n",
+                                      event.jball.which,
+                                      event.jball.ball,
+                                      event.jball.xrel,
+                                      event.jball.yrel);
+                               break;
+                           case SDL_JOYBUTTONDOWN:
+                             joystick_nr = event.jaxis.which;
+                               printf("Joystick %d button %d down\n",
+                                      event.jbutton.which,
+                                      event.jbutton.button);
+                               break;
+                           case SDL_JOYBUTTONUP:
+                             joystick_nr = event.jaxis.which;
+                               printf("Joystick %d button %d up\n",
+                                      event.jbutton.which,
+                                      event.jbutton.button);
+                               break;
+                           case SDL_KEYDOWN:
+                               if ( event.key.keysym.sym != SDLK_ESCAPE ) {
+                                       break;
+                               }
+                               /* Fall through to signal quit */
+                           case SDL_QUIT:
+                               done = 1;
+                               break;
+                           default:
+                               break;
+                       }
+               }
+
+               joystick = Get_SDL_Joystick(joystick_nr);               
+
+               /* Update visual joystick state */
+               for ( i=0; i<SDL_JoystickNumButtons(joystick); ++i ) {
+                       SDL_Rect area;
+
+                       area.x = i*34;
+                       area.y = SCREEN_HEIGHT-34;
+                       area.w = 32;
+                       area.h = 32;
+                       if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
+                               SDL_FillRect(screen, &area, 0xFFFF);
+                       } else {
+                               SDL_FillRect(screen, &area, 0x0000);
+                       }
+                       SDL_UpdateRects(screen, 1, &area);
+               }
+
+               /* Erase previous axes */
+               SDL_FillRect(screen, &axis_area[draw], 0x0000);
+
+               /* Draw the X/Y axis */
+               draw = !draw;
+               x = (((int)SDL_JoystickGetAxis(joystick, 0))+32768);
+               x *= SCREEN_WIDTH;
+               x /= 65535;
+               if ( x < 0 ) {
+                       x = 0;
+               } else
+               if ( x > (SCREEN_WIDTH-16) ) {
+                       x = SCREEN_WIDTH-16;
+               }
+               y = (((int)SDL_JoystickGetAxis(joystick, 1))+32768);
+               y *= SCREEN_HEIGHT;
+               y /= 65535;
+               if ( y < 0 ) {
+                       y = 0;
+               } else
+               if ( y > (SCREEN_HEIGHT-16) ) {
+                       y = SCREEN_HEIGHT-16;
+               }
+               axis_area[draw].x = (Sint16)x;
+               axis_area[draw].y = (Sint16)y;
+               axis_area[draw].w = 16;
+               axis_area[draw].h = 16;
+               SDL_FillRect(screen, &axis_area[draw], 0xFFFF);
+
+               SDL_UpdateRects(screen, 2, axis_area);
+       }
+}
+
+void TEST_SDL_JOYSTICK()
+{
+  const char *name;
+  int i;
+
+  /* Initialize SDL (Note: video is required to start event loop) */
+  if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0 )
+  {
+    fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
+    exit(1);
+  }
+
+  /* Print information about the joysticks */
+  printf("There are %d joysticks attached\n", SDL_NumJoysticks());
+  for ( i=0; i<SDL_NumJoysticks(); ++i )
+  {
+    name = SDL_JoystickName(i);
+    printf("Joystick %d: %s\n",i,name ? name : "Unknown Joystick");
+  }
+
+  for (i=0; i<2; i++)
+  {
+    if (!Open_SDL_Joystick(i))
+      printf("Couldn't open joystick %d: %s\n", i, SDL_GetError());
+  }
+
+  WatchJoysticks();
+
+  for (i=0; i<2; i++)
+    Close_SDL_Joystick(i);
+
+  SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK);
+}
+
 #endif /* USE_SDL_LIBRARY */
 
 /* +-----------------------------------------------------------------------+ */
@@ -763,13 +950,18 @@ int main(int argc, char *argv[])
   _fmode = O_BINARY;
 #endif
 
+#if 1
   GetOptions(argv);
   OpenAll(argc,argv);
+#endif
 
 #if 0
 #ifdef USE_SDL_LIBRARY
+  /*
   TEST_SDL_BLIT_RECT((WIN_XSIZE - TILEX)/2, (WIN_YSIZE - TILEY)/2);
   TEST_SDL_EVENT_LOOP();
+  */
+  TEST_SDL_JOYSTICK();
   exit(0);
 #endif
 #endif
index ddd4def814d661eba2e1ade1a6ff5d63753a01e9..948f1ceb73602a5bbdfda6c0c2161be133678c15 100644 (file)
@@ -2087,11 +2087,17 @@ void CalibrateJoystick(int player_nr)
     }
 
 #ifndef MSDOS
+
+#ifdef USE_SDL_LIBRARY
+    joy_ctrl.x = Get_SDL_Joystick_Axis(joystick_fd, 0);
+    joy_ctrl.y = Get_SDL_Joystick_Axis(joystick_fd, 1);
+#else
     if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
     {
       joystick_status = JOYSTICK_OFF;
       goto error_out;
     }
+#endif
 
     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_ctrl.x);
     new_joystick_xright = MAX(new_joystick_xright, joy_ctrl.x);
@@ -2199,7 +2205,18 @@ void CalibrateJoystick(int player_nr)
   StopAnimation();
 
   DrawSetupInputScreen();
-  while(Joystick(player_nr) & JOY_BUTTON);
+
+  /* wait until the last pressed button was released */
+  while(Joystick(player_nr) & JOY_BUTTON)
+  {
+    if (PendingEvent())                /* got event */
+    {
+      Event event;
+
+      NextEvent(&event);
+      HandleOtherEvents(&event);
+    }
+  }
   return;
 
   error_out:
index 7d7c1c114d971553b5904949c4a23c8063f6e701..d6800fa9b700d20187572ef71952620ee0d14b08 100644 (file)
--- a/src/sdl.h
+++ b/src/sdl.h
 
 #if SDL_MAJOR_VERSION >= 1 && SDL_MINOR_VERSION >= 1
 #include "SDL_image.h"
+#include "SDL_mixer.h"
 #else
 #include "IMG.h"
+#include "mixer.h"
 #endif
 
 /* SDL type definitions */
index 7839902a50d21f24c7d2080035c3047cc11d6d6d..e89a153fd665db256c4306e1c231632227e83446 100644 (file)
@@ -715,18 +715,31 @@ boolean LoadSound(struct SoundInfo *snd_info)
 {
   char filename[256];
   char *sound_ext = "wav";
+#ifndef USE_SDL_LIBRARY
 #ifndef MSDOS
   byte sound_header_buffer[WAV_HEADER_SIZE];
   char chunk[CHUNK_ID_LEN + 1];
   int chunk_length, dummy;
   FILE *file;
   int i;
+#endif
 #endif
 
   sprintf(filename, "%s/%s/%s.%s",
          options.ro_base_directory, SOUNDS_DIRECTORY,
          snd_info->name, sound_ext);
 
+#ifdef USE_SDL_LIBRARY
+
+  snd_info->mix_chunk = Mix_LoadWAV(filename);
+  if (snd_info->mix_chunk == NULL)
+  {
+    Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename);
+    return FALSE;
+  }
+
+#else /* !USE_SDL_LIBRARY */
+
 #ifndef MSDOS
 
   if ((file = fopen(filename, "r")) == NULL)
@@ -789,12 +802,13 @@ boolean LoadSound(struct SoundInfo *snd_info)
   if (!snd_info->sample_ptr)
   {
     Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename);
-    return(FALSE);
+    return FALSE;
   }
 
 #endif /* MSDOS */
+#endif /* !USE_SDL_LIBRARY */
 
-  return(TRUE);
+  return TRUE;
 }
 
 void PlaySound(int nr)
@@ -816,6 +830,17 @@ void PlaySoundExt(int nr, int volume, int stereo, boolean loop)
 {
   struct SoundControl snd_ctrl = emptySoundControl;
 
+#ifdef USE_SDL_LIBRARY
+  Mix_PlayChannel(-1, Sound[nr].mix_chunk, 0);
+
+  /*
+  Mix_Volume(-1, SDL_MIX_MAXVOLUME / 4);
+  Mix_VolumeMusic(SDL_MIX_MAXVOLUME / 4);
+  */
+
+  return;
+#endif
+
   if (sound_status==SOUND_OFF || !setup.sound)
     return;
 
@@ -873,6 +898,11 @@ void StopSoundExt(int nr, int method)
 {
   struct SoundControl snd_ctrl = emptySoundControl;
 
+#ifdef USE_SDL_LIBRARY
+  Mix_HaltMusic();
+  return;
+#endif
+
   if (sound_status==SOUND_OFF)
     return;
 
index 2e934709e2ee041ebb0be6cc40c9aada059d4b84..dada0e89bcba1beefc78c3ed93163a1abc3725ab 100644 (file)
@@ -130,9 +130,14 @@ struct SoundInfo
   char *name;
   byte *data_ptr;
   long data_len;
+
 #ifdef MSDOS
   SAMPLE *sample_ptr;
 #endif
+
+#ifdef USE_SDL_LIBRARY
+  Mix_Chunk *mix_chunk;
+#endif
 };
 
 struct SoundControl
index 3c1823572889f7cc36416aaf537e798d4ba0feb5..6b85ccb38c219d7fe790031bdbf817a355c179d6 100644 (file)
@@ -120,20 +120,20 @@ inline void KeyboardAutoRepeatOff()
 #endif
 }
 
-inline boolean QueryPointer(DrawWindow window, int *win_x, int *win_y)
+inline boolean PointerInWindow(DrawWindow window)
 {
 #ifdef USE_SDL_LIBRARY
-  SDL_GetMouseState(win_x, win_y);
   return TRUE;
 #else
   DrawWindow root, child;
   int root_x, root_y;
   unsigned int mask;
+  int win_x, win_y;
 
   /* if XQueryPointer() returns False, the pointer
      is not on the same screen as the specified window */
   return XQueryPointer(display, window, &root, &child, &root_x, &root_y,
-                      win_x, win_y, &mask);
+                      &win_x, &win_y, &mask);
 #endif
 }
 
index 01f5afe8eef40ef80c8261e8bdbd1583c5004bab..3ccdbb6a17219235c0945763c752783fafbe2947 100644 (file)
@@ -45,7 +45,7 @@ inline void FlushDisplay();
 inline void SyncDisplay();
 inline void KeyboardAutoRepeatOn();
 inline void KeyboardAutoRepeatOff();
-inline boolean QueryPointer(DrawWindow, int *, int *);
+inline boolean PointerInWindow(DrawWindow);
 
 inline boolean PendingEvent();
 inline void NextEvent(Event *event);
index 3bf2c5a5ad9644c6587e4a4b1655528ff763a7e9..559e06bfb8f3e036f83840f7e0e350a8af3a3de6 100644 (file)
@@ -1888,9 +1888,7 @@ boolean Request(char *text, unsigned int req_state)
        {
          if (event.type == EVENT_MOTIONNOTIFY)
          {
-           int win_x, win_y;
-
-           if (!QueryPointer(window, &win_x, &win_y))
+           if (!PointerInWindow(window))
              continue; /* window and pointer are on different screens */
 
            if (!button_status)