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