rnd-20100315-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 mhWnd;
12 long mScrollX, mScrollY;
13 long mScrollX_last, mScrollY_last;
14 long mDestXOff, mDestYOff;
15
16 long ScreenBuffer[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
17 boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
18
19
20 void RestorePlayfield()
21 {
22   int x1 = mScrollX / TILEX - 2;
23   int y1 = mScrollY / TILEY - 2;
24   int x2 = mScrollX / TILEX + (SCR_FIELDX - 1) + 2;
25   int y2 = mScrollY / TILEY + (SCR_FIELDY - 1) + 2;
26   int x, y;
27
28   DrawFrameIfNeeded();
29
30   for (y = DisplayMinY; y <= DisplayMaxY; y++)
31   {
32     for (x = DisplayMinX; x <= DisplayMaxX; x++)
33     {
34       if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
35       {
36         DrawFieldNoAnimated(x, y);
37         DrawFieldAnimated(x, y);
38       }
39     }
40   }
41 }
42
43 static void ScrollPlayfield(int dx, int dy)
44 {
45   int x1 = mScrollX_last / TILEX - 2;
46   int y1 = mScrollY_last / TILEY - 2;
47   int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
48   int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
49   int x, y;
50
51   BlitBitmap(screenBitmap, screenBitmap,
52              TILEX * (dx == -1),
53              TILEY * (dy == -1),
54              (MAX_BUF_XSIZE * TILEX) - TILEX * (dx != 0),
55              (MAX_BUF_YSIZE * TILEY) - TILEY * (dy != 0),
56              TILEX * (dx == 1),
57              TILEY * (dy == 1));
58
59   /* when scrolling the whole playfield, do not redraw single tiles */
60   for (x = 0; x < MAX_BUF_XSIZE; x++)
61     for (y = 0; y < MAX_BUF_YSIZE; y++)
62       redraw[x][y] = FALSE;
63   redraw_tiles = 0;
64
65 #if 1
66   DrawFrameIfNeeded();
67 #endif
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         StretchedSprites.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   int xsize = SXSIZE;
226   int ysize = SYSIZE;
227   int full_xsize = (FieldWidth  - (menBorder.Checked ? 0 : 1)) * TILEX;
228   int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
229
230   sxsize = (full_xsize < xsize ? full_xsize : xsize);
231   sysize = (full_ysize < ysize ? full_ysize : ysize);
232   sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
233   sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
234
235   if (!menBorder.Checked)
236   {
237     px += TILEX / 2;
238     py += TILEY / 2;
239   }
240
241   BlitBitmap(screenBitmap, target_bitmap, px, py, sxsize, sysize, sx, sy);
242 }
243
244 void BackToFront_SP(void)
245 {
246   static boolean scrolling_last = FALSE;
247   int left = mScrollX / TILEX;
248   int top  = mScrollY / TILEY;
249   boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
250   int x, y;
251
252   SyncDisplay();
253
254   if (1 ||
255       redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
256   {
257     BlitScreenToBitmap_SP(window);
258   }
259   else
260   {
261     for (x = 0; x < SCR_FIELDX; x++)
262     {
263       for (y = 0; y < SCR_FIELDY; y++)
264       {
265         int xx = (left + x) % MAX_BUF_XSIZE;
266         int yy = (top  + y) % MAX_BUF_YSIZE;
267
268         if (redraw[xx][yy])
269           BlitBitmap(screenBitmap, window,
270                      xx * TILEX, yy * TILEY, TILEX, TILEY,
271                      SX + x * TILEX, SY + y * TILEY);
272       }
273     }
274   }
275
276   FlushDisplay();
277
278   for (x = 0; x < MAX_BUF_XSIZE; x++)
279     for (y = 0; y < MAX_BUF_YSIZE; y++)
280       redraw[x][y] = FALSE;
281   redraw_tiles = 0;
282
283   scrolling_last = scrolling;
284 }
285
286 void DDScrollBuffer_Blt()
287 {
288   BackToFront_SP();
289 }
290
291 void DDScrollBuffer_ScrollTo(int X, int Y)
292 {
293   if (NoDisplayFlag)
294     return;
295
296   X = X / Stretch;
297   Y = Y / Stretch;
298   mScrollX = X;
299   mScrollY = Y;
300   ScrollX = mScrollX;
301   ScrollY = mScrollY;
302
303 #if 1
304   ScrollPlayfieldIfNeeded();
305 #endif
306 }
307
308 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
309 {
310   double dx, dY, r;
311
312   if (NoDisplayFlag)
313     return;
314
315   X = X / Stretch;
316   Y = Y / Stretch;
317   dx = X - mScrollX;
318   dY = Y - mScrollY;
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   mScrollX = mScrollX + dx * r;
329   mScrollY = mScrollY + dY * r;
330   ScrollX = mScrollX;
331   ScrollY = mScrollY;
332
333 #if 1
334   ScrollPlayfieldIfNeeded();
335 #endif
336 }
337
338 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
339 {
340   double dx, dY;
341   long dT, StepCount;
342   double T, tStep;
343   long oldX, oldY, maxD;
344   static boolean AlreadyRunning = False;
345
346   if (NoDisplayFlag)
347     return;
348
349   if (AlreadyRunning)
350   {
351     return;
352   }
353
354   AlreadyRunning = True;
355   X = X / Stretch;
356   Y = Y / Stretch;
357   dx = X - mScrollX;
358   dY = Y - mScrollY;
359   maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
360   StepCount = FPS * (TimeMS / (double)1000);
361   if (StepCount > maxD)
362     StepCount = maxD;
363
364   if (StepCount == 0)
365     StepCount = 1;
366
367   dT = 1000 / FPS;
368   tStep = (double)1 / StepCount;
369   oldX = mScrollX;
370   oldY = mScrollY;
371   // R = Sqr(dX * dX + dY * dY)
372   // If R = 0 Then Exit Sub 'we are there already
373   for (T = (double)tStep; T <= (double)1; T += tStep)
374   {
375     if (UserDragFlag)
376       goto SoftScrollEH;
377
378     mScrollX = oldX + T * dx;
379     mScrollY = oldY + T * dY;
380     ScrollX = mScrollX;
381     ScrollY = mScrollY;
382   }
383
384   if (UserDragFlag)
385     goto SoftScrollEH;
386
387   mScrollX = X;
388   mScrollY = Y;
389   ScrollX = mScrollX;
390   ScrollY = mScrollY;
391
392 SoftScrollEH:
393   AlreadyRunning = False;
394
395 #if 1
396   ScrollPlayfieldIfNeeded();
397 #endif
398 }