1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
5 #include "DDScrollBuffer.h"
10 int mScrollX, mScrollY;
11 int mScrollX_last, mScrollY_last;
14 int 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 int 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;
54 BlitBitmap(bitmap_db_field_sp, bitmap_db_field_sp,
55 TILEX_VAR * (dx == -1),
56 TILEY_VAR * (dy == -1),
57 (MAX_BUF_XSIZE * TILEX_VAR) - TILEX_VAR * (dx != 0),
58 (MAX_BUF_YSIZE * TILEY_VAR) - TILEY_VAR * (dy != 0),
59 TILEX_VAR * (dx == 1),
60 TILEY_VAR * (dy == 1));
62 BlitBitmap(bitmap_db_field_sp, bitmap_db_field_sp,
65 (MAX_BUF_XSIZE * TILEX) - TILEX * (dx != 0),
66 (MAX_BUF_YSIZE * TILEY) - TILEY * (dy != 0),
71 /* when scrolling the whole playfield, do not redraw single tiles */
73 for (x = 0; x < 2 + MAX_PLAYFIELD_WIDTH + 2; x++)
74 for (y = 0; y < 2 + MAX_PLAYFIELD_HEIGHT + 2; y++)
77 for (x = 0; x < MAX_BUF_XSIZE; x++)
78 for (y = 0; y < MAX_BUF_YSIZE; y++)
85 for (y = DisplayMinY; y <= DisplayMaxY; y++)
87 for (x = DisplayMinX; x <= DisplayMaxX; x++)
89 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
93 int tsi = GetSI(x, y);
94 int id = ((PlayField16[tsi]) |
95 (PlayField8[tsi] << 16) |
96 (DisPlayField[tsi] << 24));
98 if ((dx == -1 && x == x2) ||
99 (dx == +1 && x == x1) ||
100 (dy == -1 && y == y2) ||
101 (dy == +1 && y == y1))
103 DrawFieldNoAnimated(x, y);
104 DrawFieldAnimated(x, y);
107 ScreenBuffer[sx][sy] = id;
113 static void ScrollPlayfieldIfNeededExt(boolean reset)
123 if (mScrollX_last == -1 || mScrollY_last == -1)
126 mScrollX_last = (mScrollX / TILESIZE) * TILESIZE;
127 mScrollY_last = (mScrollY / TILESIZE) * TILESIZE;
129 mScrollX_last = mScrollX;
130 mScrollY_last = mScrollY;
136 /* check if scrolling the playfield requires redrawing the viewport bitmap */
137 if ((mScrollX != mScrollX_last ||
138 mScrollY != mScrollY_last) &&
139 (ABS(mScrollX - mScrollX_last) >= TILEX ||
140 ABS(mScrollY - mScrollY_last) >= TILEY))
142 int dx = (ABS(mScrollX - mScrollX_last) < TILEX ? 0 :
143 mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
144 int dy = (ABS(mScrollY - mScrollY_last) < TILEY ? 0 :
145 mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
147 mScrollX_last -= dx * TILEX;
148 mScrollY_last -= dy * TILEY;
150 ScrollPlayfield(dx, dy);
154 static void ScrollPlayfieldIfNeeded()
156 ScrollPlayfieldIfNeededExt(FALSE);
159 void InitScrollPlayfield()
161 ScrollPlayfieldIfNeededExt(TRUE);
164 #define DEBUG_REDRAW 0
166 void UpdatePlayfield(boolean force_redraw)
174 for (y = DisplayMinY; y <= DisplayMaxY; y++)
176 for (x = DisplayMinX; x <= DisplayMaxX; x++)
178 int element = LowByte(PlayField16[GetSI(x, y)]);
179 int graphic = GfxGraphic[x][y];
180 int sync_frame = GfxFrame[x][y];
181 boolean redraw = force_redraw;
185 redraw = TRUE; // !!! TEST ONLY -- ALWAYS REDRAW !!!
191 GfxGraphicLast[x][y] = GfxGraphic[x][y];
196 if (element != GfxElementLast[x][y] &&
197 graphic == GfxGraphicLast[x][y])
199 /* element changed, but not graphic => disable updating graphic */
201 GfxElementLast[x][y] = element;
202 GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
207 if (graphic != GfxGraphicLast[x][y]) // new graphic
211 GfxElementLast[x][y] = element;
212 GfxGraphicLast[x][y] = GfxGraphic[x][y];
213 sync_frame = GfxFrame[x][y] = 0;
215 else if (isNextAnimationFrame_SP(graphic, sync_frame)) // new frame
222 int sx = x * StretchWidth;
223 int sy = y * StretchWidth;
227 printf("::: REDRAW (%d, %d): %d, %d\n", x, y, graphic, sync_frame);
231 DDSpriteBuffer_BltImg(sx, sy, graphic, sync_frame);
241 printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
245 /* copy the entire screen to the window at the scroll position */
247 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
249 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
250 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
251 int sx, sy, sxsize, sysize;
254 printf("::: %d, %d / %d, %d / %ld, %ld (%ld, %ld) / %d, %d\n",
255 MurphyScreenXPos, MurphyScreenYPos,
256 ScreenScrollXPos, ScreenScrollYPos,
258 mScrollX_last, mScrollY_last,
265 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX_VAR;
266 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY_VAR;
268 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX;
269 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY;
276 sxsize = (full_xsize < xsize ? full_xsize : xsize);
277 sysize = (full_ysize < ysize ? full_ysize : ysize);
278 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
279 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
281 /* scroll correction for even number of visible tiles (half tile shifted) */
282 px += game_sp.scroll_xoffset;
283 py += game_sp.scroll_yoffset;
286 if (ExplosionShakeMurphy != 0)
288 px += TILEX / 2 - GetSimpleRandom(TILEX + 1);
289 py += TILEY / 2 - GetSimpleRandom(TILEX + 1);
294 px = px * TILESIZE_VAR / TILESIZE;
295 py = py * TILESIZE_VAR / TILESIZE;
299 printf("::: (%d, %d) (%d, %d) (%d, %d) [%d / %d]\n",
300 px, py, sxsize, sysize, sx, sy,
301 FieldHeight, menBorder);
305 printf("::: (%d, %d)\n",
306 bitmap_db_field_sp->width, bitmap_db_field_sp->height);
309 BlitBitmap(bitmap_db_field_sp, target_bitmap, px, py, sxsize, sysize, sx, sy);
312 void BackToFront_SP(void)
314 static int scroll_x_last = -1, scroll_y_last = -1;
315 static boolean scrolling_last = FALSE;
316 static boolean ExplosionShakeMurphy_last = -1;
318 boolean scrolling = (mScrollX != scroll_x_last || mScrollY != scroll_y_last);
319 // boolean scrolling = (mScrollX != mScrollX_last || mScrollY != mScrollY_last);
321 boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
326 printf("::: %d, %d / %d, %d [%d, %d]\n",
328 mScrollX_last, mScrollY_last,
329 game_sp.scroll_xoffset, game_sp.scroll_yoffset);
335 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last ||
336 ExplosionShakeMurphy != 0 || ExplosionShakeMurphy_last != 0)
338 BlitScreenToBitmap_SP(window);
342 int scroll_xoffset = mScrollX - mScrollX_last + game_sp.scroll_xoffset;
343 int scroll_yoffset = mScrollY - mScrollY_last + game_sp.scroll_yoffset;
344 int x1 = 0, x2 = SCR_FIELDX - (scroll_xoffset != 0 ? 0 : 1);
345 int y1 = 0, y2 = SCR_FIELDY - (scroll_yoffset != 0 ? 0 : 1);
347 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX_VAR;
348 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY_VAR;
350 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX;
351 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY;
356 int sxsize = (full_xsize < xsize ? full_xsize : xsize);
357 int sysize = (full_ysize < ysize ? full_ysize : ysize);
358 int sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
359 int sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
361 int sx = SX + (full_xsize < SXSIZE ? (SXSIZE - full_xsize) / 2 : 0);
362 int sy = SY + (full_ysize < SYSIZE ? (SYSIZE - full_ysize) / 2 : 0);
366 InitGfxClipRegion(TRUE, sx, sy, sxsize, sysize);
368 InitGfxClipRegion(TRUE, SX, SY, SXSIZE, SYSIZE);
372 scroll_xoffset = scroll_xoffset * TILESIZE_VAR / TILESIZE;
373 scroll_yoffset = scroll_yoffset * TILESIZE_VAR / TILESIZE;
376 for (x = x1; x <= x2; x++)
378 for (y = y1; y <= y2; y++)
385 BlitBitmap(bitmap_db_field_sp, window,
386 xx * TILEX_VAR, yy * TILEY_VAR, TILEX_VAR, TILEY_VAR,
387 sx + x * TILEX_VAR - scroll_xoffset,
388 sy + y * TILEY_VAR - scroll_yoffset);
391 BlitBitmap(bitmap_db_field_sp, window,
392 xx * TILEX, yy * TILEY, TILEX, TILEY,
393 sx + x * TILEX - scroll_xoffset,
394 sy + y * TILEY - scroll_yoffset);
399 InitGfxClipRegion(FALSE, -1, -1, -1, -1);
405 for (x = 0; x < 2 + MAX_PLAYFIELD_WIDTH + 2; x++)
406 for (y = 0; y < 2 + MAX_PLAYFIELD_HEIGHT + 2; y++)
407 redraw[x][y] = FALSE;
409 for (x = 0; x < MAX_BUF_XSIZE; x++)
410 for (y = 0; y < MAX_BUF_YSIZE; y++)
411 redraw[x][y] = FALSE;
415 scroll_x_last = mScrollX;
416 scroll_y_last = mScrollY;
417 scrolling_last = scrolling;
418 ExplosionShakeMurphy_last = ExplosionShakeMurphy;
421 void DDScrollBuffer_ScrollTo(int X, int Y)
426 ScrollX = mScrollX = X;
427 ScrollY = mScrollY = Y;
429 ScrollPlayfieldIfNeeded();
432 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
442 r = Sqr(dx * dx + dY * dY);
443 if (r == 0) // we are there already
451 ScrollX = mScrollX = mScrollX + dx * r;
452 ScrollY = mScrollY = mScrollY + dY * r;
454 ScrollPlayfieldIfNeeded();
457 void DDScrollBuffer_SoftScrollTo(int X, int Y, int TimeMS, int FPS)
465 int oldX, oldY, maxD;
466 static boolean AlreadyRunning = False;
474 AlreadyRunning = True;
478 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
480 StepCount = FPS * (TimeMS / (double)1000);
481 if (StepCount > maxD)
490 tStep = (double)1 / StepCount;
494 for (T = (double)tStep; T <= (double)1; T += tStep)
496 ScrollX = mScrollX = oldX + T * dx;
497 ScrollY = mScrollY = oldY + T * dY;
500 ScrollX = mScrollX = X;
501 ScrollY = mScrollY = Y;
503 AlreadyRunning = False;
505 ScrollPlayfieldIfNeeded();