1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
5 #include "DDScrollBuffer.h"
10 long mScrollX, mScrollY;
11 long mScrollX_last, mScrollY_last;
13 long ScreenBuffer[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
14 boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
17 void RestorePlayfield()
19 int x1 = mScrollX / TILEX - 2;
20 int y1 = mScrollY / TILEY - 2;
21 int x2 = mScrollX / TILEX + (SCR_FIELDX - 1) + 2;
22 int y2 = mScrollY / TILEY + (SCR_FIELDY - 1) + 2;
27 for (y = DisplayMinY; y <= DisplayMaxY; y++)
29 for (x = DisplayMinX; x <= DisplayMaxX; x++)
31 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
33 DrawFieldNoAnimated(x, y);
34 DrawFieldAnimated(x, y);
40 static void ScrollPlayfield(int dx, int dy)
42 int x1 = mScrollX_last / TILEX - 2;
43 int y1 = mScrollY_last / TILEY - 2;
44 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
45 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
48 BlitBitmap(screenBitmap, screenBitmap,
51 (MAX_BUF_XSIZE * TILEX) - TILEX * (dx != 0),
52 (MAX_BUF_YSIZE * TILEY) - TILEY * (dy != 0),
56 /* when scrolling the whole playfield, do not redraw single tiles */
57 for (x = 0; x < MAX_BUF_XSIZE; x++)
58 for (y = 0; y < MAX_BUF_YSIZE; y++)
64 for (y = DisplayMinY; y <= DisplayMaxY; y++)
66 for (x = DisplayMinX; x <= DisplayMaxX; x++)
68 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
72 int tsi = GetSI(x, y);
73 long id = ((PlayField16[tsi]) |
74 (PlayField8[tsi] << 16) |
75 (DisPlayField[tsi] << 24));
77 if ((dx == -1 && x == x2) ||
78 (dx == +1 && x == x1) ||
79 (dy == -1 && y == y2) ||
80 (dy == +1 && y == y1))
82 DrawFieldNoAnimated(x, y);
83 DrawFieldAnimated(x, y);
86 ScreenBuffer[sx][sy] = id;
92 static void ScrollPlayfieldIfNeededExt(boolean reset)
102 if (mScrollX_last == -1 || mScrollY_last == -1)
104 mScrollX_last = mScrollX;
105 mScrollY_last = mScrollY;
110 /* check if scrolling the playfield requires redrawing the viewport bitmap */
111 if ((mScrollX != mScrollX_last ||
112 mScrollY != mScrollY_last) &&
113 (ABS(mScrollX - mScrollX_last) >= TILEX ||
114 ABS(mScrollY - mScrollY_last) >= TILEY))
116 int dx = (ABS(mScrollX - mScrollX_last) < TILEX ? 0 :
117 mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
118 int dy = (ABS(mScrollY - mScrollY_last) < TILEY ? 0 :
119 mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
121 mScrollX_last -= dx * TILEX;
122 mScrollY_last -= dy * TILEY;
124 ScrollPlayfield(dx, dy);
128 static void ScrollPlayfieldIfNeeded()
130 ScrollPlayfieldIfNeededExt(FALSE);
133 void InitScrollPlayfield()
135 ScrollPlayfieldIfNeededExt(TRUE);
138 void UpdatePlayfield(boolean force_redraw)
145 for (y = DisplayMinY; y <= DisplayMaxY; y++)
147 for (x = DisplayMinX; x <= DisplayMaxX; x++)
149 int element = LowByte(PlayField16[GetSI(x, y)]);
150 int graphic = GfxGraphic[x][y];
151 int sync_frame = GfxFrame[x][y];
152 boolean redraw = force_redraw;
155 redraw = TRUE; // !!! TEST ONLY -- ALWAYS REDRAW !!!
160 GfxGraphicLast[x][y] = GfxGraphic[x][y];
165 if (element != GfxElementLast[x][y] &&
166 graphic == GfxGraphicLast[x][y])
168 /* element changed, but not graphic => disable updating graphic */
170 GfxElementLast[x][y] = element;
171 GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
176 if (graphic != GfxGraphicLast[x][y]) // new graphic
180 GfxElementLast[x][y] = element;
181 GfxGraphicLast[x][y] = GfxGraphic[x][y];
182 sync_frame = GfxFrame[x][y] = 0;
184 else if (isNextAnimationFrame_SP(graphic, sync_frame)) // new frame
191 int sx = x * StretchWidth;
192 int sy = y * StretchWidth;
195 printf("::: REDRAW (%d, %d): %d, %d\n", x, y, graphic, sync_frame);
198 DDSpriteBuffer_BltImg(sx, sy, graphic, sync_frame);
208 printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
212 /* copy the entire screen to the window at the scroll position */
214 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
216 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
217 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
218 int sx, sy, sxsize, sysize;
222 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX;
223 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY;
225 sxsize = (full_xsize < xsize ? full_xsize : xsize);
226 sysize = (full_ysize < ysize ? full_ysize : ysize);
227 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
228 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
236 BlitBitmap(screenBitmap, target_bitmap, px, py, sxsize, sysize, sx, sy);
239 void BackToFront_SP(void)
241 static boolean scrolling_last = FALSE;
242 int left = mScrollX / TILEX;
243 int top = mScrollY / TILEY;
244 boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
250 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
252 BlitScreenToBitmap_SP(window);
256 for (x = 0; x < SCR_FIELDX; x++)
258 for (y = 0; y < SCR_FIELDY; y++)
260 int xx = (left + x) % MAX_BUF_XSIZE;
261 int yy = (top + y) % MAX_BUF_YSIZE;
264 BlitBitmap(screenBitmap, window,
265 xx * TILEX, yy * TILEY, TILEX, TILEY,
266 SX + x * TILEX, SY + y * TILEY);
273 for (x = 0; x < MAX_BUF_XSIZE; x++)
274 for (y = 0; y < MAX_BUF_YSIZE; y++)
275 redraw[x][y] = FALSE;
278 scrolling_last = scrolling;
281 void DDScrollBuffer_ScrollTo(int X, int Y)
286 ScrollX = mScrollX = X;
287 ScrollY = mScrollY = Y;
289 ScrollPlayfieldIfNeeded();
292 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
302 r = Sqr(dx * dx + dY * dY);
303 if (r == 0) // we are there already
311 ScrollX = mScrollX = mScrollX + dx * r;
312 ScrollY = mScrollY = mScrollY + dY * r;
314 ScrollPlayfieldIfNeeded();
317 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
322 long oldX, oldY, maxD;
323 static boolean AlreadyRunning = False;
331 AlreadyRunning = True;
335 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
337 StepCount = FPS * (TimeMS / (double)1000);
338 if (StepCount > maxD)
345 tStep = (double)1 / StepCount;
349 for (T = (double)tStep; T <= (double)1; T += tStep)
351 ScrollX = mScrollX = oldX + T * dx;
352 ScrollY = mScrollY = oldY + T * dY;
355 ScrollX = mScrollX = X;
356 ScrollY = mScrollY = Y;
358 AlreadyRunning = False;
360 ScrollPlayfieldIfNeeded();