rnd-20100317-1-src
[rocksndiamonds.git] / src / game_sp / DDScrollBuffer.c
1 // ----------------------------------------------------------------------------
2 // DDScrollBuffer.c
3 // ----------------------------------------------------------------------------
4
5 #include "DDScrollBuffer.h"
6
7 #include <math.h>
8
9
10 long mScrollX, mScrollY;
11 long mScrollX_last, mScrollY_last;
12
13 long ScreenBuffer[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
14 boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
15
16
17 void RestorePlayfield()
18 {
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;
23   int x, y;
24
25   DrawFrameIfNeeded();
26
27   for (y = DisplayMinY; y <= DisplayMaxY; y++)
28   {
29     for (x = DisplayMinX; x <= DisplayMaxX; x++)
30     {
31       if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
32       {
33         DrawFieldNoAnimated(x, y);
34         DrawFieldAnimated(x, y);
35       }
36     }
37   }
38 }
39
40 static void ScrollPlayfield(int dx, int dy)
41 {
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;
46   int x, y;
47
48   BlitBitmap(screenBitmap, screenBitmap,
49              TILEX * (dx == -1),
50              TILEY * (dy == -1),
51              (MAX_BUF_XSIZE * TILEX) - TILEX * (dx != 0),
52              (MAX_BUF_YSIZE * TILEY) - TILEY * (dy != 0),
53              TILEX * (dx == 1),
54              TILEY * (dy == 1));
55
56   /* when scrolling the whole playfield, do not redraw single tiles */
57   for (x = 0; x < MAX_BUF_XSIZE; x++)
58     for (y = 0; y < MAX_BUF_YSIZE; y++)
59       redraw[x][y] = FALSE;
60   redraw_tiles = 0;
61
62   DrawFrameIfNeeded();
63
64   for (y = DisplayMinY; y <= DisplayMaxY; y++)
65   {
66     for (x = DisplayMinX; x <= DisplayMaxX; x++)
67     {
68       if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
69       {
70         int sx = x - x1;
71         int sy = y - y1;
72         int tsi = GetSI(x, y);
73         long id = ((PlayField16[tsi]) |
74                    (PlayField8[tsi] << 16) |
75                    (DisPlayField[tsi] << 24));
76
77         if ((dx == -1 && x == x2) ||
78             (dx == +1 && x == x1) ||
79             (dy == -1 && y == y2) ||
80             (dy == +1 && y == y1))
81         {
82           DrawFieldNoAnimated(x, y);
83           DrawFieldAnimated(x, y);
84         }
85
86         ScreenBuffer[sx][sy] = id;
87       }
88     }
89   }
90 }
91
92 static void ScrollPlayfieldIfNeededExt(boolean reset)
93 {
94   if (reset)
95   {
96     mScrollX_last = -1;
97     mScrollY_last = -1;
98
99     return;
100   }
101
102   if (mScrollX_last == -1 || mScrollY_last == -1)
103   {
104     mScrollX_last = mScrollX;
105     mScrollY_last = mScrollY;
106
107     return;
108   }
109
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))
115   {
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);
120
121     mScrollX_last -= dx * TILEX;
122     mScrollY_last -= dy * TILEY;
123
124     ScrollPlayfield(dx, dy);
125   }
126 }
127
128 static void ScrollPlayfieldIfNeeded()
129 {
130   ScrollPlayfieldIfNeededExt(FALSE);
131 }
132
133 void InitScrollPlayfield()
134 {
135   ScrollPlayfieldIfNeededExt(TRUE);
136 }
137
138 void UpdatePlayfield(boolean force_redraw)
139 {
140   int x, y;
141 #if 1
142   int num_redrawn = 0;
143 #endif
144
145   for (y = DisplayMinY; y <= DisplayMaxY; y++)
146   {
147     for (x = DisplayMinX; x <= DisplayMaxX; x++)
148     {
149       int element = LowByte(PlayField16[GetSI(x, y)]);
150       int graphic = GfxGraphic[x][y];
151       int sync_frame = GfxFrame[x][y];
152       boolean redraw = force_redraw;
153
154 #if 0
155       redraw = TRUE;    // !!! TEST ONLY -- ALWAYS REDRAW !!!
156 #endif
157
158       if (graphic < 0)
159       {
160         GfxGraphicLast[x][y] = GfxGraphic[x][y];
161
162         continue;
163       }
164
165       if (element != GfxElementLast[x][y] &&
166           graphic == GfxGraphicLast[x][y])
167       {
168         /* element changed, but not graphic => disable updating graphic */
169
170         GfxElementLast[x][y] = element;
171         GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
172
173         continue;
174       }
175
176       if (graphic != GfxGraphicLast[x][y])                      // new graphic
177       {
178         redraw = TRUE;
179
180         GfxElementLast[x][y] = element;
181         GfxGraphicLast[x][y] = GfxGraphic[x][y];
182         sync_frame = GfxFrame[x][y] = 0;
183       }
184       else if (isNextAnimationFrame_SP(graphic, sync_frame))    // new frame
185       {
186         redraw = TRUE;
187       }
188
189       if (redraw)
190       {
191         int sx = x * StretchWidth;
192         int sy = y * StretchWidth;
193
194 #if 0
195         printf("::: REDRAW (%d, %d): %d, %d\n", x, y, graphic, sync_frame);
196 #endif
197
198         DDSpriteBuffer_BltImg(sx, sy, graphic, sync_frame);
199
200 #if 1
201         num_redrawn++;
202 #endif
203       }
204     }
205   }
206
207 #if 0
208   printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
209 #endif
210 }
211
212 /* copy the entire screen to the window at the scroll position */
213
214 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
215 {
216   int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
217   int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
218   int sx, sy, sxsize, sysize;
219
220   int xsize = SXSIZE;
221   int ysize = SYSIZE;
222   int full_xsize = (FieldWidth  - (menBorder ? 0 : 1)) * TILEX;
223   int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY;
224
225   sxsize = (full_xsize < xsize ? full_xsize : xsize);
226   sysize = (full_ysize < ysize ? full_ysize : ysize);
227   sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
228   sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
229
230   if (!menBorder)
231   {
232     px += TILEX / 2;
233     py += TILEY / 2;
234   }
235
236   BlitBitmap(screenBitmap, target_bitmap, px, py, sxsize, sysize, sx, sy);
237 }
238
239 void BackToFront_SP(void)
240 {
241   static boolean scrolling_last = FALSE;
242   int left = mScrollX / TILEX;
243   int top  = mScrollY / TILEY;
244   boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
245   int x, y;
246
247   SyncDisplay();
248
249   if (1 ||
250       redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
251   {
252     BlitScreenToBitmap_SP(window);
253   }
254   else
255   {
256     for (x = 0; x < SCR_FIELDX; x++)
257     {
258       for (y = 0; y < SCR_FIELDY; y++)
259       {
260         int xx = (left + x) % MAX_BUF_XSIZE;
261         int yy = (top  + y) % MAX_BUF_YSIZE;
262
263         if (redraw[xx][yy])
264           BlitBitmap(screenBitmap, window,
265                      xx * TILEX, yy * TILEY, TILEX, TILEY,
266                      SX + x * TILEX, SY + y * TILEY);
267       }
268     }
269   }
270
271   FlushDisplay();
272
273   for (x = 0; x < MAX_BUF_XSIZE; x++)
274     for (y = 0; y < MAX_BUF_YSIZE; y++)
275       redraw[x][y] = FALSE;
276   redraw_tiles = 0;
277
278   scrolling_last = scrolling;
279 }
280
281 void DDScrollBuffer_ScrollTo(int X, int Y)
282 {
283   if (NoDisplayFlag)
284     return;
285
286   ScrollX = mScrollX = X;
287   ScrollY = mScrollY = Y;
288
289   ScrollPlayfieldIfNeeded();
290 }
291
292 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
293 {
294   double dx, dY, r;
295
296   if (NoDisplayFlag)
297     return;
298
299   dx = X - mScrollX;
300   dY = Y - mScrollY;
301
302   r = Sqr(dx * dx + dY * dY);
303   if (r == 0)   // we are there already
304     return;
305
306   if (Step < r)
307     r = Step / r;
308   else
309     r = 1;
310
311   ScrollX = mScrollX = mScrollX + dx * r;
312   ScrollY = mScrollY = mScrollY + dY * r;
313
314   ScrollPlayfieldIfNeeded();
315 }
316
317 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
318 {
319   double dx, dY;
320   long dT, StepCount;
321   double T, tStep;
322   long oldX, oldY, maxD;
323   static boolean AlreadyRunning = False;
324
325   if (NoDisplayFlag)
326     return;
327
328   if (AlreadyRunning)
329     return;
330
331   AlreadyRunning = True;
332
333   dx = X - mScrollX;
334   dY = Y - mScrollY;
335   maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
336
337   StepCount = FPS * (TimeMS / (double)1000);
338   if (StepCount > maxD)
339     StepCount = maxD;
340
341   if (StepCount == 0)
342     StepCount = 1;
343
344   dT = 1000 / FPS;
345   tStep = (double)1 / StepCount;
346   oldX = mScrollX;
347   oldY = mScrollY;
348
349   for (T = (double)tStep; T <= (double)1; T += tStep)
350   {
351     ScrollX = mScrollX = oldX + T * dx;
352     ScrollY = mScrollY = oldY + T * dY;
353   }
354
355   ScrollX = mScrollX = X;
356   ScrollY = mScrollY = Y;
357
358   AlreadyRunning = False;
359
360   ScrollPlayfieldIfNeeded();
361 }