1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
5 #include "DDScrollBuffer.h"
12 long mScrollX, mScrollY;
13 long mScrollX_last, mScrollY_last;
14 long mDestXOff, mDestYOff;
16 long ScreenBuffer[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
17 boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
20 void RestorePlayfield()
22 int x1 = mScrollX / TILEX - 2;
23 int y1 = mScrollY / TILEY - 2;
24 int x2 = mScrollX / TILEX + (SCR_FIELDX - 1) + 2;
25 int y2 = mScrollY / TILEY + (SCR_FIELDY - 1) + 2;
30 for (y = DisplayMinY; y <= DisplayMaxY; y++)
32 for (x = DisplayMinX; x <= DisplayMaxX; x++)
34 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
36 DrawFieldNoAnimated(x, y);
37 DrawFieldAnimated(x, y);
43 static void ScrollPlayfield(int dx, int dy)
45 int x1 = mScrollX_last / TILEX - 2;
46 int y1 = mScrollY_last / TILEY - 2;
47 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
48 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
51 BlitBitmap(screenBitmap, screenBitmap,
54 (MAX_BUF_XSIZE * TILEX) - TILEX * (dx != 0),
55 (MAX_BUF_YSIZE * TILEY) - TILEY * (dy != 0),
59 /* when scrolling the whole playfield, do not redraw single tiles */
60 for (x = 0; x < MAX_BUF_XSIZE; x++)
61 for (y = 0; y < MAX_BUF_YSIZE; y++)
67 for (y = DisplayMinY; y <= DisplayMaxY; y++)
69 for (x = DisplayMinX; x <= DisplayMaxX; x++)
71 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
75 int tsi = GetSI(x, y);
76 long id = ((PlayField16[tsi]) |
77 (PlayField8[tsi] << 16) |
78 (DisPlayField[tsi] << 24));
80 if ((dx == -1 && x == x2) ||
81 (dx == +1 && x == x1) ||
82 (dy == -1 && y == y2) ||
83 (dy == +1 && y == y1))
85 DrawFieldNoAnimated(x, y);
86 DrawFieldAnimated(x, y);
89 ScreenBuffer[sx][sy] = id;
95 static void ScrollPlayfieldIfNeededExt(boolean reset)
105 if (mScrollX_last == -1 || mScrollY_last == -1)
107 mScrollX_last = mScrollX;
108 mScrollY_last = mScrollY;
113 /* check if scrolling the playfield requires redrawing the viewport bitmap */
114 if ((mScrollX != mScrollX_last ||
115 mScrollY != mScrollY_last) &&
116 (ABS(mScrollX - mScrollX_last) >= TILEX ||
117 ABS(mScrollY - mScrollY_last) >= TILEY))
119 int dx = (ABS(mScrollX - mScrollX_last) < TILEX ? 0 :
120 mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
121 int dy = (ABS(mScrollY - mScrollY_last) < TILEY ? 0 :
122 mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
124 mScrollX_last -= dx * TILEX;
125 mScrollY_last -= dy * TILEY;
127 ScrollPlayfield(dx, dy);
131 static void ScrollPlayfieldIfNeeded()
133 ScrollPlayfieldIfNeededExt(FALSE);
136 void InitScrollPlayfield()
138 ScrollPlayfieldIfNeededExt(TRUE);
141 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;
158 redraw = TRUE; // !!! TEST ONLY -- ALWAYS REDRAW !!!
163 GfxGraphicLast[x][y] = GfxGraphic[x][y];
168 if (element != GfxElementLast[x][y] &&
169 graphic == GfxGraphicLast[x][y])
171 /* element changed, but not graphic => disable updating graphic */
173 GfxElementLast[x][y] = element;
174 GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
179 if (graphic != GfxGraphicLast[x][y]) // new graphic
183 GfxElementLast[x][y] = element;
184 GfxGraphicLast[x][y] = GfxGraphic[x][y];
185 sync_frame = GfxFrame[x][y] = 0;
187 else if (isNextAnimationFrame_SP(graphic, sync_frame)) // new frame
194 int sx = x * StretchWidth;
195 int sy = y * StretchWidth;
198 printf("::: REDRAW (%d, %d): %d, %d\n", x, y, graphic, sync_frame);
201 StretchedSprites.BltImg(sx, sy, graphic, sync_frame);
211 printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
215 /* copy the entire screen to the window at the scroll position */
217 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
219 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
220 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
221 int sx, sy, sxsize, sysize;
225 int full_xsize = (FieldWidth - (menBorder.Checked ? 0 : 1)) * TILEX;
226 int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
228 sxsize = (full_xsize < xsize ? full_xsize : xsize);
229 sysize = (full_ysize < ysize ? full_ysize : ysize);
230 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
231 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
233 if (!menBorder.Checked)
239 BlitBitmap(screenBitmap, target_bitmap, px, py, sxsize, sysize, sx, sy);
242 void BackToFront_SP(void)
244 static boolean scrolling_last = FALSE;
245 int left = mScrollX / TILEX;
246 int top = mScrollY / TILEY;
247 boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
253 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
255 BlitScreenToBitmap_SP(window);
259 for (x = 0; x < SCR_FIELDX; x++)
261 for (y = 0; y < SCR_FIELDY; y++)
263 int xx = (left + x) % MAX_BUF_XSIZE;
264 int yy = (top + y) % MAX_BUF_YSIZE;
267 BlitBitmap(screenBitmap, window,
268 xx * TILEX, yy * TILEY, TILEX, TILEY,
269 SX + x * TILEX, SY + y * TILEY);
276 for (x = 0; x < MAX_BUF_XSIZE; x++)
277 for (y = 0; y < MAX_BUF_YSIZE; y++)
278 redraw[x][y] = FALSE;
281 scrolling_last = scrolling;
284 void DDScrollBuffer_Blt()
289 void DDScrollBuffer_ScrollTo(int X, int Y)
301 ScrollPlayfieldIfNeeded();
304 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
316 r = Sqr(dx * dx + dY * dY);
317 if (r == 0) // we are there already
325 mScrollX = mScrollX + dx * r;
326 mScrollY = mScrollY + dY * r;
330 ScrollPlayfieldIfNeeded();
333 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
338 long oldX, oldY, maxD;
339 static boolean AlreadyRunning = False;
347 AlreadyRunning = True;
352 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
354 StepCount = FPS * (TimeMS / (double)1000);
355 if (StepCount > maxD)
362 tStep = (double)1 / StepCount;
366 for (T = (double)tStep; T <= (double)1; T += tStep)
371 mScrollX = oldX + T * dx;
372 mScrollY = oldY + T * dY;
386 AlreadyRunning = False;
388 ScrollPlayfieldIfNeeded();