rnd-20100224-1-src
[rocksndiamonds.git] / src / game_sp / DDScrollBuffer.c
index fdc925f2bbbec6fcff23e67c17114340c7a38d5f..de5b237edceeea21b823462ed992fe30ca4e0943 100644 (file)
@@ -39,6 +39,28 @@ boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
 
 int TEST_flag = 0;
 
+void RestorePlayfield()
+{
+  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;
+
+  DrawFrameIfNeeded();
+
+  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);
+      }
+    }
+  }
+}
 
 static void ScrollPlayfield(int dx, int dy)
 {
@@ -129,6 +151,31 @@ static void ScrollPlayfieldIfNeededExt(boolean reset)
     return;
   }
 
+#if 1
+
+  /* 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);
+
+    mScrollX_last -= dx * TILEX;
+    mScrollY_last -= dy * TILEY;
+
+    ScrollPlayfield(dx, dy);
+
+#if 0
+    printf("::: %ld, %ld\n", mScrollX, mScrollY);
+#endif
+  }
+
+#else
+
   /* check if scrolling the playfield reached the destination tile position */
   if ((mScrollX != mScrollX_last || mScrollY != mScrollY_last) &&
       mScrollX % TILEX == 0 && mScrollY % TILEY == 0)
@@ -140,7 +187,13 @@ static void ScrollPlayfieldIfNeededExt(boolean reset)
     mScrollY_last = mScrollY;
 
     ScrollPlayfield(dx, dy);
+
+#if 0
+    printf("::: %ld, %ld\n", mScrollX, mScrollY);
+#endif
   }
+
+#endif
 }
 
 static void ScrollPlayfieldIfNeeded()
@@ -153,7 +206,81 @@ void InitScrollPlayfield()
   ScrollPlayfieldIfNeededExt(TRUE);
 }
 
-void UpdatePlayfield()
+void UpdatePlayfield(boolean force_redraw)
+{
+  int x, y;
+#if 1
+  int num_redrawn = 0;
+#endif
+
+  for (y = DisplayMinY; y <= DisplayMaxY; y++)
+  {
+    for (x = DisplayMinX; x <= DisplayMaxX; x++)
+    {
+      int element = LowByte(PlayField16[GetSI(x, y)]);
+      int graphic = GfxGraphic[x][y];
+      int sync_frame = GfxFrame[x][y];
+      boolean redraw = force_redraw;
+
+#if 0
+      redraw = TRUE;   // !!! TEST ONLY -- ALWAYS REDRAW !!!
+#endif
+
+      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;
+
+#if 0
+       printf("::: REDRAW (%d, %d): %d, %d\n", x, y, graphic, sync_frame);
+#endif
+
+       StretchedSprites.BltImg(sx, sy, graphic, sync_frame);
+
+#if 1
+       num_redrawn++;
+#endif
+      }
+    }
+  }
+
+#if 0
+  printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
+#endif
+}
+
+void UpdatePlayfield_TMP()
 {
   int x1 = mScrollX_last / TILEX - 2;
   int y1 = mScrollY_last / TILEY - 2;
@@ -284,6 +411,8 @@ int DDScrollBuffer_Get_Height()
   return Height;
 }
 
+#if 0
+
 long DDScrollBuffer_CreateAtSize(long Width, long Height, long hWndViewPort)
 {
   long CreateAtSize;
@@ -327,15 +456,28 @@ void DDScrollBuffer_Cls(int BackColor)
   Buffer.BltColorFill(EmptyRect, BackColor);
 }
 
+#endif
 
 /* copy the entire screen to the window at the scroll position */
 
 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
 {
+#if 0
+  int px = 2 * TILEX + mScrollX % TILEX;
+  int py = 2 * TILEY + mScrollY % TILEY;
+#else
   int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
   int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
+#endif
   int sx, sy, sxsize, sysize;
 
+#if 0
+  if (mScrollX % TILEX != (mScrollX - mScrollX_last) % TILEX ||
+      mScrollY % TILEY != (mScrollY - mScrollY_last) % TILEY)
+    printf("::: %ld, %ld / %ld, %ld\n",
+          mScrollX, mScrollY, mScrollX_last, mScrollY_last);
+#endif
+
 #if 1
   int xsize = SXSIZE;
   int ysize = SYSIZE;
@@ -510,8 +652,12 @@ void BackToFront_SP(void)
 
 void DDScrollBuffer_Blt_Ext(Bitmap *target_bitmap)
 {
-  RECT DR, SR;
+  MyRECT DR, SR;
+#if 1
+  long tX, tY;
+#else
   long tX, tY, L;
+#endif
   int sX, sY;
   // RECT ERect;
   // long Restore;
@@ -627,8 +773,12 @@ void DDScrollBuffer_Blt_Ext(Bitmap *target_bitmap)
   if (IS_NOTHING(&Buffer, sizeof(Buffer)))
     return;
 
+#if 0
   if (IS_NOTHING(&PrimarySurface, sizeof(PrimarySurface)))
     return;
+#endif
+
+#if 0
 
   L = PrimarySurface.Blt(DR, &Buffer, SR, DDBLT_WAIT);
   if (L != DD_OK)
@@ -697,6 +847,7 @@ void DDScrollBuffer_Blt_Ext(Bitmap *target_bitmap)
         break;
 #endif
 
+#if 0
       case DDERR_SURFACELOST:
         DDraw.RestoreAllSurfaces();
         if (! PrimarySurface.isLost())
@@ -708,6 +859,7 @@ void DDScrollBuffer_Blt_Ext(Bitmap *target_bitmap)
         // RestorePrimarySurface
         // ClipToWindow 0
         break;
+#endif
 
 #if 0
       case DDERR_UNSUPPORTED:
@@ -725,6 +877,8 @@ void DDScrollBuffer_Blt_Ext(Bitmap *target_bitmap)
     }
   }
 
+#endif
+
 #if 0
   //  Buffer.UpdateOverlay SR, PrimarySurface, DR, DDOVER_SHOW
   if (EditFlag)
@@ -781,8 +935,8 @@ void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
     return;
 
 #if 0
-  printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTowards(): (1) mScroll: %ld, %ld [%d, %d, %f]\n",
-        mScrollX, mScrollY, X, Y, Step);
+  printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTowards(): (1) mScroll: %ld, %ld [%d, %d, %f, %f]\n",
+        mScrollX, mScrollY, X, Y, Step, Stretch);
 #endif
 
   X = X / Stretch;
@@ -824,6 +978,14 @@ void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
   long oldX, oldY, maxD;
   static boolean AlreadyRunning = False;
 
+#if 0
+  printf(":a: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
+        mScrollX, mScrollY,
+        mScrollX_last, mScrollY_last,
+        ScreenScrollXPos, ScreenScrollYPos,
+        ScrollX, ScrollY);
+#endif
+
   if (NoDisplayFlag)
     return;
 
@@ -866,9 +1028,20 @@ void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
     mScrollY = oldY + T * dY;
     ScrollX = mScrollX;
     ScrollY = mScrollY;
-    // Blt();
+
+#if 0
+    Blt();
+#endif
   }
 
+#if 0
+  printf(":x: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
+        mScrollX, mScrollY,
+        mScrollX_last, mScrollY_last,
+        ScreenScrollXPos, ScreenScrollYPos,
+        ScrollX, ScrollY);
+#endif
+
   if (UserDragFlag)
     goto SoftScrollEH;
 
@@ -880,7 +1053,10 @@ void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
   mScrollY = Y;
   ScrollX = mScrollX;
   ScrollY = mScrollY;
-  // Blt();
+
+#if 0
+  Blt();
+#endif
 
 SoftScrollEH:
   AlreadyRunning = False;
@@ -890,7 +1066,23 @@ SoftScrollEH:
         mScrollX, mScrollY);
 #endif
 
+#if 0
+  printf(":y: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
+        mScrollX, mScrollY,
+        mScrollX_last, mScrollY_last,
+        ScreenScrollXPos, ScreenScrollYPos,
+        ScrollX, ScrollY);
+#endif
+
 #if 1
   ScrollPlayfieldIfNeeded();
 #endif
+
+#if 0
+  printf(":z: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
+        mScrollX, mScrollY,
+        mScrollX_last, mScrollY_last,
+        ScreenScrollXPos, ScreenScrollYPos,
+        ScrollX, ScrollY);
+#endif
 }