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];
16 int getFieldbufferOffsetX_SP(void)
18 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
20 // scroll correction for even number of visible tiles (half tile shifted)
21 px += game_sp.scroll_xoffset;
23 if (ExplosionShakeMurphy != 0)
24 px += TILEX / 2 - GetSimpleRandom(TILEX + 1);
26 px = px * TILESIZE_VAR / TILESIZE;
31 int getFieldbufferOffsetY_SP(void)
33 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
35 // scroll correction for even number of visible tiles (half tile shifted)
36 py += game_sp.scroll_yoffset;
38 if (ExplosionShakeMurphy != 0)
39 py += TILEY / 2 - GetSimpleRandom(TILEX + 1);
41 py = py * TILESIZE_VAR / TILESIZE;
46 void RestorePlayfield(void)
48 int x1 = mScrollX / TILEX - 2;
49 int y1 = mScrollY / TILEY - 2;
50 int x2 = mScrollX / TILEX + (SCR_FIELDX - 1) + 2;
51 int y2 = mScrollY / TILEY + (SCR_FIELDY - 1) + 2;
56 for (y = DisplayMinY; y <= DisplayMaxY; y++)
58 for (x = DisplayMinX; x <= DisplayMaxX; x++)
60 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
62 DrawFieldNoAnimated(x, y);
63 DrawFieldAnimated(x, y);
69 static void ScrollPlayfield(int dx, int dy)
71 int x1 = mScrollX_last / TILEX - 2;
72 int y1 = mScrollY_last / TILEY - 2;
73 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
74 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
77 BlitBitmap(bitmap_db_field_sp, bitmap_db_field_sp,
78 TILEX_VAR * (dx == -1),
79 TILEY_VAR * (dy == -1),
80 (MAX_BUF_XSIZE * TILEX_VAR) - TILEX_VAR * (dx != 0),
81 (MAX_BUF_YSIZE * TILEY_VAR) - TILEY_VAR * (dy != 0),
82 TILEX_VAR * (dx == 1),
83 TILEY_VAR * (dy == 1));
87 for (y = DisplayMinY; y <= DisplayMaxY; y++)
89 for (x = DisplayMinX; x <= DisplayMaxX; x++)
91 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
95 int tsi = GetSI(x, y);
96 int id = ((PlayField16[tsi]) |
97 (PlayField8[tsi] << 16) |
98 (DisPlayField[tsi] << 24));
100 if ((dx == -1 && x == x2) ||
101 (dx == +1 && x == x1) ||
102 (dy == -1 && y == y2) ||
103 (dy == +1 && y == y1))
105 DrawFieldNoAnimated(x, y);
106 DrawFieldAnimated(x, y);
109 ScreenBuffer[sx][sy] = id;
115 static void ScrollPlayfieldIfNeededExt(boolean reset)
125 if (mScrollX_last == -1 || mScrollY_last == -1)
127 mScrollX_last = (mScrollX / TILESIZE) * TILESIZE;
128 mScrollY_last = (mScrollY / TILESIZE) * TILESIZE;
133 // check if scrolling the playfield requires redrawing the viewport bitmap
134 if ((mScrollX != mScrollX_last ||
135 mScrollY != mScrollY_last) &&
136 (ABS(mScrollX - mScrollX_last) >= TILEX ||
137 ABS(mScrollY - mScrollY_last) >= TILEY))
139 int dx = (ABS(mScrollX - mScrollX_last) < TILEX ? 0 :
140 mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
141 int dy = (ABS(mScrollY - mScrollY_last) < TILEY ? 0 :
142 mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
144 mScrollX_last -= dx * TILEX;
145 mScrollY_last -= dy * TILEY;
147 ScrollPlayfield(dx, dy);
151 static void ScrollPlayfieldIfNeeded(void)
153 ScrollPlayfieldIfNeededExt(FALSE);
156 void InitScrollPlayfield(void)
158 ScrollPlayfieldIfNeededExt(TRUE);
161 #define DEBUG_REDRAW 0
163 void UpdatePlayfield(boolean force_redraw)
173 // force re-initialization of graphics status variables
174 for (y = DisplayMinY; y <= DisplayMaxY; y++)
175 for (x = DisplayMinX; x <= DisplayMaxX; x++)
176 GfxGraphic[x][y] = -1;
178 // force complete playfield redraw
182 for (y = DisplayMinY; y <= DisplayMaxY; y++)
184 for (x = DisplayMinX; x <= DisplayMaxX; x++)
186 int element = LowByte(PlayField16[GetSI(x, y)]);
187 int graphic = GfxGraphic[x][y];
188 int sync_frame = GfxFrameSP[x][y];
189 boolean redraw = force_redraw;
193 GfxGraphicLast[x][y] = GfxGraphic[x][y];
198 if (element != GfxElementLast[x][y] &&
199 graphic == GfxGraphicLast[x][y])
201 // element changed, but not graphic => disable updating graphic
203 GfxElementLast[x][y] = element;
204 GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
209 if (graphic != GfxGraphicLast[x][y]) // new graphic
213 GfxElementLast[x][y] = element;
214 GfxGraphicLast[x][y] = GfxGraphic[x][y];
215 sync_frame = GfxFrameSP[x][y] = 0;
217 else if (isNextAnimationFrame_SP(graphic, sync_frame)) // new frame
224 int sx = x * StretchWidth;
225 int sy = y * StretchWidth;
227 DDSpriteBuffer_BltImg(sx, sy, graphic, sync_frame);
237 printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
241 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
243 // copy playfield buffer to target bitmap at scroll position
245 int px = getFieldbufferOffsetX_SP();
246 int py = getFieldbufferOffsetY_SP();
249 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX_VAR;
250 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY_VAR;
251 int sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
252 int sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
253 int sxsize = (full_xsize < xsize ? full_xsize : xsize);
254 int sysize = (full_ysize < ysize ? full_ysize : ysize);
256 BlitBitmap(bitmap_db_field_sp, target_bitmap, px, py, sxsize, sysize, sx, sy);
259 void DDScrollBuffer_ScrollTo(int X, int Y)
264 ScrollX = mScrollX = X;
265 ScrollY = mScrollY = Y;
267 ScrollPlayfieldIfNeeded();
270 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
280 r = Sqr(dx * dx + dY * dY);
281 if (r == 0) // we are there already
289 ScrollX = mScrollX = mScrollX + dx * r;
290 ScrollY = mScrollY = mScrollY + dY * r;
292 ScrollPlayfieldIfNeeded();
295 void DDScrollBuffer_SoftScrollTo(int X, int Y, int TimeMS, int FPS)
300 int oldX, oldY, maxD;
301 static boolean AlreadyRunning = False;
309 AlreadyRunning = True;
313 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
315 StepCount = FPS * (TimeMS / (double)1000);
316 if (StepCount > maxD)
322 tStep = (double)1 / StepCount;
326 for (T = (double)tStep; T <= (double)1; T += tStep)
328 ScrollX = mScrollX = oldX + T * dx;
329 ScrollY = mScrollY = oldY + T * dY;
332 ScrollX = mScrollX = X;
333 ScrollY = mScrollY = Y;
335 AlreadyRunning = False;
337 ScrollPlayfieldIfNeeded();