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)
110 mScrollX_last = (mScrollX / TILESIZE) * TILESIZE;
111 mScrollY_last = (mScrollY / TILESIZE) * TILESIZE;
113 mScrollX_last = mScrollX;
114 mScrollY_last = mScrollY;
120 /* check if scrolling the playfield requires redrawing the viewport bitmap */
121 if ((mScrollX != mScrollX_last ||
122 mScrollY != mScrollY_last) &&
123 (ABS(mScrollX - mScrollX_last) >= TILEX ||
124 ABS(mScrollY - mScrollY_last) >= TILEY))
126 int dx = (ABS(mScrollX - mScrollX_last) < TILEX ? 0 :
127 mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
128 int dy = (ABS(mScrollY - mScrollY_last) < TILEY ? 0 :
129 mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
131 mScrollX_last -= dx * TILEX;
132 mScrollY_last -= dy * TILEY;
134 ScrollPlayfield(dx, dy);
138 static void ScrollPlayfieldIfNeeded()
140 ScrollPlayfieldIfNeededExt(FALSE);
143 void InitScrollPlayfield()
145 ScrollPlayfieldIfNeededExt(TRUE);
148 void UpdatePlayfield(boolean force_redraw)
155 for (y = DisplayMinY; y <= DisplayMaxY; y++)
157 for (x = DisplayMinX; x <= DisplayMaxX; x++)
159 int element = LowByte(PlayField16[GetSI(x, y)]);
160 int graphic = GfxGraphic[x][y];
161 int sync_frame = GfxFrame[x][y];
162 boolean redraw = force_redraw;
165 redraw = TRUE; // !!! TEST ONLY -- ALWAYS REDRAW !!!
170 GfxGraphicLast[x][y] = GfxGraphic[x][y];
175 if (element != GfxElementLast[x][y] &&
176 graphic == GfxGraphicLast[x][y])
178 /* element changed, but not graphic => disable updating graphic */
180 GfxElementLast[x][y] = element;
181 GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
186 if (graphic != GfxGraphicLast[x][y]) // new graphic
190 GfxElementLast[x][y] = element;
191 GfxGraphicLast[x][y] = GfxGraphic[x][y];
192 sync_frame = GfxFrame[x][y] = 0;
194 else if (isNextAnimationFrame_SP(graphic, sync_frame)) // new frame
201 int sx = x * StretchWidth;
202 int sy = y * StretchWidth;
205 printf("::: REDRAW (%d, %d): %d, %d\n", x, y, graphic, sync_frame);
208 DDSpriteBuffer_BltImg(sx, sy, graphic, sync_frame);
218 printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
222 /* copy the entire screen to the window at the scroll position */
224 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
226 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
227 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
228 int sx, sy, sxsize, sysize;
231 printf("::: %d, %d / %d, %d / %ld, %ld (%ld, %ld) / %d, %d\n",
232 MurphyScreenXPos, MurphyScreenYPos,
233 ScreenScrollXPos, ScreenScrollYPos,
235 mScrollX_last, mScrollY_last,
241 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX;
242 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY;
244 sxsize = (full_xsize < xsize ? full_xsize : xsize);
245 sysize = (full_ysize < ysize ? full_ysize : ysize);
246 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
247 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
249 /* scroll correction for even number of visible tiles (half tile shifted) */
250 px += game_sp.scroll_xoffset;
251 py += game_sp.scroll_yoffset;
254 if (ExplosionShakeMurphy != 0)
256 px += TILEX / 2 - GetSimpleRandom(TILEX + 1);
257 py += TILEY / 2 - GetSimpleRandom(TILEX + 1);
261 BlitBitmap(bitmap_db_field_sp, target_bitmap, px, py, sxsize, sysize, sx, sy);
264 void BackToFront_SP(void)
266 static boolean scrolling_last = FALSE;
267 int left = mScrollX / TILEX;
268 int top = mScrollY / TILEY;
269 boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
275 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
277 BlitScreenToBitmap_SP(window);
281 for (x = 0; x < SCR_FIELDX; x++)
283 for (y = 0; y < SCR_FIELDY; y++)
285 int xx = (left + x) % MAX_BUF_XSIZE;
286 int yy = (top + y) % MAX_BUF_YSIZE;
289 BlitBitmap(bitmap_db_field_sp, window,
290 xx * TILEX, yy * TILEY, TILEX, TILEY,
291 SX + x * TILEX, SY + y * TILEY);
298 for (x = 0; x < MAX_BUF_XSIZE; x++)
299 for (y = 0; y < MAX_BUF_YSIZE; y++)
300 redraw[x][y] = FALSE;
303 scrolling_last = scrolling;
306 void DDScrollBuffer_ScrollTo(int X, int Y)
311 ScrollX = mScrollX = X;
312 ScrollY = mScrollY = Y;
314 ScrollPlayfieldIfNeeded();
317 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
327 r = Sqr(dx * dx + dY * dY);
328 if (r == 0) // we are there already
336 ScrollX = mScrollX = mScrollX + dx * r;
337 ScrollY = mScrollY = mScrollY + dY * r;
339 ScrollPlayfieldIfNeeded();
342 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
347 long oldX, oldY, maxD;
348 static boolean AlreadyRunning = False;
356 AlreadyRunning = True;
360 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
362 StepCount = FPS * (TimeMS / (double)1000);
363 if (StepCount > maxD)
370 tStep = (double)1 / StepCount;
374 for (T = (double)tStep; T <= (double)1; T += tStep)
376 ScrollX = mScrollX = oldX + T * dx;
377 ScrollY = mScrollY = oldY + T * dY;
380 ScrollX = mScrollX = X;
381 ScrollY = mScrollY = Y;
383 AlreadyRunning = False;
385 ScrollPlayfieldIfNeeded();