1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
5 #include "DDScrollBuffer.h"
10 int mScrollX, mScrollY;
11 int mScrollX_last, mScrollY_last;
13 int ScreenBuffer[2 + MAX_PLAYFIELD_WIDTH + 2][2 + MAX_PLAYFIELD_HEIGHT + 2];
14 boolean redraw[2 + MAX_PLAYFIELD_WIDTH + 2][2 + MAX_PLAYFIELD_HEIGHT + 2];
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(bitmap_db_field_sp, bitmap_db_field_sp,
49 TILEX_VAR * (dx == -1),
50 TILEY_VAR * (dy == -1),
51 (MAX_BUF_XSIZE * TILEX_VAR) - TILEX_VAR * (dx != 0),
52 (MAX_BUF_YSIZE * TILEY_VAR) - TILEY_VAR * (dy != 0),
53 TILEX_VAR * (dx == 1),
54 TILEY_VAR * (dy == 1));
56 /* when scrolling the whole playfield, do not redraw single tiles */
57 for (x = 0; x < 2 + MAX_PLAYFIELD_WIDTH + 2; x++)
58 for (y = 0; y < 2 + MAX_PLAYFIELD_HEIGHT + 2; 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 int 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 / TILESIZE) * TILESIZE;
105 mScrollY_last = (mScrollY / TILESIZE) * TILESIZE;
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 #define DEBUG_REDRAW 0
140 void UpdatePlayfield(boolean force_redraw)
148 for (y = DisplayMinY; y <= DisplayMaxY; y++)
150 for (x = DisplayMinX; x <= DisplayMaxX; x++)
152 int element = LowByte(PlayField16[GetSI(x, y)]);
153 int graphic = GfxGraphic[x][y];
154 int sync_frame = GfxFrame[x][y];
155 boolean redraw = force_redraw;
159 GfxGraphicLast[x][y] = GfxGraphic[x][y];
164 if (element != GfxElementLast[x][y] &&
165 graphic == GfxGraphicLast[x][y])
167 /* element changed, but not graphic => disable updating graphic */
169 GfxElementLast[x][y] = element;
170 GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
175 if (graphic != GfxGraphicLast[x][y]) // new graphic
179 GfxElementLast[x][y] = element;
180 GfxGraphicLast[x][y] = GfxGraphic[x][y];
181 sync_frame = GfxFrame[x][y] = 0;
183 else if (isNextAnimationFrame_SP(graphic, sync_frame)) // new frame
190 int sx = x * StretchWidth;
191 int sy = y * StretchWidth;
193 DDSpriteBuffer_BltImg(sx, sy, graphic, sync_frame);
203 printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
207 /* copy the entire screen to the window at the scroll position */
209 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
211 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
212 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
213 int sx, sy, sxsize, sysize;
216 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX_VAR;
217 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY_VAR;
219 sxsize = (full_xsize < xsize ? full_xsize : xsize);
220 sysize = (full_ysize < ysize ? full_ysize : ysize);
221 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
222 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
224 /* scroll correction for even number of visible tiles (half tile shifted) */
225 px += game_sp.scroll_xoffset;
226 py += game_sp.scroll_yoffset;
228 if (ExplosionShakeMurphy != 0)
230 px += TILEX / 2 - GetSimpleRandom(TILEX + 1);
231 py += TILEY / 2 - GetSimpleRandom(TILEX + 1);
234 px = px * TILESIZE_VAR / TILESIZE;
235 py = py * TILESIZE_VAR / TILESIZE;
237 BlitBitmap(bitmap_db_field_sp, target_bitmap, px, py, sxsize, sysize, sx, sy);
240 void BackToFront_SP(void)
242 static int scroll_x_last = -1, scroll_y_last = -1;
243 static boolean scrolling_last = FALSE;
244 static boolean ExplosionShakeMurphy_last = -1;
245 boolean scrolling = (mScrollX != scroll_x_last || mScrollY != scroll_y_last);
249 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last ||
250 ExplosionShakeMurphy != 0 || ExplosionShakeMurphy_last != 0)
252 BlitScreenToBitmap_SP(window);
256 int scroll_xoffset = mScrollX - mScrollX_last + game_sp.scroll_xoffset;
257 int scroll_yoffset = mScrollY - mScrollY_last + game_sp.scroll_yoffset;
258 int x1 = 0, x2 = SCR_FIELDX - (scroll_xoffset != 0 ? 0 : 1);
259 int y1 = 0, y2 = SCR_FIELDY - (scroll_yoffset != 0 ? 0 : 1);
260 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX_VAR;
261 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY_VAR;
264 int sxsize = (full_xsize < xsize ? full_xsize : xsize);
265 int sysize = (full_ysize < ysize ? full_ysize : ysize);
266 int sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
267 int sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
269 InitGfxClipRegion(TRUE, sx, sy, sxsize, sysize);
271 scroll_xoffset = scroll_xoffset * TILESIZE_VAR / TILESIZE;
272 scroll_yoffset = scroll_yoffset * TILESIZE_VAR / TILESIZE;
274 for (x = x1; x <= x2; x++)
276 for (y = y1; y <= y2; y++)
282 BlitBitmap(bitmap_db_field_sp, window,
283 xx * TILEX_VAR, yy * TILEY_VAR, TILEX_VAR, TILEY_VAR,
284 sx + x * TILEX_VAR - scroll_xoffset,
285 sy + y * TILEY_VAR - scroll_yoffset);
289 InitGfxClipRegion(FALSE, -1, -1, -1, -1);
292 for (x = 0; x < 2 + MAX_PLAYFIELD_WIDTH + 2; x++)
293 for (y = 0; y < 2 + MAX_PLAYFIELD_HEIGHT + 2; y++)
294 redraw[x][y] = FALSE;
297 scroll_x_last = mScrollX;
298 scroll_y_last = mScrollY;
299 scrolling_last = scrolling;
300 ExplosionShakeMurphy_last = ExplosionShakeMurphy;
303 void DDScrollBuffer_ScrollTo(int X, int Y)
308 ScrollX = mScrollX = X;
309 ScrollY = mScrollY = Y;
311 ScrollPlayfieldIfNeeded();
314 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
324 r = Sqr(dx * dx + dY * dY);
325 if (r == 0) // we are there already
333 ScrollX = mScrollX = mScrollX + dx * r;
334 ScrollY = mScrollY = mScrollY + dY * r;
336 ScrollPlayfieldIfNeeded();
339 void DDScrollBuffer_SoftScrollTo(int X, int Y, int TimeMS, int FPS)
344 int oldX, oldY, maxD;
345 static boolean AlreadyRunning = False;
353 AlreadyRunning = True;
357 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
359 StepCount = FPS * (TimeMS / (double)1000);
360 if (StepCount > maxD)
366 tStep = (double)1 / StepCount;
370 for (T = (double)tStep; T <= (double)1; T += tStep)
372 ScrollX = mScrollX = oldX + T * dx;
373 ScrollY = mScrollY = oldY + T * dY;
376 ScrollX = mScrollX = X;
377 ScrollY = mScrollY = Y;
379 AlreadyRunning = False;
381 ScrollPlayfieldIfNeeded();