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