rnd-20001205-3-src
[rocksndiamonds.git] / src / events.c
index 85ae4d61f86c4da85ef41f9ce473d454b3918075..1e2ca16841661450886d719a2f701e0c8eea49be 100644 (file)
@@ -1,26 +1,26 @@
 /***********************************************************
-*  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
+* Rocks'n'Diamonds -- McDuffin Strikes Back!               *
 *----------------------------------------------------------*
-*  (c) 1995-98 Artsoft Entertainment                       *
-*              Holger Schemel                              *
-*              Oststrasse 11a                              *
-*              33604 Bielefeld                             *
-*              phone: ++49 +521 290471                     *
-*              email: aeglos@valinor.owl.de                *
+* (c) 1995-2000 Artsoft Entertainment                      *
+*               Holger Schemel                             *
+*               Detmolder Strasse 189                      *
+*               33604 Bielefeld                            *
+*               Germany                                    *
+*               e-mail: info@artsoft.org                   *
 *----------------------------------------------------------*
-*  events.c                                                *
+* events.c                                                 *
 ***********************************************************/
 
+#include "libgame/libgame.h"
+
 #include "events.h"
 #include "init.h"
 #include "screens.h"
 #include "tools.h"
 #include "game.h"
 #include "editor.h"
-#include "misc.h"
 #include "tape.h"
 #include "joystick.h"
-#include "buttons.h"
 #include "network.h"
 
 /* values for key_status */
 #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 FilterMouseMotionEvents(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,34 +55,39 @@ void EventLoop(void)
 
       NextEvent(&event);
 
-      switch(event.type)
+      if (FilterMouseMotionEvents(&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;
+       }
       }
     }
-
-    HandleNoXEvent();
+    else
+      HandleNoEvent();
 
     /* don't use all CPU time when idle; the main loop while playing
        has its own synchronization and is CPU friendly, too */
 
-    if (game_status != PLAYING)
+    if (game_status == PLAYING)
+      HandleGameActions();
+    else
     {
       SyncDisplay();
       if (!PendingEvent())     /* delay only if no pending events */
@@ -101,6 +123,14 @@ void HandleOtherEvents(Event *event)
       HandleClientMessageEvent((ClientMessageEvent *) event);
       break;
 
+#if defined(TARGET_SDL)
+    case SDL_JOYAXISMOTION:
+    case SDL_JOYBUTTONDOWN:
+    case SDL_JOYBUTTONUP:
+      HandleJoystickEvent(event);
+      break;
+#endif
+
     default:
       break;
   }
@@ -176,6 +206,7 @@ void SleepWhileUnmapped()
 
 void HandleExposeEvent(ExposeEvent *event)
 {
+#ifndef TARGET_SDL
   int x = event->x, y = event->y;
   int width = event->width, height = event->height;
 
@@ -209,6 +240,7 @@ void HandleExposeEvent(ExposeEvent *event)
   BlitBitmap(drawto, window, x,y, width,height, x,y);
 
   FlushDisplay();
+#endif
 }
 
 void HandleButtonEvent(ButtonEvent *event)
@@ -225,17 +257,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 1
+  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)
@@ -294,15 +326,8 @@ void HandleFocusEvent(FocusChangeEvent *event)
 
 void HandleClientMessageEvent(ClientMessageEvent *event)
 {
-#ifdef USE_SDL_LIBRARY
-  CloseAllAndExit(0);  /* the only possible message here is SDL_QUIT */
-#else
-#ifndef MSDOS
-  if ((event->window == window) &&
-      (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
+  if (CheckCloseWindowEvent(event))
     CloseAllAndExit(0);
-#endif
-#endif
 }
 
 void HandleButton(int mx, int my, int button)
@@ -330,7 +355,7 @@ void HandleButton(int mx, int my, int button)
       break;
 
     case TYPENAME:
-      HandleTypeName(0, KEY_Return);
+      HandleTypeName(0, KSYM_Return);
       break;
 
     case CHOOSELEVEL:
@@ -394,6 +419,7 @@ void HandleButton(int mx, int my, int button)
 void HandleKey(Key key, int key_status)
 {
   int joy = 0;
+  boolean anyTextGadgetActiveOrJustFinished = anyTextGadgetActive();
   static struct SetupKeyboardInfo custom_key;
   static struct
   {
@@ -459,7 +485,7 @@ void HandleKey(Key key, int key_status)
   if (key_status == KEY_RELEASED)
     return;
 
-  if ((key == KEY_Return || key == KEY_space) &&
+  if ((key == KSYM_Return || key == KSYM_space) &&
       game_status == PLAYING && AllPlayersGone)
   {
     CloseDoor(DOOR_CLOSE_1);
@@ -469,7 +495,7 @@ void HandleKey(Key key, int key_status)
   }
 
   /* allow quick escape to the main menu with the Escape key */
-  if (key == KEY_Escape && game_status != MAINMENU)
+  if (key == KSYM_Escape && game_status != MAINMENU)
   {
     CloseDoor(DOOR_CLOSE_1 | DOOR_OPEN_2 | DOOR_NO_DELAY);
     game_status = MAINMENU;
@@ -502,8 +528,8 @@ void HandleKey(Key key, int key_status)
     case SETUPINPUT:
       switch(key)
       {
-       case KEY_Return:
-       case KEY_space:
+       case KSYM_Return:
+       case KSYM_space:
          if (game_status == MAINMENU)
            HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
           else if (game_status == CHOOSELEVEL)
@@ -514,12 +540,12 @@ void HandleKey(Key key, int key_status)
            HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
          break;
 
-        case KEY_Page_Up:
+        case KSYM_Page_Up:
           if (game_status == CHOOSELEVEL)
             HandleChooseLevel(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
          break;
 
-        case KEY_Page_Down:
+        case KSYM_Page_Down:
           if (game_status == CHOOSELEVEL)
             HandleChooseLevel(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
          break;
@@ -536,18 +562,18 @@ void HandleKey(Key key, int key_status)
     case HALLOFFAME:
       switch(key)
       {
-       case KEY_Return:
-       case KEY_space:
+       case KSYM_Return:
+       case KSYM_space:
          game_status = MAINMENU;
          DrawMainMenu();
          BackToFront();
          break;
 
-        case KEY_Page_Up:
+        case KSYM_Page_Up:
          HandleHallOfFame(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
          break;
 
-        case KEY_Page_Down:
+        case KSYM_Page_Down:
          HandleHallOfFame(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
          break;
 
@@ -557,7 +583,8 @@ void HandleKey(Key key, int key_status)
       break;
 
     case LEVELED:
-      HandleLevelEditorKeyInput(key);
+      if (!anyTextGadgetActiveOrJustFinished)
+       HandleLevelEditorKeyInput(key);
       break;
 
     case PLAYING:
@@ -566,17 +593,17 @@ void HandleKey(Key key, int key_status)
       {
 
 #ifdef DEBUG
-       case KEY_0:
-       case KEY_1:
-       case KEY_2:
-       case KEY_3:
-       case KEY_4:
-       case KEY_5:
-       case KEY_6:
-       case KEY_7:
-       case KEY_8:
-       case KEY_9:
-         if (key == KEY_0)
+       case KSYM_0:
+       case KSYM_1:
+       case KSYM_2:
+       case KSYM_3:
+       case KSYM_4:
+       case KSYM_5:
+       case KSYM_6:
+       case KSYM_7:
+       case KSYM_8:
+       case KSYM_9:
+         if (key == KSYM_0)
          {
            if (GameFrameDelay == 500)
              GameFrameDelay = GAME_FRAME_DELAY;
@@ -584,14 +611,46 @@ void HandleKey(Key key, int key_status)
              GameFrameDelay = 500;
          }
          else
-           GameFrameDelay = (key - KEY_0) * 10;
+           GameFrameDelay = (key - KSYM_0) * 10;
          printf("Game speed == %d%% (%d ms delay between two frames)\n",
                 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
          break;
 
+       case KSYM_d:
+         if (options.debug)
+         {
+           options.debug = FALSE;
+           printf("debug mode disabled\n");
+         }
+         else
+         {
+           options.debug = TRUE;
+           printf("debug mode enabled\n");
+         }
+         break;
+
+       case KSYM_s:
+         if (!global.fps_slowdown)
+         {
+           global.fps_slowdown = TRUE;
+           global.fps_slowdown_factor = 2;
+           printf("fps slowdown enabled -- display only every 2nd frame\n");
+         }
+         else if (global.fps_slowdown_factor == 2)
+         {
+           global.fps_slowdown_factor = 4;
+           printf("fps slowdown enabled -- display only every 4th frame\n");
+         }
+         else
+         {
+           global.fps_slowdown = FALSE;
+           global.fps_slowdown_factor = 1;
+           printf("fps slowdown disabled\n");
+         }
+         break;
 
 #if 0
-       case KEY_a:
+       case KSYM_a:
          if (ScrollStepSize == TILEX/8)
            ScrollStepSize = TILEX/4;
          else
@@ -601,7 +660,7 @@ void HandleKey(Key key, int key_status)
 #endif
 
 #if 0
-       case KEY_m:
+       case KSYM_m:
          if (MoveSpeed == 8)
          {
            MoveSpeed = 4;
@@ -616,30 +675,28 @@ void HandleKey(Key key, int key_status)
          break;
 #endif
 
-       case KEY_f:
+       case KSYM_f:
          ScrollStepSize = TILEX/8;
          printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
          break;
 
-       case KEY_g:
+       case KSYM_g:
          ScrollStepSize = TILEX/4;
          printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
          break;
 
-       case KEY_h:
+       case KSYM_h:
          ScrollStepSize = TILEX/2;
          printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
          break;
 
-       case KEY_l:
+       case KSYM_l:
          ScrollStepSize = TILEX;
          printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
          break;
 
-#ifndef MSDOS
-       case KEY_Q:
-#endif
-       case KEY_q:
+       case KSYM_Q:
+       case KSYM_q:
          local_player->dynamite = 1000;
          break;
 
@@ -647,7 +704,7 @@ void HandleKey(Key key, int key_status)
 
 #if 0
 
-       case KEY_z:
+       case KSYM_z:
          {
            int i;
 
@@ -676,7 +733,7 @@ void HandleKey(Key key, int key_status)
   }
 }
 
-void HandleNoXEvent()
+void HandleNoEvent()
 {
   if (button_status && game_status != PLAYING)
   {
@@ -684,15 +741,12 @@ void HandleNoXEvent()
     return;
   }
 
-#ifndef MSDOS
+#if defined(PLATFORM_UNIX)
   if (options.network)
     HandleNetworking();
 #endif
 
   HandleJoystick();
-
-  if (game_status == PLAYING)
-    HandleGameActions();
 }
 
 static int HandleJoystickForAllPlayers()