1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
5 #include "DDScrollBuffer.h"
12 long mScrollX, mScrollY;
13 long mScrollX_last, mScrollY_last;
14 long mDestXOff, mDestYOff;
16 long ScreenBuffer[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
17 boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
20 void RestorePlayfield()
22 int x1 = mScrollX / TILEX - 2;
23 int y1 = mScrollY / TILEY - 2;
24 int x2 = mScrollX / TILEX + (SCR_FIELDX - 1) + 2;
25 int y2 = mScrollY / TILEY + (SCR_FIELDY - 1) + 2;
30 for (y = DisplayMinY; y <= DisplayMaxY; y++)
32 for (x = DisplayMinX; x <= DisplayMaxX; x++)
34 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
36 DrawFieldNoAnimated(x, y);
37 DrawFieldAnimated(x, y);
43 static void ScrollPlayfield(int dx, int dy)
45 int x1 = mScrollX_last / TILEX - 2;
46 int y1 = mScrollY_last / TILEY - 2;
47 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
48 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
51 BlitBitmap(screenBitmap, screenBitmap,
54 (MAX_BUF_XSIZE * TILEX) - TILEX * (dx != 0),
55 (MAX_BUF_YSIZE * TILEY) - TILEY * (dy != 0),
59 /* when scrolling the whole playfield, do not redraw single tiles */
60 for (x = 0; x < MAX_BUF_XSIZE; x++)
61 for (y = 0; y < MAX_BUF_YSIZE; y++)
69 for (y = DisplayMinY; y <= DisplayMaxY; y++)
71 for (x = DisplayMinX; x <= DisplayMaxX; x++)
73 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
77 int tsi = GetSI(x, y);
78 long id = ((PlayField16[tsi]) |
79 (PlayField8[tsi] << 16) |
80 (DisPlayField[tsi] << 24));
82 if ((dx == -1 && x == x2) ||
83 (dx == +1 && x == x1) ||
84 (dy == -1 && y == y2) ||
85 (dy == +1 && y == y1))
87 DrawFieldNoAnimated(x, y);
88 DrawFieldAnimated(x, y);
91 ScreenBuffer[sx][sy] = id;
97 static void ScrollPlayfieldIfNeededExt(boolean reset)
107 if (mScrollX_last == -1 || mScrollY_last == -1)
109 mScrollX_last = mScrollX;
110 mScrollY_last = mScrollY;
115 /* check if scrolling the playfield requires redrawing the viewport bitmap */
116 if ((mScrollX != mScrollX_last ||
117 mScrollY != mScrollY_last) &&
118 (ABS(mScrollX - mScrollX_last) >= TILEX ||
119 ABS(mScrollY - mScrollY_last) >= TILEY))
121 int dx = (ABS(mScrollX - mScrollX_last) < TILEX ? 0 :
122 mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
123 int dy = (ABS(mScrollY - mScrollY_last) < TILEY ? 0 :
124 mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
126 mScrollX_last -= dx * TILEX;
127 mScrollY_last -= dy * TILEY;
129 ScrollPlayfield(dx, dy);
133 static void ScrollPlayfieldIfNeeded()
135 ScrollPlayfieldIfNeededExt(FALSE);
138 void InitScrollPlayfield()
140 ScrollPlayfieldIfNeededExt(TRUE);
143 void UpdatePlayfield(boolean force_redraw)
150 for (y = DisplayMinY; y <= DisplayMaxY; y++)
152 for (x = DisplayMinX; x <= DisplayMaxX; x++)
154 int element = LowByte(PlayField16[GetSI(x, y)]);
155 int graphic = GfxGraphic[x][y];
156 int sync_frame = GfxFrame[x][y];
157 boolean redraw = force_redraw;
160 redraw = TRUE; // !!! TEST ONLY -- ALWAYS REDRAW !!!
165 GfxGraphicLast[x][y] = GfxGraphic[x][y];
170 if (element != GfxElementLast[x][y] &&
171 graphic == GfxGraphicLast[x][y])
173 /* element changed, but not graphic => disable updating graphic */
175 GfxElementLast[x][y] = element;
176 GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
181 if (graphic != GfxGraphicLast[x][y]) // new graphic
185 GfxElementLast[x][y] = element;
186 GfxGraphicLast[x][y] = GfxGraphic[x][y];
187 sync_frame = GfxFrame[x][y] = 0;
189 else if (isNextAnimationFrame_SP(graphic, sync_frame)) // new frame
196 int sx = x * StretchWidth;
197 int sy = y * StretchWidth;
200 printf("::: REDRAW (%d, %d): %d, %d\n", x, y, graphic, sync_frame);
203 StretchedSprites.BltImg(sx, sy, graphic, sync_frame);
213 printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
217 /* copy the entire screen to the window at the scroll position */
219 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
221 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
222 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
223 int sx, sy, sxsize, sysize;
227 int full_xsize = (FieldWidth - (menBorder.Checked ? 0 : 1)) * TILEX;
228 int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
230 sxsize = (full_xsize < xsize ? full_xsize : xsize);
231 sysize = (full_ysize < ysize ? full_ysize : ysize);
232 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
233 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
235 if (!menBorder.Checked)
241 BlitBitmap(screenBitmap, target_bitmap, px, py, sxsize, sysize, sx, sy);
244 void BackToFront_SP(void)
246 static boolean scrolling_last = FALSE;
247 int left = mScrollX / TILEX;
248 int top = mScrollY / TILEY;
249 boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
255 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
257 BlitScreenToBitmap_SP(window);
261 for (x = 0; x < SCR_FIELDX; x++)
263 for (y = 0; y < SCR_FIELDY; y++)
265 int xx = (left + x) % MAX_BUF_XSIZE;
266 int yy = (top + y) % MAX_BUF_YSIZE;
269 BlitBitmap(screenBitmap, window,
270 xx * TILEX, yy * TILEY, TILEX, TILEY,
271 SX + x * TILEX, SY + y * TILEY);
278 for (x = 0; x < MAX_BUF_XSIZE; x++)
279 for (y = 0; y < MAX_BUF_YSIZE; y++)
280 redraw[x][y] = FALSE;
283 scrolling_last = scrolling;
286 void DDScrollBuffer_Blt()
291 void DDScrollBuffer_ScrollTo(int X, int Y)
304 ScrollPlayfieldIfNeeded();
308 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
319 r = Sqr(dx * dx + dY * dY);
320 if (r == 0) // we are there already
328 mScrollX = mScrollX + dx * r;
329 mScrollY = mScrollY + dY * r;
334 ScrollPlayfieldIfNeeded();
338 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
343 long oldX, oldY, maxD;
344 static boolean AlreadyRunning = False;
354 AlreadyRunning = True;
359 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
360 StepCount = FPS * (TimeMS / (double)1000);
361 if (StepCount > maxD)
368 tStep = (double)1 / StepCount;
371 // R = Sqr(dX * dX + dY * dY)
372 // If R = 0 Then Exit Sub 'we are there already
373 for (T = (double)tStep; T <= (double)1; T += tStep)
378 mScrollX = oldX + T * dx;
379 mScrollY = oldY + T * dY;
393 AlreadyRunning = False;
396 ScrollPlayfieldIfNeeded();