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