rnd-20021129-2-src
[rocksndiamonds.git] / src / tools.c
index b3dca8a0b8e2616e44d004232646fc46c8f38182..8ed62a521b36a2fbc2fefd7ceeadf38f710e9ae1 100644 (file)
@@ -32,7 +32,6 @@
 #define NUM_TOOL_BUTTONS       7
 
 /* forward declaration for internal use */
-static int getGraphicAnimationPhase(int, int, int);
 static void UnmapToolButtons();
 static void HandleToolButtons(struct GadgetInfo *);
 
@@ -384,6 +383,39 @@ void ClearWindow()
   redraw_mask |= REDRAW_FIELD;
 }
 
+static int getGraphicAnimationPhase(int frames, int delay, int mode)
+{
+  int phase;
+
+  if (mode & ANIM_PINGPONG)
+  {
+    int max_anim_frames = 2 * frames - 2;
+
+    phase = (FrameCounter % (delay * max_anim_frames)) / delay;
+    phase = (phase < frames ? phase : max_anim_frames - phase);
+  }
+  else
+    phase = (FrameCounter % (delay * frames)) / delay;
+
+  if (mode & ANIM_REVERSE)
+    phase = -phase;
+
+  return phase;
+}
+
+inline int getGraphicAnimationFrame(int graphic, int sync_frame)
+{
+  /* animation synchronized with global frame counter, not move position */
+  if (new_graphic_info[graphic].anim_global_sync || sync_frame < 0)
+    sync_frame = FrameCounter;
+
+  return getAnimationFrame(new_graphic_info[graphic].anim_frames,
+                          new_graphic_info[graphic].anim_delay,
+                          new_graphic_info[graphic].anim_mode,
+                          new_graphic_info[graphic].anim_start_frame,
+                          sync_frame);
+}
+
 void MarkTileDirty(int x, int y)
 {
   int xx = redraw_x1 + x;
@@ -700,18 +732,23 @@ void DrawPlayer(struct PlayerInfo *player)
 
   if (player_is_moving && last_element == EL_EXPLOSION)
   {
-    int frame = Frame[last_jx][last_jy];
-    int delay = 2;
+    int stored = Store[last_jx][last_jy];
+    int graphic = (game.emulation != EMU_SUPAPLEX ? IMG_EXPLOSION :
+                  stored == EL_SP_INFOTRON ? IMG_SP_EXPLOSION_INFOTRON :
+                  IMG_SP_EXPLOSION);
+    int delay = (game.emulation == EMU_SUPAPLEX ? 3 : 2);
+    int phase = Frame[last_jx][last_jy] - 1;
+    int frame = getGraphicAnimationFrame(graphic, phase - delay);
 
-    if (frame > 2)
-      DrawGraphicThruMask(SCREENX(last_jx), SCREENY(last_jy), GFX_EXPLOSION,
-                         ((frame - 1) / delay - 1));
+    if (phase >= delay)
+      DrawGraphicThruMask(SCREENX(last_jx), SCREENY(last_jy), graphic, frame);
   }
 
   /* draw elements that stay over the player */
   /* handle the field the player is leaving ... */
   if (player_is_moving && IS_OVER_PLAYER(last_element))
     DrawLevelField(last_jx, last_jy);
+
   /* ... and the field the player is entering */
   if (IS_OVER_PLAYER(element))
     DrawLevelField(jx, jy);
@@ -731,98 +768,19 @@ void DrawPlayer(struct PlayerInfo *player)
   MarkTileDirty(sx,sy);
 }
 
-static int getGraphicAnimationPhase(int frames, int delay, int mode)
-{
-  int phase;
-
-  if (mode & ANIM_PINGPONG)
-  {
-    int max_anim_frames = 2 * frames - 2;
-
-    phase = (FrameCounter % (delay * max_anim_frames)) / delay;
-    phase = (phase < frames ? phase : max_anim_frames - phase);
-  }
-  else
-    phase = (FrameCounter % (delay * frames)) / delay;
-
-  if (mode & ANIM_REVERSE)
-    phase = -phase;
-
-  return phase;
-}
-
-int getGraphicAnimationFrame(int graphic, int sync_frame)
-{
-  int num_frames = new_graphic_info[graphic].anim_frames;
-  int delay = new_graphic_info[graphic].anim_delay;
-  int mode = new_graphic_info[graphic].anim_mode;
-  int frame = 0;
-
-  /* animation synchronized with global frame counter, not move position */
-  if (new_graphic_info[graphic].anim_global_sync || sync_frame < 0)
-    sync_frame = FrameCounter;
-
-  sync_frame += new_graphic_info[graphic].anim_start_frame * delay;
-
-  if (mode & ANIM_LOOP)                        /* normal, looping animation */
-  {
-    frame = (sync_frame % (delay * num_frames)) / delay;
-  }
-  else if (mode & ANIM_LINEAR)         /* normal, non-looping animation */
-  {
-    frame = sync_frame / delay;
-
-    if (frame > num_frames - 1)
-      frame = num_frames - 1;
-  }
-  else if (mode & ANIM_PINGPONG)       /* use border frames once */
-  {
-    int max_anim_frames = 2 * num_frames - 2;
-
-    frame = (sync_frame % (delay * max_anim_frames)) / delay;
-    frame = (frame < num_frames ? frame : max_anim_frames - frame);
-  }
-  else if (mode & ANIM_PINGPONG2)      /* use border frames twice */
-  {
-    int max_anim_frames = 2 * num_frames;
-
-    frame = (sync_frame % (delay * max_anim_frames)) / delay;
-    frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
-  }
-
-  if (mode & ANIM_REVERSE)             /* use reverse animation direction */
-    frame = num_frames - frame - 1;
-
-  return frame;
-}
-
 void DrawGraphicAnimationExt(int x, int y, int graphic, int mask_mode)
 {
-#if 0
-  int delay = new_graphic_info[graphic].anim_delay;
-
-  if (!(FrameCounter % delay) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
-#else
-  if (IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
-#endif
+  if (IN_SCR_FIELD(x, y))
   {
     int frame = getGraphicAnimationFrame(graphic, -1);
 
     if (mask_mode == USE_MASKING)
-      DrawGraphicThruMask(SCREENX(x), SCREENY(y), graphic, frame);
+      DrawGraphicThruMask(x, y, graphic, frame);
     else
-      DrawGraphic(SCREENX(x), SCREENY(y), graphic, frame);
+      DrawGraphic(x, y, graphic, frame);
   }
 }
 
-#if 0
-void DrawOldGraphicAnimation(int x, int y, int graphic,
-                            int frames, int delay, int mode)
-{
-  DrawGraphicAnimationExt(x, y, graphic, frames, delay, mode, NO_MASKING);
-}
-#endif
-
 void DrawGraphicAnimation(int x, int y, int graphic)
 {
   DrawGraphicAnimationExt(x, y, graphic, NO_MASKING);
@@ -841,54 +799,67 @@ void getOldGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
           graphic <= GFX_END_ROCKSELEMENTS)
   {
     graphic -= GFX_START_ROCKSELEMENTS;
-    *bitmap = pix[PIX_ELEMENTS];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_ELEMENTS].bitmap;
     *x = (graphic % GFX_PER_LINE) * TILEX;
     *y = (graphic / GFX_PER_LINE) * TILEY;
   }
   else if (graphic >= GFX_START_ROCKSHEROES && graphic <= GFX_END_ROCKSHEROES)
   {
     graphic -= GFX_START_ROCKSHEROES;
-    *bitmap = pix[PIX_HEROES];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_HEROES].bitmap;
     *x = (graphic % HEROES_PER_LINE) * TILEX;
     *y = (graphic / HEROES_PER_LINE) * TILEY;
   }
   else if (graphic >= GFX_START_ROCKSSP && graphic <= GFX_END_ROCKSSP)
   {
     graphic -= GFX_START_ROCKSSP;
-    *bitmap = pix[PIX_SP];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_SP].bitmap;
     *x = (graphic % SP_PER_LINE) * TILEX;
     *y = (graphic / SP_PER_LINE) * TILEY;
   }
   else if (graphic >= GFX_START_ROCKSDC && graphic <= GFX_END_ROCKSDC)
   {
     graphic -= GFX_START_ROCKSDC;
-    *bitmap = pix[PIX_DC];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_DC].bitmap;
     *x = (graphic % DC_PER_LINE) * TILEX;
     *y = (graphic / DC_PER_LINE) * TILEY;
   }
   else if (graphic >= GFX_START_ROCKSMORE && graphic <= GFX_END_ROCKSMORE)
   {
     graphic -= GFX_START_ROCKSMORE;
-    *bitmap = pix[PIX_MORE];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_MORE].bitmap;
     *x = (graphic % MORE_PER_LINE) * TILEX;
     *y = (graphic / MORE_PER_LINE) * TILEY;
   }
   else if (graphic >= GFX_START_ROCKSFONT && graphic <= GFX_END_ROCKSFONT)
   {
     graphic -= GFX_START_ROCKSFONT;
-    *bitmap = pix[PIX_FONT_EM];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_FONT_EM].bitmap;
     *x = (graphic % FONT_CHARS_PER_LINE) * TILEX;
     *y = (graphic / FONT_CHARS_PER_LINE) * TILEY;
   }
   else
   {
-    *bitmap = pix[PIX_SP];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_SP].bitmap;
     *x = 0;
     *y = 0;
   }
 }
 #endif
 
+void getGraphicSource(int graphic, int frame, Bitmap **bitmap, int *x, int *y)
+{
+  Bitmap *src_bitmap = new_graphic_info[graphic].bitmap;
+  int offset_x = new_graphic_info[graphic].offset_x;
+  int offset_y = new_graphic_info[graphic].offset_y;
+  int src_x = new_graphic_info[graphic].src_x + frame * offset_x;
+  int src_y = new_graphic_info[graphic].src_y + frame * offset_y;
+
+  *bitmap = src_bitmap;
+  *x = src_x;
+  *y = src_y;
+}
+
 void DrawGraphic(int x, int y, int graphic, int frame)
 {
 #if DEBUG
@@ -904,6 +875,7 @@ void DrawGraphic(int x, int y, int graphic, int frame)
   MarkTileDirty(x, y);
 }
 
+#if 1
 void DrawOldGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic)
 {
   Bitmap *src_bitmap;
@@ -912,10 +884,17 @@ void DrawOldGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic)
   getOldGraphicSource(graphic, &src_bitmap, &src_x, &src_y);
   BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, TILEX, TILEY, x, y);
 }
+#endif
 
 void DrawGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic,
                    int frame)
 {
+#if 1
+  Bitmap *src_bitmap;
+  int src_x, src_y;
+
+  getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
+#else
   Bitmap *src_bitmap = new_graphic_info[graphic].bitmap;
   int src_x = new_graphic_info[graphic].src_x;
   int src_y = new_graphic_info[graphic].src_y;
@@ -924,6 +903,7 @@ void DrawGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic,
 
   src_x += frame * offset_x;
   src_y += frame * offset_y;
+#endif
 
   BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, TILEX, TILEY, x, y);
 }
@@ -947,8 +927,16 @@ void DrawGraphicThruMask(int x, int y, int graphic, int frame)
 void DrawGraphicThruMaskExt(DrawBuffer *d, int dest_x, int dest_y, int graphic,
                            int frame)
 {
-  Bitmap *src_bitmap = new_graphic_info[graphic].bitmap;
+#if 1
+  Bitmap *src_bitmap;
+  int src_x, src_y;
+  GC drawing_gc;
+
+  getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
+  drawing_gc = src_bitmap->stored_clip_gc;
+#else
   GC drawing_gc = src_bitmap->stored_clip_gc;
+  Bitmap *src_bitmap = new_graphic_info[graphic].bitmap;
   int src_x = new_graphic_info[graphic].src_x;
   int src_y = new_graphic_info[graphic].src_y;
   int offset_x = new_graphic_info[graphic].offset_x;
@@ -957,6 +945,8 @@ void DrawGraphicThruMaskExt(DrawBuffer *d, int dest_x, int dest_y, int graphic,
   src_x += frame * offset_x;
   src_y += frame * offset_y;
 
+#endif
+
   SetClipOrigin(src_bitmap, drawing_gc, dest_x - src_x, dest_y - src_y);
   BlitBitmapMasked(src_bitmap, d, src_x, src_y, TILEX, TILEY, dest_x, dest_y);
 }
@@ -1271,8 +1261,9 @@ void DrawScreenElementExt(int x, int y, int dx, int dy, int element,
 
 inline static int getFramePosition(int x, int y)
 {
+  int frame_pos = -1;          /* default: global synchronization */
+#if 0
   int element = Feld[x][y];
-  int frame_pos = -1;
 
   if (element == EL_QUICKSAND_FULL ||
       element == EL_MAGIC_WALL_FULL ||
@@ -1280,6 +1271,9 @@ inline static int getFramePosition(int x, int y)
     frame_pos = -1;
   else if (IS_MOVING(x, y) || CAN_MOVE(element) || CAN_FALL(element))
     frame_pos = ABS(MovPos[x][y]) / (TILEX / 8);
+#else
+  frame_pos = ABS(MovPos[x][y]) / (TILEX / 8);
+#endif
 
   return frame_pos;
 }
@@ -1288,10 +1282,14 @@ inline static int getGfxAction(int x, int y)
 {
   int gfx_action = GFX_ACTION_DEFAULT;
 
+#if 0
   if (GfxAction[x][y] != GFX_ACTION_DEFAULT)
     gfx_action = GfxAction[x][y];
   else if (IS_MOVING(x, y))
     gfx_action = GFX_ACTION_MOVING;
+#else
+  gfx_action = GfxAction[x][y];
+#endif
 
   return gfx_action;
 }
@@ -1328,32 +1326,6 @@ void DrawScreenElementExt(int x, int y, int dx, int dy, int element,
       frame = new_graphic_info[graphic].anim_frames - 1;
     }
   }
-#if 0
-  else if ((element == EL_ROCK ||
-           element == EL_SP_ZONK ||
-           element == EL_BD_ROCK ||
-           element == EL_SP_INFOTRON ||
-           IS_GEM(element))
-          && !cut_mode)
-  {
-    if (uy >= lev_fieldy-1 || !IS_BELT(Feld[ux][uy+1]))
-    {
-      if (element == EL_ROCK ||
-         element == EL_SP_ZONK ||
-         element == EL_BD_ROCK)
-      {
-       if (move_dir == MV_LEFT)
-         graphic += (4 - phase4) % 4;
-       else if (move_dir == MV_RIGHT)
-         graphic += phase4;
-       else
-         graphic += phase2 * 2;
-      }
-      else if (element != EL_SP_INFOTRON)
-       graphic += phase2;
-    }
-  }
-#endif
   else if (IS_AMOEBOID(element) || element == EL_AMOEBA_DRIPPING)
   {
     graphic = (element == EL_BD_AMOEBA ? IMG_BD_AMOEBA_PART1 :
@@ -1948,7 +1920,7 @@ boolean Request(char *text, unsigned int req_state)
   CloseDoor(DOOR_CLOSE_1);
 
   /* save old door content */
-  BlitBitmap(pix[PIX_DB_DOOR], pix[PIX_DB_DOOR],
+  BlitBitmap(bitmap_db_door, bitmap_db_door,
             DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE,
             DOOR_GFX_PAGEX2, DOOR_GFX_PAGEY1);
 
@@ -2005,7 +1977,7 @@ boolean Request(char *text, unsigned int req_state)
   }
 
   /* copy request gadgets to door backbuffer */
-  BlitBitmap(drawto, pix[PIX_DB_DOOR],
+  BlitBitmap(drawto, bitmap_db_door,
             DX, DY, DXSIZE, DYSIZE,
             DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
 
@@ -2151,7 +2123,7 @@ boolean Request(char *text, unsigned int req_state)
 
     if (!(req_state & REQ_STAY_CLOSED) && (old_door_state & DOOR_OPEN_1))
     {
-      BlitBitmap(pix[PIX_DB_DOOR], pix[PIX_DB_DOOR],
+      BlitBitmap(bitmap_db_door, bitmap_db_door,
                 DOOR_GFX_PAGEX2,DOOR_GFX_PAGEY1, DXSIZE,DYSIZE,
                 DOOR_GFX_PAGEX1,DOOR_GFX_PAGEY1);
       OpenDoor(DOOR_OPEN_1);
@@ -2177,7 +2149,7 @@ unsigned int OpenDoor(unsigned int door_state)
 
   if (door_state & DOOR_COPY_BACK)
   {
-    BlitBitmap(pix[PIX_DB_DOOR], pix[PIX_DB_DOOR],
+    BlitBitmap(bitmap_db_door, bitmap_db_door,
               DOOR_GFX_PAGEX2, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE + VYSIZE,
               DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
     door_state &= ~DOOR_COPY_BACK;
@@ -2192,9 +2164,9 @@ unsigned int CloseDoor(unsigned int door_state)
 {
   unsigned int new_door_state;
 
-  BlitBitmap(backbuffer, pix[PIX_DB_DOOR],
+  BlitBitmap(backbuffer, bitmap_db_door,
             DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
-  BlitBitmap(backbuffer, pix[PIX_DB_DOOR],
+  BlitBitmap(backbuffer, bitmap_db_door,
             VX, VY, VXSIZE, VYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2);
 
   new_door_state = MoveDoor(door_state);
@@ -2265,7 +2237,7 @@ unsigned int MoveDoor(unsigned int door_state)
 
     for(x=start; x<=DXSIZE; x+=stepsize)
     {
-      Bitmap *bitmap = pix[PIX_DOOR];
+      Bitmap *bitmap = new_graphic_info[IMG_MENU_DOOR].bitmap;
       GC gc = bitmap->stored_clip_gc;
 
       WaitUntilDelayReached(&door_delay, door_delay_value);
@@ -2275,7 +2247,7 @@ unsigned int MoveDoor(unsigned int door_state)
        int i = (door_state & DOOR_OPEN_1 ? DXSIZE-x : x);
        int j = (DXSIZE - i) / 3;
 
-       BlitBitmap(pix[PIX_DB_DOOR], drawto,
+       BlitBitmap(bitmap_db_door, drawto,
                   DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1 + i/2,
                   DXSIZE,DYSIZE - i/2, DX, DY);
 
@@ -2318,7 +2290,7 @@ unsigned int MoveDoor(unsigned int door_state)
        int i = (door_state & DOOR_OPEN_2 ? VXSIZE - x : x);
        int j = (VXSIZE - i) / 3;
 
-       BlitBitmap(pix[PIX_DB_DOOR], drawto,
+       BlitBitmap(bitmap_db_door, drawto,
                   DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2 + i/2,
                   VXSIZE, VYSIZE - i/2, VX, VY);
 
@@ -2370,7 +2342,7 @@ unsigned int MoveDoor(unsigned int door_state)
 void DrawSpecialEditorDoor()
 {
   /* draw bigger toolbox window */
-  BlitBitmap(pix[PIX_DOOR], drawto,
+  BlitBitmap(new_graphic_info[IMG_MENU_DOOR].bitmap, drawto,
             DOOR_GFX_PAGEX7, 0, 108, 56, EX - 4, EY - 12);
 
   redraw_mask |= REDRAW_ALL;
@@ -2379,7 +2351,7 @@ void DrawSpecialEditorDoor()
 void UndrawSpecialEditorDoor()
 {
   /* draw normal tape recorder window */
-  BlitBitmap(pix[PIX_BACK], drawto,
+  BlitBitmap(new_graphic_info[IMG_MENU_BACK].bitmap, drawto,
             562, 344, 108, 56, EX - 4, EY - 12);
 
   redraw_mask |= REDRAW_ALL;
@@ -2508,7 +2480,7 @@ void CreateToolButtons()
 
   for (i=0; i<NUM_TOOL_BUTTONS; i++)
   {
-    Bitmap *gd_bitmap = pix[PIX_DOOR];
+    Bitmap *gd_bitmap = new_graphic_info[IMG_MENU_DOOR].bitmap;
     Bitmap *deco_bitmap = None;
     int deco_x = 0, deco_y = 0, deco_xpos = 0, deco_ypos = 0;
     struct GadgetInfo *gi;
@@ -2857,7 +2829,6 @@ int el2gfx_OLD(int element)
     case EL_TUBE_RIGHT_UP:             return GFX_TUBE_RIGHT_UP;
     case EL_TUBE_RIGHT_DOWN:           return GFX_TUBE_RIGHT_DOWN;
     case EL_SPRING:                    return GFX_SPRING;
-    case EL_SPRING_MOVING:             return GFX_SPRING;
     case EL_TRAP:                      return GFX_TRAP_INACTIVE;
     case EL_TRAP_ACTIVE:               return GFX_TRAP_ACTIVE;
     case EL_BD_WALL:                   return GFX_BD_WALL;
@@ -2916,30 +2887,15 @@ int el2gfx(int element)
 
 int el2img(int element)
 {
-#if 1
-  int graphic_NEW = element_info[element].graphic[GFX_ACTION_DEFAULT];
+  int graphic = element_info[element].graphic[GFX_ACTION_DEFAULT];
 
 #if DEBUG
-  if (graphic_NEW < 0)
+  if (graphic < 0)
     Error(ERR_WARN, "element %d -> graphic %d -- probably crashing now...",
-         element, graphic_NEW);
+         element, graphic);
 #endif
 
-  return graphic_NEW;
-#else
-
-  switch(element)
-  {
-    case EL_BD_BUTTERFLY:      return IMG_BD_BUTTERFLY;
-    case EL_BD_FIREFLY:                return IMG_BD_FIREFLY;
-    case EL_SP_ELECTRON:       return IMG_SP_ELECTRON;
-
-    default:
-      break;
-  }
-
-  return IMG_EMPTY;
-#endif
+  return graphic;
 }
 
 int el_dir2img(int element, int direction)