1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
5 #include "DDScrollBuffer.h"
10 long mScrollX, mScrollY;
11 long mScrollX_last, mScrollY_last;
14 long ScreenBuffer[2 + MAX_PLAYFIELD_WIDTH + 2][2 + MAX_PLAYFIELD_HEIGHT + 2];
15 boolean redraw[2 + MAX_PLAYFIELD_WIDTH + 2][2 + MAX_PLAYFIELD_HEIGHT + 2];
17 long ScreenBuffer[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
18 boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
22 void RestorePlayfield()
24 int x1 = mScrollX / TILEX - 2;
25 int y1 = mScrollY / TILEY - 2;
26 int x2 = mScrollX / TILEX + (SCR_FIELDX - 1) + 2;
27 int y2 = mScrollY / TILEY + (SCR_FIELDY - 1) + 2;
32 for (y = DisplayMinY; y <= DisplayMaxY; y++)
34 for (x = DisplayMinX; x <= DisplayMaxX; x++)
36 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
38 DrawFieldNoAnimated(x, y);
39 DrawFieldAnimated(x, y);
45 static void ScrollPlayfield(int dx, int dy)
47 int x1 = mScrollX_last / TILEX - 2;
48 int y1 = mScrollY_last / TILEY - 2;
49 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
50 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
53 BlitBitmap(bitmap_db_field_sp, bitmap_db_field_sp,
56 (MAX_BUF_XSIZE * TILEX) - TILEX * (dx != 0),
57 (MAX_BUF_YSIZE * TILEY) - TILEY * (dy != 0),
61 /* when scrolling the whole playfield, do not redraw single tiles */
62 for (x = 0; x < MAX_BUF_XSIZE; x++)
63 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 DDSpriteBuffer_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;
226 printf("::: %d, %d - %d, %d - %ld, %ld\n",
227 MurphyScreenXPos, MurphyScreenYPos,
228 ScreenScrollXPos, ScreenScrollYPos,
234 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX;
235 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY;
237 sxsize = (full_xsize < xsize ? full_xsize : xsize);
238 sysize = (full_ysize < ysize ? full_ysize : ysize);
239 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
240 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
245 if (mScrollX < ScrollMinX + TILEX / 2)
246 px += ScrollMinX + TILEX / 2 - mScrollX;
247 else if (mScrollX > ScrollMaxX - TILEX / 2)
248 px -= mScrollX - (ScrollMaxX - TILEX / 2);
249 if (mScrollY < ScrollMinY + TILEY / 2)
250 py += ScrollMinY + TILEY / 2 - mScrollY;
251 else if (mScrollY > ScrollMaxY - TILEY / 2)
252 py -= mScrollY - (ScrollMaxY - TILEY / 2);
262 BlitBitmap(bitmap_db_field_sp, target_bitmap, px, py, sxsize, sysize, sx, sy);
265 void BackToFront_SP(void)
267 static boolean scrolling_last = FALSE;
268 int left = mScrollX / TILEX;
269 int top = mScrollY / TILEY;
270 boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
276 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
278 BlitScreenToBitmap_SP(window);
282 for (x = 0; x < SCR_FIELDX; x++)
284 for (y = 0; y < SCR_FIELDY; y++)
286 int xx = (left + x) % MAX_BUF_XSIZE;
287 int yy = (top + y) % MAX_BUF_YSIZE;
290 BlitBitmap(bitmap_db_field_sp, window,
291 xx * TILEX, yy * TILEY, TILEX, TILEY,
292 SX + x * TILEX, SY + y * TILEY);
299 for (x = 0; x < MAX_BUF_XSIZE; x++)
300 for (y = 0; y < MAX_BUF_YSIZE; y++)
301 redraw[x][y] = FALSE;
304 scrolling_last = scrolling;
307 void DDScrollBuffer_ScrollTo(int X, int Y)
312 ScrollX = mScrollX = X;
313 ScrollY = mScrollY = Y;
315 ScrollPlayfieldIfNeeded();
318 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
328 r = Sqr(dx * dx + dY * dY);
329 if (r == 0) // we are there already
337 ScrollX = mScrollX = mScrollX + dx * r;
338 ScrollY = mScrollY = mScrollY + dY * r;
340 ScrollPlayfieldIfNeeded();
343 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
348 long oldX, oldY, maxD;
349 static boolean AlreadyRunning = False;
357 AlreadyRunning = True;
361 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
363 StepCount = FPS * (TimeMS / (double)1000);
364 if (StepCount > maxD)
371 tStep = (double)1 / StepCount;
375 for (T = (double)tStep; T <= (double)1; T += tStep)
377 ScrollX = mScrollX = oldX + T * dx;
378 ScrollY = mScrollY = oldY + T * dY;
381 ScrollX = mScrollX = X;
382 ScrollY = mScrollY = Y;
384 AlreadyRunning = False;
386 ScrollPlayfieldIfNeeded();