d90835b3e50ef804e0d220600cec84b35fea16bd
[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 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
201 {
202   /* copy playfield buffer to target bitmap at scroll position */
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 DDScrollBuffer_ScrollTo(int X, int Y)
234 {
235   if (NoDisplayFlag)
236     return;
237
238   ScrollX = mScrollX = X;
239   ScrollY = mScrollY = Y;
240
241   ScrollPlayfieldIfNeeded();
242 }
243
244 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
245 {
246   double dx, dY, r;
247
248   if (NoDisplayFlag)
249     return;
250
251   dx = X - mScrollX;
252   dY = Y - mScrollY;
253
254   r = Sqr(dx * dx + dY * dY);
255   if (r == 0)   // we are there already
256     return;
257
258   if (Step < r)
259     r = Step / r;
260   else
261     r = 1;
262
263   ScrollX = mScrollX = mScrollX + dx * r;
264   ScrollY = mScrollY = mScrollY + dY * r;
265
266   ScrollPlayfieldIfNeeded();
267 }
268
269 void DDScrollBuffer_SoftScrollTo(int X, int Y, int TimeMS, int FPS)
270 {
271   double dx, dY;
272   int StepCount;
273   double T, tStep;
274   int oldX, oldY, maxD;
275   static boolean AlreadyRunning = False;
276
277   if (NoDisplayFlag)
278     return;
279
280   if (AlreadyRunning)
281     return;
282
283   AlreadyRunning = True;
284
285   dx = X - mScrollX;
286   dY = Y - mScrollY;
287   maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
288
289   StepCount = FPS * (TimeMS / (double)1000);
290   if (StepCount > maxD)
291     StepCount = maxD;
292
293   if (StepCount == 0)
294     StepCount = 1;
295
296   tStep = (double)1 / StepCount;
297   oldX = mScrollX;
298   oldY = mScrollY;
299
300   for (T = (double)tStep; T <= (double)1; T += tStep)
301   {
302     ScrollX = mScrollX = oldX + T * dx;
303     ScrollY = mScrollY = oldY + T * dY;
304   }
305
306   ScrollX = mScrollX = X;
307   ScrollY = mScrollY = Y;
308
309   AlreadyRunning = False;
310
311   ScrollPlayfieldIfNeeded();
312 }