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