fixed compiler warnings (after adding "-Wstrict-prototypes")
[rocksndiamonds.git] / src / game_sp / DDScrollBuffer.c
index 1734aa8b7ebadc7d91d6d43285efa9892c622e89..62257d0314c403d026e83152cec72c5999d31e95 100644 (file)
 #include <math.h>
 
 
-// --- VERSION 1.0 CLASS
-// --- BEGIN
-// ---   MultiUse = -1  'True  // True
-// ---   Persistable = 0  'NotPersistable  // NotPersistable
-// ---   DataBindingBehavior = 0  'vbNone  // vbNone
-// ---   DataSourceBehavior  = 0  'vbNone  // vbNone
-// ---   MTSTransactionMode  = 0  'NotAnMTSObject  // NotAnMTSObject
-// --- END
-
-static char *VB_Name = "DDScrollBuffer";
-static boolean VB_GlobalNameSpace = False;
-static boolean VB_Creatable = True;
-static boolean VB_PredeclaredId = False;
-static boolean VB_Exposed = False;
-// --- Option Explicit
-
-// needs reference to: DirectX7 for Visual Basic Type Library
-
-DirectDrawSurface7 Buffer;
-DirectDrawSurface7 mPrimary;
-long mWidth, mHeight;
-long mhWnd;
-long mScrollX, mScrollY;
-long mDestXOff, mDestYOff;
-
-void DDScrollBuffer_Let_DestXOff(long NewVal)
-{
-  mDestXOff = NewVal;
-}
+int mScrollX, mScrollY;
+int mScrollX_last, mScrollY_last;
 
-long DDScrollBuffer_Get_DestXOff()
-{
-  long DestXOff;
+int ScreenBuffer[2 + MAX_PLAYFIELD_WIDTH + 2][2 + MAX_PLAYFIELD_HEIGHT + 2];
 
-  DestXOff = mDestXOff;
 
-  return DestXOff;
-}
-
-void DDScrollBuffer_Let_DestYOff(long NewVal)
+int getFieldbufferOffsetX_SP(void)
 {
-  mDestYOff = NewVal;
-}
+  int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
 
-long DDScrollBuffer_Get_DestYOff()
-{
-  long DestYOff;
+  /* scroll correction for even number of visible tiles (half tile shifted) */
+  px += game_sp.scroll_xoffset;
 
-  DestYOff = mDestYOff;
+  if (ExplosionShakeMurphy != 0)
+    px += TILEX / 2 - GetSimpleRandom(TILEX + 1);
 
-  return DestYOff;
+  px = px * TILESIZE_VAR / TILESIZE;
+
+  return px;
 }
 
-DirectDrawSurface7 DDScrollBuffer_Get_Surface()
+int getFieldbufferOffsetY_SP(void)
 {
-  DirectDrawSurface7 Surface;
+  int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
+
+  /* scroll correction for even number of visible tiles (half tile shifted) */
+  py += game_sp.scroll_yoffset;
 
-  Surface = Buffer;
+  if (ExplosionShakeMurphy != 0)
+    py += TILEY / 2 - GetSimpleRandom(TILEX + 1);
 
-  return Surface;
+  py = py * TILESIZE_VAR / TILESIZE;
+
+  return py;
 }
 
-long DDScrollBuffer_Get_Width()
+void RestorePlayfield(void)
 {
-  long Width;
+  int x1 = mScrollX / TILEX - 2;
+  int y1 = mScrollY / TILEY - 2;
+  int x2 = mScrollX / TILEX + (SCR_FIELDX - 1) + 2;
+  int y2 = mScrollY / TILEY + (SCR_FIELDY - 1) + 2;
+  int x, y;
 
-  Width = mWidth;
+  DrawFrameIfNeeded();
 
-  return Width;
+  for (y = DisplayMinY; y <= DisplayMaxY; y++)
+  {
+    for (x = DisplayMinX; x <= DisplayMaxX; x++)
+    {
+      if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
+      {
+       DrawFieldNoAnimated(x, y);
+       DrawFieldAnimated(x, y);
+      }
+    }
+  }
 }
 
-int DDScrollBuffer_Get_Height()
+static void ScrollPlayfield(int dx, int dy)
 {
-  int Height;
-
-  Height = mHeight;
-
-  return Height;
+  int x1 = mScrollX_last / TILEX - 2;
+  int y1 = mScrollY_last / TILEY - 2;
+  int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
+  int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
+  int x, y;
+
+  BlitBitmap(bitmap_db_field_sp, bitmap_db_field_sp,
+             TILEX_VAR * (dx == -1),
+             TILEY_VAR * (dy == -1),
+             (MAX_BUF_XSIZE * TILEX_VAR) - TILEX_VAR * (dx != 0),
+             (MAX_BUF_YSIZE * TILEY_VAR) - TILEY_VAR * (dy != 0),
+             TILEX_VAR * (dx == 1),
+             TILEY_VAR * (dy == 1));
+
+  DrawFrameIfNeeded();
+
+  for (y = DisplayMinY; y <= DisplayMaxY; y++)
+  {
+    for (x = DisplayMinX; x <= DisplayMaxX; x++)
+    {
+      if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
+      {
+       int sx = x - x1;
+       int sy = y - y1;
+       int tsi = GetSI(x, y);
+       int id = ((PlayField16[tsi]) |
+                 (PlayField8[tsi] << 16) |
+                 (DisPlayField[tsi] << 24));
+
+       if ((dx == -1 && x == x2) ||
+           (dx == +1 && x == x1) ||
+           (dy == -1 && y == y2) ||
+           (dy == +1 && y == y1))
+       {
+         DrawFieldNoAnimated(x, y);
+         DrawFieldAnimated(x, y);
+       }
+
+       ScreenBuffer[sx][sy] = id;
+      }
+    }
+  }
 }
 
-long DDScrollBuffer_CreateAtSize(long Width, long Height, long hWndViewPort)
+static void ScrollPlayfieldIfNeededExt(boolean reset)
 {
-  long CreateAtSize;
+  if (reset)
+  {
+    mScrollX_last = -1;
+    mScrollY_last = -1;
 
-  DDSURFACEDESC2 SD;
+    return;
+  }
 
-  CreateAtSize = 0;
-  mhWnd = hWndViewPort;
-  // Create ScrollBuffer:
+  if (mScrollX_last == -1 || mScrollY_last == -1)
   {
-    SD.lFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
-    SD.ddsCaps.lCaps = DDSCAPS_VIDEOMEMORY;
-    // SD.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
-    SD.LWidth = Width;
-    SD.LHeight = Height;
-  }
+    mScrollX_last = (mScrollX / TILESIZE) * TILESIZE;
+    mScrollY_last = (mScrollY / TILESIZE) * TILESIZE;
 
-  // --- On Error Resume Next
-  Buffer = DDraw.CreateSurface(SD);
-  if (Err.Number != 0)
-    return CreateAtSize;
+    return;
+  }
 
-  // --- On Error GoTo 0
+  /* check if scrolling the playfield requires redrawing the viewport bitmap */
+  if ((mScrollX != mScrollX_last ||
+       mScrollY != mScrollY_last) &&
+      (ABS(mScrollX - mScrollX_last) >= TILEX ||
+       ABS(mScrollY - mScrollY_last) >= TILEY))
+  {
+    int dx = (ABS(mScrollX - mScrollX_last) < TILEX ? 0 :
+             mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
+    int dy = (ABS(mScrollY - mScrollY_last) < TILEY ? 0 :
+             mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
 
-  mWidth = Width;
-  mHeight = Height;
-  mScrollX = 0;
-  mScrollY = 0;
-  CreateAtSize = -1;
+    mScrollX_last -= dx * TILEX;
+    mScrollY_last -= dy * TILEY;
 
-  return CreateAtSize;
+    ScrollPlayfield(dx, dy);
+  }
 }
 
-void DDScrollBuffer_Cls(int BackColor)
+static void ScrollPlayfieldIfNeeded(void)
 {
-  RECT EmptyRect;
-
-  if (NoDisplayFlag)
-    return;
-
-  Buffer.BltColorFill(EmptyRect, BackColor);
+  ScrollPlayfieldIfNeededExt(FALSE);
 }
 
-void DDScrollBuffer_Blt()
+void InitScrollPlayfield(void)
 {
-  RECT DR, SR;
-  long tX, tY, L;
-  // RECT ERect;
-  // long Restore;
+  ScrollPlayfieldIfNeededExt(TRUE);
+}
 
-  if (NoDisplayFlag)
-    return;
+#define DEBUG_REDRAW   0
 
+void UpdatePlayfield(boolean force_redraw)
+{
+  int x, y;
 
-  // --- On Error GoTo BltEH
-  DirectX.GetWindowRect(mhWnd, DR);
-  // --- On Error GoTo 0
+#if DEBUG_REDRAW
+  int num_redrawn = 0;
+#endif
 
+  if (force_redraw)
   {
-    tX = (DR.right - DR.left) / Stretch;
-    tY = (DR.bottom - DR.top) / Stretch;
-  }
-  {
-    SR.left = mScrollX + mDestXOff;
-    SR.top = mScrollY + mDestYOff;
-    SR.right = SR.left + tX;
-    SR.bottom = SR.top + tY;
-    //    If mWidth < SR.right Then
-    //      SR.right = mWidth
-    //      DR.right = DR.left + Stretch * (SR.right - SR.left)
-    //    End If
-    //    If mHeight < SR.bottom Then
-    //      SR.bottom = mHeight
-    //      DR.bottom = DR.top + Stretch * (SR.bottom - SR.top)
-    //    End If
-    //    If (mScrollX + mDestXOff) < 0 Then
-    //      SR.left = 0
-    //      DR.left = DR.left - Stretch * (mScrollX + mDestXOff)
-    //    End If
-    //    If (mScrollY + mDestYOff) < 0 Then
-    //      SR.top = 0
-    //      DR.top = DR.top - Stretch * (mScrollY + mDestYOff)
-    //    End If
-  }
-  // DDraw.WaitForVerticalBlank DDWAITVB_BLOCKBEGIN, 0
-  if (IS_NOTHING(&Buffer, sizeof(Buffer)))
-    return;
+    // force re-initialization of graphics status variables
+    for (y = DisplayMinY; y <= DisplayMaxY; y++)
+      for (x = DisplayMinX; x <= DisplayMaxX; x++)
+       GfxGraphic[x][y] = -1;
 
-  if (IS_NOTHING(&PrimarySurface, sizeof(PrimarySurface)))
-    return;
+    // force complete playfield redraw
+    DisplayLevel();
+  }
 
-  L = PrimarySurface_Blt(DR, Buffer, SR, DDBLT_WAIT);
-  if (L != DD_OK)
+  for (y = DisplayMinY; y <= DisplayMaxY; y++)
   {
-    switch (L)
+    for (x = DisplayMinX; x <= DisplayMaxX; x++)
     {
-      case DDERR_GENERIC:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_INVALIDCLIPLIST:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_INVALIDOBJECT:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_INVALIDPARAMS:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_INVALIDRECT:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_NOALPHAHW:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_NOBLTHW:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_NOCLIPLIST:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_NODDROPSHW:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_NOMIRRORHW:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_NORASTEROPHW:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_NOROTATIONHW:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_NOSTRETCHHW:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_NOZBUFFERHW:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_SURFACEBUSY:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_SURFACELOST:
-        DDraw.RestoreAllSurfaces();
-        if (! PrimarySurface.isLost())
-        {
-          subDisplayLevel();
-          // Blt();
-        }
-
-        // RestorePrimarySurface
-        // ClipToWindow 0
-        break;
-
-      case DDERR_UNSUPPORTED:
-        Debug.Assert(False);
-        break;
-
-      case DDERR_WASSTILLDRAWING:
-        Debug.Assert(False);
-        break;
-
-      default:
-        Debug.Assert(False);
-        break;
+      int element = LowByte(PlayField16[GetSI(x, y)]);
+      int graphic = GfxGraphic[x][y];
+      int sync_frame = GfxFrame[x][y];
+      boolean redraw = force_redraw;
+
+      if (graphic < 0)
+      {
+       GfxGraphicLast[x][y] = GfxGraphic[x][y];
+
+       continue;
+      }
+
+      if (element != GfxElementLast[x][y] &&
+         graphic == GfxGraphicLast[x][y])
+      {
+       /* element changed, but not graphic => disable updating graphic */
+
+       GfxElementLast[x][y] = element;
+       GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
+
+       continue;
+      }
+
+      if (graphic != GfxGraphicLast[x][y])                     // new graphic
+      {
+       redraw = TRUE;
+
+       GfxElementLast[x][y] = element;
+       GfxGraphicLast[x][y] = GfxGraphic[x][y];
+       sync_frame = GfxFrame[x][y] = 0;
+      }
+      else if (isNextAnimationFrame_SP(graphic, sync_frame))   // new frame
+      {
+       redraw = TRUE;
+      }
+
+      if (redraw)
+      {
+       int sx = x * StretchWidth;
+       int sy = y * StretchWidth;
+
+       DDSpriteBuffer_BltImg(sx, sy, graphic, sync_frame);
+
+#if DEBUG_REDRAW
+       num_redrawn++;
+#endif
+      }
     }
   }
 
-#if 0
-  //  Buffer.UpdateOverlay SR, PrimarySurface, DR, DDOVER_SHOW
-  if (EditFlag)
-    FMark.RefreshMarker();
+#if DEBUG_REDRAW
+  printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
 #endif
+}
 
-  // BltEH:
+void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
+{
+  /* copy playfield buffer to target bitmap at scroll position */
+
+  int px = getFieldbufferOffsetX_SP();
+  int py = getFieldbufferOffsetY_SP();
+  int xsize = SXSIZE;
+  int ysize = SYSIZE;
+  int full_xsize = (FieldWidth  - (menBorder ? 0 : 1)) * TILEX_VAR;
+  int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY_VAR;
+  int sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
+  int sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
+  int sxsize = (full_xsize < xsize ? full_xsize : xsize);
+  int sysize = (full_ysize < ysize ? full_ysize : ysize);
+
+  BlitBitmap(bitmap_db_field_sp, target_bitmap, px, py, sxsize, sysize, sx, sy);
 }
 
 void DDScrollBuffer_ScrollTo(int X, int Y)
@@ -283,12 +261,10 @@ void DDScrollBuffer_ScrollTo(int X, int Y)
   if (NoDisplayFlag)
     return;
 
-  X = X / Stretch;
-  Y = Y / Stretch;
-  mScrollX = X;
-  mScrollY = Y;
-  ScrollX = mScrollX;
-  ScrollY = mScrollY;
+  ScrollX = mScrollX = X;
+  ScrollY = mScrollY = Y;
+
+  ScrollPlayfieldIfNeeded();
 }
 
 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
@@ -298,12 +274,11 @@ void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
   if (NoDisplayFlag)
     return;
 
-  X = X / Stretch;
-  Y = Y / Stretch;
   dx = X - mScrollX;
   dY = Y - mScrollY;
+
   r = Sqr(dx * dx + dY * dY);
-  if (r == 0) // we are there already
+  if (r == 0)  // we are there already
     return;
 
   if (Step < r)
@@ -311,35 +286,32 @@ void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
   else
     r = 1;
 
-  mScrollX = mScrollX + dx * r;
-  mScrollY = mScrollY + dY * r;
-  ScrollX = mScrollX;
-  ScrollY = mScrollY;
+  ScrollX = mScrollX = mScrollX + dx * r;
+  ScrollY = mScrollY = mScrollY + dY * r;
+
+  ScrollPlayfieldIfNeeded();
 }
 
-void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
+void DDScrollBuffer_SoftScrollTo(int X, int Y, int TimeMS, int FPS)
 {
   double dx, dY;
-  TickCountObject Tick;
-  long dT, StepCount;
+  int StepCount;
   double T, tStep;
-  long oldX, oldY, maxD;
-  boolean AlreadyRunning;
+  int oldX, oldY, maxD;
+  static boolean AlreadyRunning = False;
 
   if (NoDisplayFlag)
     return;
 
   if (AlreadyRunning)
-  {
     return;
-  }
 
   AlreadyRunning = True;
-  X = X / Stretch;
-  Y = Y / Stretch;
+
   dx = X - mScrollX;
   dY = Y - mScrollY;
-  maxD = (Abs(dx) < Abs(dY) ?  Abs(dY) :  Abs(dY));
+  maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
+
   StepCount = FPS * (TimeMS / (double)1000);
   if (StepCount > maxD)
     StepCount = maxD;
@@ -347,36 +319,20 @@ void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
   if (StepCount == 0)
     StepCount = 1;
 
-  dT = 1000 / FPS;
   tStep = (double)1 / StepCount;
   oldX = mScrollX;
   oldY = mScrollY;
-  // R = Sqr(dX * dX + dY * dY)
-  // If R = 0 Then Exit Sub 'we are there already
+
   for (T = (double)tStep; T <= (double)1; T += tStep)
   {
-    if (UserDragFlag)
-      goto SoftScrollEH;
-
-    // If Claim Then Exit For
-    Tick.DelayMS(dT, False);
-    mScrollX = oldX + T * dx;
-    mScrollY = oldY + T * dY;
-    ScrollX = mScrollX;
-    ScrollY = mScrollY;
-    // Blt();
+    ScrollX = mScrollX = oldX + T * dx;
+    ScrollY = mScrollY = oldY + T * dY;
   }
 
-  if (UserDragFlag)
-    goto SoftScrollEH;
-
-  Tick.DelayMS(dT, False);
-  mScrollX = X;
-  mScrollY = Y;
-  ScrollX = mScrollX;
-  ScrollY = mScrollY;
-  // Blt();
+  ScrollX = mScrollX = X;
+  ScrollY = mScrollY = Y;
 
-SoftScrollEH:
   AlreadyRunning = False;
+
+  ScrollPlayfieldIfNeeded();
 }