fixed broken screen blitting if cave is smaller than screen for EM engine
[rocksndiamonds.git] / src / game_em / graphics.c
1 /* 2000-08-13T14:36:17Z
2  *
3  * graphics manipulation crap
4  */
5
6 #include "main_em.h"
7
8 #define MIN_SCREEN_XPOS_RAW     0
9 #define MIN_SCREEN_YPOS_RAW     0
10 #define MAX_SCREEN_XPOS_RAW     MAX(0, lev.width  - SCR_FIELDX)
11 #define MAX_SCREEN_YPOS_RAW     MAX(0, lev.height - SCR_FIELDY)
12
13 #define MIN_SCREEN_XPOS         (MIN_SCREEN_XPOS_RAW + CAVE_BUFFER_XOFFSET)
14 #define MIN_SCREEN_YPOS         (MIN_SCREEN_YPOS_RAW + CAVE_BUFFER_YOFFSET)
15 #define MAX_SCREEN_XPOS         (MAX_SCREEN_XPOS_RAW + CAVE_BUFFER_XOFFSET)
16 #define MAX_SCREEN_YPOS         (MAX_SCREEN_YPOS_RAW + CAVE_BUFFER_YOFFSET)
17
18 #define MIN_SCREEN_X            (MIN_SCREEN_XPOS * TILEX)
19 #define MIN_SCREEN_Y            (MIN_SCREEN_YPOS * TILEY)
20 #define MAX_SCREEN_X            (MAX_SCREEN_XPOS * TILEX)
21 #define MAX_SCREEN_Y            (MAX_SCREEN_YPOS * TILEY)
22
23 #define VALID_SCREEN_X(x)       ((x) < MIN_SCREEN_X ? MIN_SCREEN_X :    \
24                                  (x) > MAX_SCREEN_X ? MAX_SCREEN_X : (x))
25 #define VALID_SCREEN_Y(y)       ((y) < MIN_SCREEN_Y ? MIN_SCREEN_Y :    \
26                                  (y) > MAX_SCREEN_Y ? MAX_SCREEN_Y : (y))
27
28 #define PLAYER_POS_X(nr)        (((7 - frame) * ply[nr].oldx +          \
29                                   (1 + frame) * ply[nr].x) * TILEX / 8)
30 #define PLAYER_POS_Y(nr)        (((7 - frame) * ply[nr].oldy +          \
31                                   (1 + frame) * ply[nr].y) * TILEY / 8)
32
33 #define PLAYER_SCREEN_X(nr)     (PLAYER_POS_X(nr) -                     \
34                                  (SCR_FIELDX - 1) * TILEX / 2)
35 #define PLAYER_SCREEN_Y(nr)     (PLAYER_POS_Y(nr) -                     \
36                                  (SCR_FIELDY - 1) * TILEY / 2)
37
38 #define USE_EXTENDED_GRAPHICS_ENGINE            1
39
40
41 int frame;                              /* current screen frame */
42 int screen_x, screen_y;                 /* current scroll position */
43
44 /* tiles currently on screen */
45 static int screen_tiles[MAX_PLAYFIELD_WIDTH + 2][MAX_PLAYFIELD_HEIGHT + 2];
46 static int crumbled_state[MAX_PLAYFIELD_WIDTH + 2][MAX_PLAYFIELD_HEIGHT + 2];
47
48 /* graphic info for game objects/frames and players/actions/frames */
49 struct GraphicInfo_EM graphic_info_em_object[GAME_TILE_MAX][8];
50 struct GraphicInfo_EM graphic_info_em_player[MAX_PLAYERS][PLY_MAX][8];
51
52 int getFieldbufferOffsetX_EM(void)
53 {
54   return screen_x % TILEX;
55 }
56
57 int getFieldbufferOffsetY_EM(void)
58 {
59   return screen_y % TILEY;
60 }
61
62 void BlitScreenToBitmap_EM(Bitmap *target_bitmap)
63 {
64   /* blit all (up to four) parts of the scroll buffer to the target bitmap */
65
66   int x = screen_x % (MAX_BUF_XSIZE * TILEX);
67   int y = screen_y % (MAX_BUF_YSIZE * TILEY);
68   int xsize = SXSIZE;
69   int ysize = SYSIZE;
70   int full_xsize = lev.width  * TILEX;
71   int full_ysize = lev.height * TILEY;
72   int sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
73   int sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
74   int sxsize = (full_xsize < xsize ? full_xsize : xsize);
75   int sysize = (full_ysize < ysize ? full_ysize : ysize);
76   int xxsize = MAX_BUF_XSIZE * TILEX - x;
77   int yysize = MAX_BUF_YSIZE * TILEY - y;
78   int xoffset = 2 * CAVE_BUFFER_XOFFSET * TILEX;
79   int yoffset = 2 * CAVE_BUFFER_YOFFSET * TILEY;
80
81   if (x < xoffset && y < yoffset)
82   {
83     BlitBitmap(screenBitmap, target_bitmap, x, y, sxsize, sysize,
84                sx, sy);
85   }
86   else if (x < xoffset && y >= yoffset)
87   {
88     BlitBitmap(screenBitmap, target_bitmap, x, y, sxsize, yysize,
89                sx, sy);
90     BlitBitmap(screenBitmap, target_bitmap, x, 0, sxsize, y - yoffset,
91                sx, sy + yysize);
92   }
93   else if (x >= xoffset && y < yoffset)
94   {
95     BlitBitmap(screenBitmap, target_bitmap, x, y, xxsize, sysize,
96                sx, sy);
97     BlitBitmap(screenBitmap, target_bitmap, 0, y, x - xoffset, sysize,
98                sx + xxsize, sy);
99   }
100   else
101   {
102     BlitBitmap(screenBitmap, target_bitmap, x, y, xxsize, yysize,
103                sx, sy);
104     BlitBitmap(screenBitmap, target_bitmap, 0, y, x - xoffset, yysize,
105                sx + xxsize, sy);
106     BlitBitmap(screenBitmap, target_bitmap, x, 0, xxsize, y - yoffset,
107                sx, sy + yysize);
108     BlitBitmap(screenBitmap, target_bitmap, 0, 0, x - xoffset, y - yoffset,
109                sx + xxsize, sy + yysize);
110   }
111 }
112
113 static void BackToFront_EM(void)
114 {
115   BlitBitmap(backbuffer, window, SX, SY, SXSIZE, SYSIZE, SX, SY);
116 }
117
118 static struct GraphicInfo_EM *getObjectGraphic(int x, int y)
119 {
120   int tile = lev.draw[x][y];
121   struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
122
123   if (!game.use_native_emc_graphics_engine)
124     getGraphicSourceObjectExt_EM(g, tile, frame, x - lev.left, y - lev.top);
125
126   return g;
127 }
128
129 static struct GraphicInfo_EM *getPlayerGraphic(int player_nr, int anim)
130 {
131   struct GraphicInfo_EM *g = &graphic_info_em_player[player_nr][anim][frame];
132
133   if (!game.use_native_emc_graphics_engine)
134     getGraphicSourcePlayerExt_EM(g, player_nr, anim, frame);
135
136   return g;
137 }
138
139 static void DrawLevelField_EM(int x, int y, int sx, int sy,
140                               boolean draw_masked)
141 {
142   struct GraphicInfo_EM *g = getObjectGraphic(x, y);
143   int src_x = g->src_x + g->src_offset_x * TILESIZE_VAR / TILESIZE;
144   int src_y = g->src_y + g->src_offset_y * TILESIZE_VAR / TILESIZE;
145   int dst_x = sx * TILEX + g->dst_offset_x * TILESIZE_VAR / TILESIZE;
146   int dst_y = sy * TILEY + g->dst_offset_y * TILESIZE_VAR / TILESIZE;
147   int width = g->width * TILESIZE_VAR / TILESIZE;
148   int height = g->height * TILESIZE_VAR / TILESIZE;
149   int left = screen_x / TILEX;
150   int top  = screen_y / TILEY;
151
152   /* do not draw fields that are outside the visible screen area */
153   if (x < left || x >= left + MAX_BUF_XSIZE ||
154       y < top  || y >= top  + MAX_BUF_YSIZE)
155     return;
156
157   if (draw_masked)
158   {
159     if (width > 0 && height > 0)
160       BlitBitmapMasked(g->bitmap, screenBitmap,
161                        src_x, src_y, width, height, dst_x, dst_y);
162   }
163   else
164   {
165     if ((width != TILEX || height != TILEY) && !g->preserve_background)
166       ClearRectangle(screenBitmap, sx * TILEX, sy * TILEY, TILEX, TILEY);
167
168     if (width > 0 && height > 0)
169       BlitBitmap(g->bitmap, screenBitmap,
170                  src_x, src_y, width, height, dst_x, dst_y);
171   }
172 }
173
174 static void DrawLevelFieldCrumbled_EM(int x, int y, int sx, int sy,
175                                       int crm, boolean draw_masked)
176 {
177   struct GraphicInfo_EM *g;
178   int crumbled_border_size;
179   int left = screen_x / TILEX;
180   int top  = screen_y / TILEY;
181   int i;
182
183   /* do not draw fields that are outside the visible screen area */
184   if (x < left || x >= left + MAX_BUF_XSIZE ||
185       y < top  || y >= top  + MAX_BUF_YSIZE)
186     return;
187
188   if (crm == 0)         /* no crumbled edges for this tile */
189     return;
190
191   g = getObjectGraphic(x, y);
192
193   crumbled_border_size =
194     g->crumbled_border_size * TILESIZE_VAR / g->crumbled_tile_size;
195
196   for (i = 0; i < 4; i++)
197   {
198     if (crm & (1 << i))
199     {
200       int width, height, cx, cy;
201
202       if (i == 1 || i == 2)
203       {
204         width = crumbled_border_size;
205         height = TILEY;
206         cx = (i == 2 ? TILEX - crumbled_border_size : 0);
207         cy = 0;
208       }
209       else
210       {
211         width = TILEX;
212         height = crumbled_border_size;
213         cx = 0;
214         cy = (i == 3 ? TILEY - crumbled_border_size : 0);
215       }
216
217       if (width > 0 && height > 0)
218       {
219         int src_x = g->crumbled_src_x + cx;
220         int src_y = g->crumbled_src_y + cy;
221         int dst_x = sx * TILEX + cx;
222         int dst_y = sy * TILEY + cy;
223
224         if (draw_masked)
225           BlitBitmapMasked(g->crumbled_bitmap, screenBitmap,
226                            src_x, src_y, width, height, dst_x, dst_y);
227         else
228           BlitBitmap(g->crumbled_bitmap, screenBitmap,
229                      src_x, src_y, width, height, dst_x, dst_y);
230       }
231     }
232   }
233 }
234
235 static void DrawLevelPlayer_EM(int x1, int y1, int player_nr, int anim,
236                                boolean draw_masked)
237 {
238   struct GraphicInfo_EM *g = getPlayerGraphic(player_nr, anim);
239   int src_x = g->src_x, src_y = g->src_y;
240   int dst_x, dst_y;
241
242   /* do not draw fields that are outside the visible screen area */
243   if (x1 < screen_x - TILEX || x1 >= screen_x + MAX_BUF_XSIZE * TILEX ||
244       y1 < screen_y - TILEY || y1 >= screen_y + MAX_BUF_YSIZE * TILEY)
245     return;
246
247   x1 %= MAX_BUF_XSIZE * TILEX;
248   y1 %= MAX_BUF_YSIZE * TILEY;
249
250   if (draw_masked)
251   {
252     /* draw the player to current location */
253     dst_x = x1;
254     dst_y = y1;
255     BlitBitmapMasked(g->bitmap, screenBitmap,
256                      src_x, src_y, TILEX, TILEY, dst_x, dst_y);
257
258     /* draw the player to opposite wrap-around column */
259     dst_x = x1 - MAX_BUF_XSIZE * TILEX;
260     dst_y = y1;
261     BlitBitmapMasked(g->bitmap, screenBitmap,
262                      g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
263
264     /* draw the player to opposite wrap-around row */
265     dst_x = x1;
266     dst_y = y1 - MAX_BUF_YSIZE * TILEY;
267     BlitBitmapMasked(g->bitmap, screenBitmap,
268                      g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
269   }
270   else
271   {
272     /* draw the player to current location */
273     dst_x = x1;
274     dst_y = y1;
275     BlitBitmap(g->bitmap, screenBitmap,
276                g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
277
278     /* draw the player to opposite wrap-around column */
279     dst_x = x1 - MAX_BUF_XSIZE * TILEX;
280     dst_y = y1;
281     BlitBitmap(g->bitmap, screenBitmap,
282                g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
283
284     /* draw the player to opposite wrap-around row */
285     dst_x = x1;
286     dst_y = y1 - MAX_BUF_YSIZE * TILEY;
287     BlitBitmap(g->bitmap, screenBitmap,
288                g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
289   }
290 }
291
292 /* draw differences between game tiles and screen tiles
293  *
294  * implicitly handles scrolling and restoring background under the sprites
295  */
296
297 static void animscreen(void)
298 {
299   int x, y, i;
300   int left = screen_x / TILEX;
301   int top  = screen_y / TILEY;
302   static int xy[4][2] =
303   {
304     { 0, -1 },
305     { -1, 0 },
306     { +1, 0 },
307     { 0, +1 }
308   };
309
310   if (!game.use_native_emc_graphics_engine)
311     for (y = lev.top; y < lev.bottom; y++)
312       for (x = lev.left; x < lev.right; x++)
313         SetGfxAnimation_EM(&graphic_info_em_object[lev.draw[x][y]][frame],
314                            lev.draw[x][y], frame,
315                            x - lev.left, y - lev.top);
316
317   for (y = top; y < top + MAX_BUF_YSIZE; y++)
318   {
319     for (x = left; x < left + MAX_BUF_XSIZE; x++)
320     {
321       int sx = x % MAX_BUF_XSIZE;
322       int sy = y % MAX_BUF_YSIZE;    
323       int tile = lev.draw[x][y];
324       struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
325       int obj = g->unique_identifier;
326       int crm = 0;
327       boolean redraw_screen_tile = FALSE;
328
329       /* re-calculate crumbled state of this tile */
330       if (g->has_crumbled_graphics)
331       {
332         for (i = 0; i < 4; i++)
333         {
334           int xx = x + xy[i][0];
335           int yy = y + xy[i][1];
336           int tile_next;
337
338           if (xx < 0 || xx >= CAVE_BUFFER_WIDTH ||
339               yy < 0 || yy >= CAVE_BUFFER_HEIGHT)
340             continue;
341
342           tile_next = lev.draw[xx][yy];
343
344           if (!graphic_info_em_object[tile_next][frame].has_crumbled_graphics)
345             crm |= (1 << i);
346         }
347       }
348
349       redraw_screen_tile = (screen_tiles[sx][sy]   != obj ||
350                             crumbled_state[sx][sy] != crm);
351
352       /* only redraw screen tiles if they (or their crumbled state) changed */
353       if (redraw_screen_tile)
354       {
355         DrawLevelField_EM(x, y, sx, sy, FALSE);
356         DrawLevelFieldCrumbled_EM(x, y, sx, sy, crm, FALSE);
357
358         screen_tiles[sx][sy] = obj;
359         crumbled_state[sx][sy] = crm;
360       }
361     }
362   }
363 }
364
365
366 /* blit players to the screen
367  *
368  * handles transparency and movement
369  */
370
371 static void blitplayer(int nr)
372 {
373   int x1, y1, x2, y2;
374
375   if (!ply[nr].alive)
376     return;
377
378   /* x1/y1 are left/top and x2/y2 are right/down part of the player movement */
379   x1 = PLAYER_POS_X(nr);
380   y1 = PLAYER_POS_Y(nr);
381   x2 = x1 + TILEX - 1;
382   y2 = y1 + TILEY - 1;
383
384   if ((int)(x2 - screen_x) < ((MAX_BUF_XSIZE - 1) * TILEX - 1) &&
385       (int)(y2 - screen_y) < ((MAX_BUF_YSIZE - 1) * TILEY - 1))
386   {
387     /* some casts to "int" are needed because of negative calculation values */
388     int dx = (int)ply[nr].x - (int)ply[nr].oldx;
389     int dy = (int)ply[nr].y - (int)ply[nr].oldy;
390     int old_x = (int)ply[nr].oldx + (int)frame * dx / 8;
391     int old_y = (int)ply[nr].oldy + (int)frame * dy / 8;
392     int new_x = old_x + SIGN(dx);
393     int new_y = old_y + SIGN(dy);
394     int old_sx = old_x % MAX_BUF_XSIZE;
395     int old_sy = old_y % MAX_BUF_YSIZE;
396     int new_sx = new_x % MAX_BUF_XSIZE;
397     int new_sy = new_y % MAX_BUF_YSIZE;
398     int new_crm = crumbled_state[new_sx][new_sy];
399
400     /* only diggable elements can be crumbled in the classic EM engine */
401     boolean player_is_digging = (new_crm != 0);
402
403     if (player_is_digging)
404     {
405       /* draw the field the player is moving to (under the player) */
406       DrawLevelField_EM(new_x, new_y, new_sx, new_sy, FALSE);
407       DrawLevelFieldCrumbled_EM(new_x, new_y, new_sx, new_sy, new_crm, FALSE);
408
409       /* draw the player (masked) over the element he is just digging away */
410       DrawLevelPlayer_EM(x1, y1, ply[nr].num, ply[nr].anim, TRUE);
411
412       /* draw the field the player is moving from (masked over the player) */
413       DrawLevelField_EM(old_x, old_y, old_sx, old_sy, TRUE);
414     }
415     else
416     {
417       /* draw the player under the element which is on the same field */
418       DrawLevelPlayer_EM(x1, y1, ply[nr].num, ply[nr].anim, FALSE);
419
420       /* draw the field the player is moving from (masked over the player) */
421       DrawLevelField_EM(old_x, old_y, old_sx, old_sy, TRUE);
422
423       /* draw the field the player is moving to (masked over the player) */
424       DrawLevelField_EM(new_x, new_y, new_sx, new_sy, TRUE);
425     }
426
427     /* redraw screen tiles in the next frame (player may have left the tiles) */
428     screen_tiles[old_sx][old_sy] = -1;
429     screen_tiles[new_sx][new_sy] = -1;
430   }
431 }
432
433 void game_initscreen(void)
434 {
435   int player_nr;
436   int x,y;
437
438   frame = 1;
439
440   player_nr = (game.centered_player_nr != -1 ? game.centered_player_nr : 0);
441
442   screen_x = VALID_SCREEN_X(PLAYER_SCREEN_X(player_nr));
443   screen_y = VALID_SCREEN_Y(PLAYER_SCREEN_Y(player_nr));
444
445   for (y = 0; y < MAX_BUF_YSIZE; y++)
446   {
447     for (x = 0; x < MAX_BUF_XSIZE; x++)
448     {
449       screen_tiles[x][y] = -1;
450       crumbled_state[x][y] = 0;
451     }
452   }
453 }
454
455 static int getMaxCenterDistancePlayerNr(int center_x, int center_y)
456 {
457   int max_dx = 0, max_dy = 0;
458   int player_nr = game_em.last_moving_player;
459   int i;
460
461   for (i = 0; i < MAX_PLAYERS; i++)
462   {
463     if (ply[i].alive)
464     {
465       int sx = PLAYER_SCREEN_X(i);
466       int sy = PLAYER_SCREEN_Y(i);
467
468       if (game_em.last_player_direction[i] != MV_NONE &&
469           (ABS(sx - center_x) > max_dx ||
470            ABS(sy - center_y) > max_dy))
471       {
472         max_dx = MAX(max_dx, ABS(sx - center_x));
473         max_dy = MAX(max_dy, ABS(sy - center_y));
474
475         player_nr = i;
476       }
477     }
478   }
479
480   return player_nr;
481 }
482
483 static void setMinimalPlayerBoundaries(int *sx1, int *sy1, int *sx2, int *sy2)
484 {
485   boolean num_checked_players = 0;
486   int i;
487
488   for (i = 0; i < MAX_PLAYERS; i++)
489   {
490     if (ply[i].alive)
491     {
492       int sx = PLAYER_SCREEN_X(i);
493       int sy = PLAYER_SCREEN_Y(i);
494
495       if (num_checked_players == 0)
496       {
497         *sx1 = *sx2 = sx;
498         *sy1 = *sy2 = sy;
499       }
500       else
501       {
502         *sx1 = MIN(*sx1, sx);
503         *sy1 = MIN(*sy1, sy);
504         *sx2 = MAX(*sx2, sx);
505         *sy2 = MAX(*sy2, sy);
506       }
507
508       num_checked_players++;
509     }
510   }
511 }
512
513 boolean checkIfAllPlayersFitToScreen(void)
514 {
515   int sx1 = 0, sy1 = 0, sx2 = 0, sy2 = 0;
516
517   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
518
519   return (sx2 - sx1 <= SCR_FIELDX * TILEX &&
520           sy2 - sy1 <= SCR_FIELDY * TILEY);
521 }
522
523 static void setScreenCenteredToAllPlayers(int *sx, int *sy)
524 {
525   int sx1 = screen_x, sy1 = screen_y, sx2 = screen_x, sy2 = screen_y;
526
527   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
528
529   *sx = (sx1 + sx2) / 2;
530   *sy = (sy1 + sy2) / 2;
531 }
532
533 static void setMaxCenterDistanceForAllPlayers(int *max_dx, int *max_dy,
534                                               int center_x, int center_y)
535 {
536   int sx1 = center_x, sy1 = center_y, sx2 = center_x, sy2 = center_y;
537
538   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
539
540   *max_dx = MAX(ABS(sx1 - center_x), ABS(sx2 - center_x));
541   *max_dy = MAX(ABS(sy1 - center_y), ABS(sy2 - center_y));
542 }
543
544 static boolean checkIfAllPlayersAreVisible(int center_x, int center_y)
545 {
546   int max_dx, max_dy;
547
548   setMaxCenterDistanceForAllPlayers(&max_dx, &max_dy, center_x, center_y);
549
550   return (max_dx <= SCR_FIELDX * TILEX / 2 &&
551           max_dy <= SCR_FIELDY * TILEY / 2);
552 }
553
554 void RedrawPlayfield_EM(boolean force_redraw)
555 {
556   boolean draw_new_player_location = FALSE;
557   boolean draw_new_player_location_fast = FALSE;
558   boolean quick_relocation = setup.quick_switch;
559   int max_center_distance_player_nr =
560     getMaxCenterDistancePlayerNr(screen_x, screen_y);
561   int stepsize = TILEX / 8;
562   int offset_raw = game.scroll_delay_value;
563   int offset_x = MIN(offset_raw, (SCR_FIELDX - 2) / 2) * TILEX;
564   int offset_y = MIN(offset_raw, (SCR_FIELDY - 2) / 2) * TILEY;
565   int screen_x_old = screen_x;
566   int screen_y_old = screen_y;
567   int x, y, sx, sy;
568   int i;
569
570   if (game.set_centered_player)
571   {
572     boolean all_players_fit_to_screen = checkIfAllPlayersFitToScreen();
573
574     /* switching to "all players" only possible if all players fit to screen */
575     if (game.centered_player_nr_next == -1 && !all_players_fit_to_screen)
576     {
577       game.centered_player_nr_next = game.centered_player_nr;
578       game.set_centered_player = FALSE;
579     }
580
581     /* do not switch focus to non-existing (or non-active) player */
582     if (game.centered_player_nr_next >= 0 &&
583         !ply[game.centered_player_nr_next].alive)
584     {
585       game.centered_player_nr_next = game.centered_player_nr;
586       game.set_centered_player = FALSE;
587     }
588   }
589
590   /* also allow focus switching when screen is scrolled to half tile */
591   if (game.set_centered_player)
592   {
593     game.centered_player_nr = game.centered_player_nr_next;
594
595     draw_new_player_location = TRUE;
596     draw_new_player_location_fast = game.set_centered_player_fast;
597     force_redraw = TRUE;
598
599     game.set_centered_player = FALSE;
600     game.set_centered_player_fast = FALSE;
601   }
602
603   if (game.centered_player_nr == -1)
604   {
605     if (draw_new_player_location || offset_raw == 0)
606     {
607       setScreenCenteredToAllPlayers(&sx, &sy);
608     }
609     else
610     {
611       sx = PLAYER_SCREEN_X(max_center_distance_player_nr);
612       sy = PLAYER_SCREEN_Y(max_center_distance_player_nr);
613     }
614   }
615   else
616   {
617     sx = PLAYER_SCREEN_X(game.centered_player_nr);
618     sy = PLAYER_SCREEN_Y(game.centered_player_nr);
619   }
620
621   if (draw_new_player_location && quick_relocation)
622   {
623     screen_x = VALID_SCREEN_X(sx);
624     screen_y = VALID_SCREEN_Y(sy);
625     screen_x_old = screen_x;
626     screen_y_old = screen_y;
627   }
628
629   if (draw_new_player_location && !quick_relocation)
630   {
631     unsigned int frame_delay_value_old = GetVideoFrameDelay();
632     int wait_delay_value = frame_delay_value_old;
633     int screen_xx = VALID_SCREEN_X(sx);
634     int screen_yy = VALID_SCREEN_Y(sy);
635
636     if (draw_new_player_location_fast)
637       wait_delay_value /= 4;
638
639     SetVideoFrameDelay(wait_delay_value);
640
641     while (screen_x != screen_xx || screen_y != screen_yy)
642     {
643       int dx = (screen_xx < screen_x ? +1 : screen_xx > screen_x ? -1 : 0);
644       int dy = (screen_yy < screen_y ? +1 : screen_yy > screen_y ? -1 : 0);
645       int dxx = 0, dyy = 0;
646
647       if (dx == 0 && dy == 0)           /* no scrolling needed at all */
648         break;
649
650       if (ABS(screen_xx - screen_x) >= TILEX)
651       {
652         screen_x -= dx * TILEX;
653         dxx = dx * TILEX / 2;
654       }
655       else
656       {
657         screen_x = screen_xx;
658         dxx = 0;
659       }
660
661       if (ABS(screen_yy - screen_y) >= TILEY)
662       {
663         screen_y -= dy * TILEY;
664         dyy = dy * TILEY / 2;
665       }
666       else
667       {
668         screen_y = screen_yy;
669         dyy = 0;
670       }
671
672       /* scroll in two steps of half tile size to make things smoother */
673       screen_x += dxx;
674       screen_y += dyy;
675
676       animscreen();
677
678       for (i = 0; i < MAX_PLAYERS; i++)
679         blitplayer(i);
680
681       BlitScreenToBitmap_EM(backbuffer);
682       BackToFront_EM();
683
684       /* scroll second step to align at full tile size */
685       screen_x -= dxx;
686       screen_y -= dyy;
687
688       animscreen();
689
690       for (i = 0; i < MAX_PLAYERS; i++)
691         blitplayer(i);
692
693       BlitScreenToBitmap_EM(backbuffer);
694       BackToFront_EM();
695     }
696
697     SetVideoFrameDelay(frame_delay_value_old);
698
699     screen_x_old = screen_x;
700     screen_y_old = screen_y;
701   }
702
703   if (force_redraw)
704   {
705     for (y = 0; y < MAX_BUF_YSIZE; y++)
706     {
707       for (x = 0; x < MAX_BUF_XSIZE; x++)
708       {
709         screen_tiles[x][y] = -1;
710         crumbled_state[x][y] = 0;
711       }
712     }
713   }
714
715   /* calculate new screen scrolling position, with regard to scroll delay */
716   screen_x = VALID_SCREEN_X(sx + offset_x < screen_x ? sx + offset_x :
717                             sx - offset_x > screen_x ? sx - offset_x :
718                             screen_x);
719   screen_y = VALID_SCREEN_Y(sy + offset_y < screen_y ? sy + offset_y :
720                             sy - offset_y > screen_y ? sy - offset_y :
721                             screen_y);
722
723   /* prevent scrolling further than double player step size when scrolling */
724   if (ABS(screen_x - screen_x_old) > 2 * stepsize)
725   {
726     int dx = SIGN(screen_x - screen_x_old);
727
728     screen_x = screen_x_old + dx * 2 * stepsize;
729   }
730   if (ABS(screen_y - screen_y_old) > 2 * stepsize)
731   {
732     int dy = SIGN(screen_y - screen_y_old);
733
734     screen_y = screen_y_old + dy * 2 * stepsize;
735   }
736
737   /* prevent scrolling away from the other players when focus on all players */
738   if (game.centered_player_nr == -1)
739   {
740     /* check if all players are still visible with new scrolling position */
741     if (checkIfAllPlayersAreVisible(screen_x_old, screen_y_old) &&
742         !checkIfAllPlayersAreVisible(screen_x, screen_y))
743     {
744       /* reset horizontal scroll position to last value, if needed */
745       if (!checkIfAllPlayersAreVisible(screen_x, screen_y_old))
746         screen_x = screen_x_old;
747
748       /* reset vertical scroll position to last value, if needed */
749       if (!checkIfAllPlayersAreVisible(screen_x_old, screen_y))
750         screen_y = screen_y_old;
751     }
752   }
753
754   /* prevent scrolling (for screen correcting) if no player is moving */
755   if (!game_em.any_player_moving)
756   {
757     screen_x = screen_x_old;
758     screen_y = screen_y_old;
759   }
760   else
761   {
762     /* prevent scrolling against the players move direction */
763     int player_nr = (game.centered_player_nr == -1 ?
764                      max_center_distance_player_nr : game.centered_player_nr);
765     int player_move_dir = game_em.last_player_direction[player_nr];
766     int dx = SIGN(screen_x - screen_x_old);
767     int dy = SIGN(screen_y - screen_y_old);
768
769     if ((dx < 0 && player_move_dir != MV_LEFT) ||
770         (dx > 0 && player_move_dir != MV_RIGHT))
771       screen_x = screen_x_old;
772
773     if ((dy < 0 && player_move_dir != MV_UP) ||
774         (dy > 0 && player_move_dir != MV_DOWN))
775       screen_y = screen_y_old;
776   }
777
778   // skip redrawing playfield in warp mode or when testing tapes with "autotest"
779   if (DrawingDeactivatedField())
780     return;
781
782   animscreen();
783
784   for (i = 0; i < MAX_PLAYERS; i++)
785     blitplayer(i);
786 }