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