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