rnd-20100417-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     mScrollX_last = mScrollX;
110     mScrollY_last = mScrollY;
111
112     return;
113   }
114
115   /* check if scrolling the playfield requires redrawing the viewport bitmap */
116   if ((mScrollX != mScrollX_last ||
117        mScrollY != mScrollY_last) &&
118       (ABS(mScrollX - mScrollX_last) >= TILEX ||
119        ABS(mScrollY - mScrollY_last) >= TILEY))
120   {
121     int dx = (ABS(mScrollX - mScrollX_last) < TILEX ? 0 :
122               mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
123     int dy = (ABS(mScrollY - mScrollY_last) < TILEY ? 0 :
124               mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
125
126     mScrollX_last -= dx * TILEX;
127     mScrollY_last -= dy * TILEY;
128
129     ScrollPlayfield(dx, dy);
130   }
131 }
132
133 static void ScrollPlayfieldIfNeeded()
134 {
135   ScrollPlayfieldIfNeededExt(FALSE);
136 }
137
138 void InitScrollPlayfield()
139 {
140   ScrollPlayfieldIfNeededExt(TRUE);
141 }
142
143 void UpdatePlayfield(boolean force_redraw)
144 {
145   int x, y;
146 #if 1
147   int num_redrawn = 0;
148 #endif
149
150   for (y = DisplayMinY; y <= DisplayMaxY; y++)
151   {
152     for (x = DisplayMinX; x <= DisplayMaxX; x++)
153     {
154       int element = LowByte(PlayField16[GetSI(x, y)]);
155       int graphic = GfxGraphic[x][y];
156       int sync_frame = GfxFrame[x][y];
157       boolean redraw = force_redraw;
158
159 #if 0
160       redraw = TRUE;    // !!! TEST ONLY -- ALWAYS REDRAW !!!
161 #endif
162
163       if (graphic < 0)
164       {
165         GfxGraphicLast[x][y] = GfxGraphic[x][y];
166
167         continue;
168       }
169
170       if (element != GfxElementLast[x][y] &&
171           graphic == GfxGraphicLast[x][y])
172       {
173         /* element changed, but not graphic => disable updating graphic */
174
175         GfxElementLast[x][y] = element;
176         GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
177
178         continue;
179       }
180
181       if (graphic != GfxGraphicLast[x][y])                      // new graphic
182       {
183         redraw = TRUE;
184
185         GfxElementLast[x][y] = element;
186         GfxGraphicLast[x][y] = GfxGraphic[x][y];
187         sync_frame = GfxFrame[x][y] = 0;
188       }
189       else if (isNextAnimationFrame_SP(graphic, sync_frame))    // new frame
190       {
191         redraw = TRUE;
192       }
193
194       if (redraw)
195       {
196         int sx = x * StretchWidth;
197         int sy = y * StretchWidth;
198
199 #if 0
200         printf("::: REDRAW (%d, %d): %d, %d\n", x, y, graphic, sync_frame);
201 #endif
202
203         DDSpriteBuffer_BltImg(sx, sy, graphic, sync_frame);
204
205 #if 1
206         num_redrawn++;
207 #endif
208       }
209     }
210   }
211
212 #if 0
213   printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
214 #endif
215 }
216
217 /* copy the entire screen to the window at the scroll position */
218
219 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
220 {
221   int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
222   int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
223   int sx, sy, sxsize, sysize;
224
225 #if 1
226   printf("::: %d, %d - %d, %d - %ld, %ld\n",
227          MurphyScreenXPos, MurphyScreenYPos,
228          ScreenScrollXPos, ScreenScrollYPos,
229          mScrollX, mScrollY);
230 #endif
231
232   int xsize = SXSIZE;
233   int ysize = SYSIZE;
234   int full_xsize = (FieldWidth  - (menBorder ? 0 : 1)) * TILEX;
235   int full_ysize = (FieldHeight - (menBorder ? 0 : 1)) * TILEY;
236
237   sxsize = (full_xsize < xsize ? full_xsize : xsize);
238   sysize = (full_ysize < ysize ? full_ysize : ysize);
239   sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
240   sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
241
242 #if 1
243   if (0 && !menBorder)
244   {
245     if (mScrollX < ScrollMinX + TILEX / 2)
246       px += ScrollMinX + TILEX / 2 - mScrollX;
247     else if (mScrollX > ScrollMaxX - TILEX / 2)
248       px -= mScrollX - (ScrollMaxX - TILEX / 2);
249     if (mScrollY < ScrollMinY + TILEY / 2)
250       py += ScrollMinY + TILEY / 2 - mScrollY;
251     else if (mScrollY > ScrollMaxY - TILEY / 2)
252       py -= mScrollY - (ScrollMaxY - TILEY / 2);
253   }
254 #else
255   if (!menBorder)
256   {
257     px += TILEX / 2;
258     py += TILEY / 2;
259   }
260 #endif
261
262   BlitBitmap(bitmap_db_field_sp, target_bitmap, px, py, sxsize, sysize, sx, sy);
263 }
264
265 void BackToFront_SP(void)
266 {
267   static boolean scrolling_last = FALSE;
268   int left = mScrollX / TILEX;
269   int top  = mScrollY / TILEY;
270   boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
271   int x, y;
272
273   SyncDisplay();
274
275   if (1 ||
276       redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
277   {
278     BlitScreenToBitmap_SP(window);
279   }
280   else
281   {
282     for (x = 0; x < SCR_FIELDX; x++)
283     {
284       for (y = 0; y < SCR_FIELDY; y++)
285       {
286         int xx = (left + x) % MAX_BUF_XSIZE;
287         int yy = (top  + y) % MAX_BUF_YSIZE;
288
289         if (redraw[xx][yy])
290           BlitBitmap(bitmap_db_field_sp, window,
291                      xx * TILEX, yy * TILEY, TILEX, TILEY,
292                      SX + x * TILEX, SY + y * TILEY);
293       }
294     }
295   }
296
297   FlushDisplay();
298
299   for (x = 0; x < MAX_BUF_XSIZE; x++)
300     for (y = 0; y < MAX_BUF_YSIZE; y++)
301       redraw[x][y] = FALSE;
302   redraw_tiles = 0;
303
304   scrolling_last = scrolling;
305 }
306
307 void DDScrollBuffer_ScrollTo(int X, int Y)
308 {
309   if (NoDisplayFlag)
310     return;
311
312   ScrollX = mScrollX = X;
313   ScrollY = mScrollY = Y;
314
315   ScrollPlayfieldIfNeeded();
316 }
317
318 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
319 {
320   double dx, dY, r;
321
322   if (NoDisplayFlag)
323     return;
324
325   dx = X - mScrollX;
326   dY = Y - mScrollY;
327
328   r = Sqr(dx * dx + dY * dY);
329   if (r == 0)   // we are there already
330     return;
331
332   if (Step < r)
333     r = Step / r;
334   else
335     r = 1;
336
337   ScrollX = mScrollX = mScrollX + dx * r;
338   ScrollY = mScrollY = mScrollY + dY * r;
339
340   ScrollPlayfieldIfNeeded();
341 }
342
343 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
344 {
345   double dx, dY;
346   long dT, StepCount;
347   double T, tStep;
348   long oldX, oldY, maxD;
349   static boolean AlreadyRunning = False;
350
351   if (NoDisplayFlag)
352     return;
353
354   if (AlreadyRunning)
355     return;
356
357   AlreadyRunning = True;
358
359   dx = X - mScrollX;
360   dY = Y - mScrollY;
361   maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
362
363   StepCount = FPS * (TimeMS / (double)1000);
364   if (StepCount > maxD)
365     StepCount = maxD;
366
367   if (StepCount == 0)
368     StepCount = 1;
369
370   dT = 1000 / FPS;
371   tStep = (double)1 / StepCount;
372   oldX = mScrollX;
373   oldY = mScrollY;
374
375   for (T = (double)tStep; T <= (double)1; T += tStep)
376   {
377     ScrollX = mScrollX = oldX + T * dx;
378     ScrollY = mScrollY = oldY + T * dY;
379   }
380
381   ScrollX = mScrollX = X;
382   ScrollY = mScrollY = Y;
383
384   AlreadyRunning = False;
385
386   ScrollPlayfieldIfNeeded();
387 }