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