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()
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()
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()
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()
153 ScrollPlayfieldIfNeededExt(FALSE);
156 void InitScrollPlayfield()
158 ScrollPlayfieldIfNeededExt(TRUE);
161 #define DEBUG_REDRAW 0
163 void UpdatePlayfield(boolean force_redraw)
171 for (y = DisplayMinY; y <= DisplayMaxY; y++)
173 for (x = DisplayMinX; x <= DisplayMaxX; x++)
175 int element = LowByte(PlayField16[GetSI(x, y)]);
176 int graphic = GfxGraphic[x][y];
177 int sync_frame = GfxFrame[x][y];
178 boolean redraw = force_redraw;
182 GfxGraphicLast[x][y] = GfxGraphic[x][y];
187 if (element != GfxElementLast[x][y] &&
188 graphic == GfxGraphicLast[x][y])
190 /* element changed, but not graphic => disable updating graphic */
192 GfxElementLast[x][y] = element;
193 GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
198 if (graphic != GfxGraphicLast[x][y]) // new graphic
202 GfxElementLast[x][y] = element;
203 GfxGraphicLast[x][y] = GfxGraphic[x][y];
204 sync_frame = GfxFrame[x][y] = 0;
206 else if (isNextAnimationFrame_SP(graphic, sync_frame)) // new frame
213 int sx = x * StretchWidth;
214 int sy = y * StretchWidth;
216 DDSpriteBuffer_BltImg(sx, sy, graphic, sync_frame);
226 printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
230 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
232 /* copy playfield buffer to target bitmap at scroll position */
234 int px = getFieldbufferOffsetX_SP();
235 int py = getFieldbufferOffsetY_SP();
238 int full_xsize = (FieldWidth - (menBorder ? 0 : 1)) * TILEX_VAR;
239 int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY_VAR;
240 int sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
241 int sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
242 int sxsize = (full_xsize < xsize ? full_xsize : xsize);
243 int sysize = (full_ysize < ysize ? full_ysize : ysize);
245 BlitBitmap(bitmap_db_field_sp, target_bitmap, px, py, sxsize, sysize, sx, sy);
248 void DDScrollBuffer_ScrollTo(int X, int Y)
253 ScrollX = mScrollX = X;
254 ScrollY = mScrollY = Y;
256 ScrollPlayfieldIfNeeded();
259 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
269 r = Sqr(dx * dx + dY * dY);
270 if (r == 0) // we are there already
278 ScrollX = mScrollX = mScrollX + dx * r;
279 ScrollY = mScrollY = mScrollY + dY * r;
281 ScrollPlayfieldIfNeeded();
284 void DDScrollBuffer_SoftScrollTo(int X, int Y, int TimeMS, int FPS)
289 int oldX, oldY, maxD;
290 static boolean AlreadyRunning = False;
298 AlreadyRunning = True;
302 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
304 StepCount = FPS * (TimeMS / (double)1000);
305 if (StepCount > maxD)
311 tStep = (double)1 / StepCount;
315 for (T = (double)tStep; T <= (double)1; T += tStep)
317 ScrollX = mScrollX = oldX + T * dx;
318 ScrollY = mScrollY = oldY + T * dY;
321 ScrollX = mScrollX = X;
322 ScrollY = mScrollY = Y;
324 AlreadyRunning = False;
326 ScrollPlayfieldIfNeeded();