rnd-20100418-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   BlitBitmap(bitmap_db_field_sp, target_bitmap, px, py, sxsize, sysize, sx, sy);
254 }
255
256 void BackToFront_SP(void)
257 {
258   static boolean scrolling_last = FALSE;
259   int left = mScrollX / TILEX;
260   int top  = mScrollY / TILEY;
261   boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
262   int x, y;
263
264   SyncDisplay();
265
266   if (1 ||
267       redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
268   {
269     BlitScreenToBitmap_SP(window);
270   }
271   else
272   {
273     for (x = 0; x < SCR_FIELDX; x++)
274     {
275       for (y = 0; y < SCR_FIELDY; y++)
276       {
277         int xx = (left + x) % MAX_BUF_XSIZE;
278         int yy = (top  + y) % MAX_BUF_YSIZE;
279
280         if (redraw[xx][yy])
281           BlitBitmap(bitmap_db_field_sp, window,
282                      xx * TILEX, yy * TILEY, TILEX, TILEY,
283                      SX + x * TILEX, SY + y * TILEY);
284       }
285     }
286   }
287
288   FlushDisplay();
289
290   for (x = 0; x < MAX_BUF_XSIZE; x++)
291     for (y = 0; y < MAX_BUF_YSIZE; y++)
292       redraw[x][y] = FALSE;
293   redraw_tiles = 0;
294
295   scrolling_last = scrolling;
296 }
297
298 void DDScrollBuffer_ScrollTo(int X, int Y)
299 {
300   if (NoDisplayFlag)
301     return;
302
303   ScrollX = mScrollX = X;
304   ScrollY = mScrollY = Y;
305
306   ScrollPlayfieldIfNeeded();
307 }
308
309 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
310 {
311   double dx, dY, r;
312
313   if (NoDisplayFlag)
314     return;
315
316   dx = X - mScrollX;
317   dY = Y - mScrollY;
318
319   r = Sqr(dx * dx + dY * dY);
320   if (r == 0)   // we are there already
321     return;
322
323   if (Step < r)
324     r = Step / r;
325   else
326     r = 1;
327
328   ScrollX = mScrollX = mScrollX + dx * r;
329   ScrollY = mScrollY = mScrollY + dY * r;
330
331   ScrollPlayfieldIfNeeded();
332 }
333
334 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
335 {
336   double dx, dY;
337   long dT, StepCount;
338   double T, tStep;
339   long oldX, oldY, maxD;
340   static boolean AlreadyRunning = False;
341
342   if (NoDisplayFlag)
343     return;
344
345   if (AlreadyRunning)
346     return;
347
348   AlreadyRunning = True;
349
350   dx = X - mScrollX;
351   dY = Y - mScrollY;
352   maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
353
354   StepCount = FPS * (TimeMS / (double)1000);
355   if (StepCount > maxD)
356     StepCount = maxD;
357
358   if (StepCount == 0)
359     StepCount = 1;
360
361   dT = 1000 / FPS;
362   tStep = (double)1 / StepCount;
363   oldX = mScrollX;
364   oldY = mScrollY;
365
366   for (T = (double)tStep; T <= (double)1; T += tStep)
367   {
368     ScrollX = mScrollX = oldX + T * dx;
369     ScrollY = mScrollY = oldY + T * dY;
370   }
371
372   ScrollX = mScrollX = X;
373   ScrollY = mScrollY = Y;
374
375   AlreadyRunning = False;
376
377   ScrollPlayfieldIfNeeded();
378 }