1 /* 2000-08-13T14:36:17Z
3 * graphics manipulation crap
8 #define MIN_SCREEN_XPOS 1
9 #define MIN_SCREEN_YPOS 1
10 #define MAX_SCREEN_XPOS MAX(1, lev.width - (SCR_FIELDX - 1))
11 #define MAX_SCREEN_YPOS MAX(1, lev.height - (SCR_FIELDY - 1))
13 #define MIN_SCREEN_X (MIN_SCREEN_XPOS * TILEX)
14 #define MIN_SCREEN_Y (MIN_SCREEN_YPOS * TILEY)
15 #define MAX_SCREEN_X (MAX_SCREEN_XPOS * TILEX)
16 #define MAX_SCREEN_Y (MAX_SCREEN_YPOS * TILEY)
18 #define VALID_SCREEN_X(x) ((x) < MIN_SCREEN_X ? MIN_SCREEN_X : \
19 (x) > MAX_SCREEN_X ? MAX_SCREEN_X : (x))
20 #define VALID_SCREEN_Y(y) ((y) < MIN_SCREEN_Y ? MIN_SCREEN_Y : \
21 (y) > MAX_SCREEN_Y ? MAX_SCREEN_Y : (y))
23 #define PLAYER_SCREEN_X(p) ((( frame) * ply[p].oldx + \
24 (8 - frame) * ply[p].x) * TILEX / 8 \
25 - ((SCR_FIELDX - 1) * TILEX) / 2)
26 #define PLAYER_SCREEN_Y(p) ((( frame) * ply[p].oldy + \
27 (8 - frame) * ply[p].y) * TILEY / 8 \
28 - ((SCR_FIELDY - 1) * TILEY) / 2)
30 #define USE_EXTENDED_GRAPHICS_ENGINE 1
32 int frame; /* current screen frame */
33 int screen_x, screen_y; /* current scroll position */
35 /* tiles currently on screen */
37 static int screentiles[MAX_PLAYFIELD_HEIGHT + 2][MAX_PLAYFIELD_WIDTH + 2];
38 static int crumbled_state[MAX_PLAYFIELD_HEIGHT + 2][MAX_PLAYFIELD_WIDTH + 2];
40 static boolean redraw[MAX_PLAYFIELD_WIDTH + 2][MAX_PLAYFIELD_HEIGHT + 2];
42 static int screentiles[MAX_BUF_YSIZE][MAX_BUF_XSIZE];
43 static int crumbled_state[MAX_BUF_YSIZE][MAX_BUF_XSIZE];
45 static boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
50 int centered_player_nr;
52 static int centered_player_nr;
56 /* copy the entire screen to the window at the scroll position */
58 void BlitScreenToBitmap_EM(Bitmap *target_bitmap)
60 int x = screen_x % (MAX_BUF_XSIZE * TILEX);
61 int y = screen_y % (MAX_BUF_YSIZE * TILEY);
63 if (x < 2 * TILEX && y < 2 * TILEY)
65 BlitBitmap(screenBitmap, target_bitmap, x, y,
66 SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
68 else if (x < 2 * TILEX && y >= 2 * TILEY)
70 BlitBitmap(screenBitmap, target_bitmap, x, y,
71 SCR_FIELDX * TILEX, MAX_BUF_YSIZE * TILEY - y,
73 BlitBitmap(screenBitmap, target_bitmap, x, 0,
74 SCR_FIELDX * TILEX, y - 2 * TILEY,
75 SX, SY + MAX_BUF_YSIZE * TILEY - y);
77 else if (x >= 2 * TILEX && y < 2 * TILEY)
79 BlitBitmap(screenBitmap, target_bitmap, x, y,
80 MAX_BUF_XSIZE * TILEX - x, SCR_FIELDY * TILEY,
82 BlitBitmap(screenBitmap, target_bitmap, 0, y,
83 x - 2 * TILEX, SCR_FIELDY * TILEY,
84 SX + MAX_BUF_XSIZE * TILEX - x, SY);
88 BlitBitmap(screenBitmap, target_bitmap, x, y,
89 MAX_BUF_XSIZE * TILEX - x, MAX_BUF_YSIZE * TILEY - y,
91 BlitBitmap(screenBitmap, target_bitmap, 0, y,
92 x - 2 * TILEX, MAX_BUF_YSIZE * TILEY - y,
93 SX + MAX_BUF_XSIZE * TILEX - x, SY);
94 BlitBitmap(screenBitmap, target_bitmap, x, 0,
95 MAX_BUF_XSIZE * TILEX - x, y - 2 * TILEY,
96 SX, SY + MAX_BUF_YSIZE * TILEY - y);
97 BlitBitmap(screenBitmap, target_bitmap, 0, 0,
98 x - 2 * TILEX, y - 2 * TILEY,
99 SX + MAX_BUF_XSIZE * TILEX - x, SY + MAX_BUF_YSIZE * TILEY - y);
103 void BackToFront_EM(void)
105 static int screen_x_last = -1, screen_y_last = -1;
106 static boolean scrolling_last = FALSE;
107 int left = screen_x / TILEX;
108 int top = screen_y / TILEY;
110 boolean scrolling = (screen_x != screen_x_last || screen_y != screen_y_last);
112 boolean scrolling = (screen_x % TILEX != 0 || screen_y % TILEY != 0);
117 printf("::: %d, %d\n", screen_x, screen_y);
122 if (redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
124 /* blit all (up to four) parts of the scroll buffer to the backbuffer */
125 BlitScreenToBitmap_EM(backbuffer);
127 /* blit the completely updated backbuffer to the window (in one blit) */
128 BlitBitmap(backbuffer, window, SX, SY, SXSIZE, SYSIZE, SX, SY);
134 boolean half_shifted_x = (screen_x % TILEX != 0);
135 boolean half_shifted_y = (screen_y % TILEY != 0);
137 boolean half_shifted_x = (EVEN(SCR_FIELDX) && screen_x % TILEX != 0);
138 boolean half_shifted_y = (EVEN(SCR_FIELDY) && screen_y % TILEY != 0);
143 printf("::: %d, %d\n", EVEN(SCR_FIELDX), screen_x);
145 half_shifted_x = TRUE;
146 half_shifted_y = FALSE;
150 int x1 = 0, x2 = SCR_FIELDX - (half_shifted_x ? 0 : 1);
151 int y1 = 0, y2 = SCR_FIELDY - (half_shifted_y ? 0 : 1);
152 int scroll_xoffset = (half_shifted_x ? TILEX / 2 : 0);
153 int scroll_yoffset = (half_shifted_y ? TILEY / 2 : 0);
155 InitGfxClipRegion(TRUE, SX, SY, SXSIZE, SYSIZE);
157 for (x = x1; x <= x2; x++)
159 for (y = y1; y <= y2; y++)
161 int xx = (left + x) % MAX_BUF_XSIZE;
162 int yy = (top + y) % MAX_BUF_YSIZE;
165 BlitBitmap(screenBitmap, window,
166 xx * TILEX, yy * TILEY, TILEX, TILEY,
167 SX + x * TILEX - scroll_xoffset,
168 SY + y * TILEY - scroll_yoffset);
172 InitGfxClipRegion(FALSE, -1, -1, -1, -1);
176 for (x = 0; x < SCR_FIELDX; x++)
178 for (y = 0; y < SCR_FIELDY; y++)
180 int xx = (left + x) % MAX_BUF_XSIZE;
181 int yy = (top + y) % MAX_BUF_YSIZE;
184 BlitBitmap(screenBitmap, window,
185 xx * TILEX, yy * TILEY, TILEX, TILEY,
186 SX + x * TILEX, SY + y * TILEY);
194 for (x = 0; x < MAX_BUF_XSIZE; x++)
195 for (y = 0; y < MAX_BUF_YSIZE; y++)
196 redraw[x][y] = FALSE;
199 screen_x_last = screen_x;
200 screen_y_last = screen_y;
201 scrolling_last = scrolling;
204 void blitscreen(void)
209 static struct GraphicInfo_EM *getObjectGraphic(int x, int y)
211 int tile = Draw[y][x];
212 struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
214 if (!game.use_native_emc_graphics_engine)
215 getGraphicSourceObjectExt_EM(g, tile, 7 - frame, x - 2, y - 2);
220 static struct GraphicInfo_EM *getPlayerGraphic(int player_nr, int anim)
222 struct GraphicInfo_EM *g = &graphic_info_em_player[player_nr][anim][frame];
224 if (!game.use_native_emc_graphics_engine)
225 getGraphicSourcePlayerExt_EM(g, player_nr, anim, 7 - frame);
230 static void DrawLevelField_EM(int x, int y, int sx, int sy,
233 struct GraphicInfo_EM *g = getObjectGraphic(x, y);
234 int src_x = g->src_x + g->src_offset_x;
235 int src_y = g->src_y + g->src_offset_y;
236 int dst_x = sx * TILEX + g->dst_offset_x;
237 int dst_y = sy * TILEY + g->dst_offset_y;
238 int width = g->width;
239 int height = g->height;
240 int left = screen_x / TILEX;
241 int top = screen_y / TILEY;
243 /* do not draw fields that are outside the visible screen area */
244 if (x < left || x >= left + MAX_BUF_XSIZE ||
245 y < top || y >= top + MAX_BUF_YSIZE)
250 if (width > 0 && height > 0)
252 SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
253 dst_x - src_x, dst_y - src_y);
254 BlitBitmapMasked(g->bitmap, screenBitmap,
255 src_x, src_y, width, height, dst_x, dst_y);
260 if ((width != TILEX || height != TILEY) && !g->preserve_background)
261 ClearRectangle(screenBitmap, sx * TILEX, sy * TILEY, TILEX, TILEY);
263 if (width > 0 && height > 0)
264 BlitBitmap(g->bitmap, screenBitmap,
265 src_x, src_y, width, height, dst_x, dst_y);
269 static void DrawLevelFieldCrumbled_EM(int x, int y, int sx, int sy,
270 int crm, boolean draw_masked)
273 struct GraphicInfo_EM *g;
275 struct GraphicInfo_EM *g = getObjectGraphic(x, y);
277 int left = screen_x / TILEX;
278 int top = screen_y / TILEY;
281 /* do not draw fields that are outside the visible screen area */
282 if (x < left || x >= left + MAX_BUF_XSIZE ||
283 y < top || y >= top + MAX_BUF_YSIZE)
286 if (crm == 0) /* no crumbled edges for this tile */
290 g = getObjectGraphic(x, y);
294 if (x == 3 && y == 3 && frame == 0)
295 printf("::: %d, %d\n",
296 graphic_info_em_object[207][0].crumbled_src_x,
297 graphic_info_em_object[207][0].crumbled_src_y);
300 for (i = 0; i < 4; i++)
304 int width, height, cx, cy;
306 if (i == 1 || i == 2)
308 width = g->crumbled_border_size;
310 cx = (i == 2 ? TILEX - g->crumbled_border_size : 0);
316 height = g->crumbled_border_size;
318 cy = (i == 3 ? TILEY - g->crumbled_border_size : 0);
321 if (width > 0 && height > 0)
323 int src_x = g->crumbled_src_x + cx;
324 int src_y = g->crumbled_src_y + cy;
325 int dst_x = sx * TILEX + cx;
326 int dst_y = sy * TILEY + cy;
330 SetClipOrigin(g->crumbled_bitmap, g->crumbled_bitmap->stored_clip_gc,
331 dst_x - src_x, dst_y - src_y);
332 BlitBitmapMasked(g->crumbled_bitmap, screenBitmap,
333 src_x, src_y, width, height, dst_x, dst_y);
336 BlitBitmap(g->crumbled_bitmap, screenBitmap,
337 src_x, src_y, width, height, dst_x, dst_y);
343 static void DrawLevelPlayer_EM(int x1, int y1, int player_nr, int anim,
346 struct GraphicInfo_EM *g = getPlayerGraphic(player_nr, anim);
347 int src_x = g->src_x, src_y = g->src_y;
350 /* do not draw fields that are outside the visible screen area */
351 if (x1 < screen_x - TILEX || x1 >= screen_x + MAX_BUF_XSIZE * TILEX ||
352 y1 < screen_y - TILEY || y1 >= screen_y + MAX_BUF_YSIZE * TILEY)
355 x1 %= MAX_BUF_XSIZE * TILEX;
356 y1 %= MAX_BUF_YSIZE * TILEY;
360 /* draw the player to current location */
363 SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
364 dst_x - src_x, dst_y - src_y);
365 BlitBitmapMasked(g->bitmap, screenBitmap,
366 src_x, src_y, TILEX, TILEY, dst_x, dst_y);
368 /* draw the player to opposite wrap-around column */
369 dst_x = x1 - MAX_BUF_XSIZE * TILEX;
371 SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
372 dst_x - src_x, dst_y - src_y);
373 BlitBitmapMasked(g->bitmap, screenBitmap,
374 g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
376 /* draw the player to opposite wrap-around row */
378 dst_y = y1 - MAX_BUF_YSIZE * TILEY;
379 SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
380 dst_x - src_x, dst_y - src_y);
381 BlitBitmapMasked(g->bitmap, screenBitmap,
382 g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
386 /* draw the player to current location */
389 BlitBitmap(g->bitmap, screenBitmap,
390 g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
392 /* draw the player to opposite wrap-around column */
393 dst_x = x1 - MAX_BUF_XSIZE * TILEX;
395 BlitBitmap(g->bitmap, screenBitmap,
396 g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
398 /* draw the player to opposite wrap-around row */
400 dst_y = y1 - MAX_BUF_YSIZE * TILEY;
401 BlitBitmap(g->bitmap, screenBitmap,
402 g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
406 /* draw differences between game tiles and screen tiles
408 * implicitly handles scrolling and restoring background under the sprites
411 static void animscreen(void)
414 int left = screen_x / TILEX;
415 int top = screen_y / TILEY;
416 static int xy[4][2] =
424 if (!game.use_native_emc_graphics_engine)
425 for (y = 2; y < EM_MAX_CAVE_HEIGHT - 2; y++)
426 for (x = 2; x < EM_MAX_CAVE_WIDTH - 2; x++)
427 SetGfxAnimation_EM(&graphic_info_em_object[Draw[y][x]][frame],
428 Draw[y][x], 7 - frame, x - 2, y - 2);
430 for (y = top; y < top + MAX_BUF_YSIZE; y++)
432 for (x = left; x < left + MAX_BUF_XSIZE; x++)
434 int sx = x % MAX_BUF_XSIZE;
435 int sy = y % MAX_BUF_YSIZE;
436 int tile = Draw[y][x];
437 struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
438 int obj = g->unique_identifier;
440 boolean redraw_screen_tile = FALSE;
442 /* re-calculate crumbled state of this tile */
443 if (g->has_crumbled_graphics)
445 for (i = 0; i < 4; i++)
447 int xx = x + xy[i][0];
448 int yy = y + xy[i][1];
451 if (xx < 0 || xx >= EM_MAX_CAVE_WIDTH ||
452 yy < 0 || yy >= EM_MAX_CAVE_HEIGHT)
455 tile_next = Draw[yy][xx];
457 if (!graphic_info_em_object[tile_next][frame].has_crumbled_graphics)
462 redraw_screen_tile = (screentiles[sy][sx] != obj ||
463 crumbled_state[sy][sx] != crm);
466 /* !!! TEST ONLY -- CHANGE THIS !!! */
467 if (!game.use_native_emc_graphics_engine)
468 redraw_screen_tile = TRUE;
471 /* only redraw screen tiles if they (or their crumbled state) changed */
472 if (redraw_screen_tile)
474 DrawLevelField_EM(x, y, sx, sy, FALSE);
475 DrawLevelFieldCrumbled_EM(x, y, sx, sy, crm, FALSE);
477 screentiles[sy][sx] = obj;
478 crumbled_state[sy][sx] = crm;
480 redraw[sx][sy] = TRUE;
488 /* blit players to the screen
490 * handles transparency and movement
493 static void blitplayer(struct PLAYER *ply)
500 /* x1/y1 are left/top and x2/y2 are right/down part of the player movement */
501 x1 = (frame * ply->oldx + (8 - frame) * ply->x) * TILEX / 8;
502 y1 = (frame * ply->oldy + (8 - frame) * ply->y) * TILEY / 8;
507 printf("::: %d, %d\n", x1, y1);
510 if ((int)(x2 - screen_x) < ((MAX_BUF_XSIZE - 1) * TILEX - 1) &&
511 (int)(y2 - screen_y) < ((MAX_BUF_YSIZE - 1) * TILEY - 1))
513 /* some casts to "int" are needed because of negative calculation values */
514 int dx = (int)ply->x - (int)ply->oldx;
515 int dy = (int)ply->y - (int)ply->oldy;
516 int old_x = (int)ply->oldx + (7 - (int)frame) * dx / 8;
517 int old_y = (int)ply->oldy + (7 - (int)frame) * dy / 8;
518 int new_x = old_x + SIGN(dx);
519 int new_y = old_y + SIGN(dy);
520 int old_sx = old_x % MAX_BUF_XSIZE;
521 int old_sy = old_y % MAX_BUF_XSIZE;
522 int new_sx = new_x % MAX_BUF_XSIZE;
523 int new_sy = new_y % MAX_BUF_XSIZE;
525 int old_crm = crumbled_state[old_sy][old_sx];
527 int new_crm = crumbled_state[new_sy][new_sx];
529 /* only diggable elements can be crumbled in the classic EM engine */
530 boolean player_is_digging = (new_crm != 0);
533 x1 %= MAX_BUF_XSIZE * TILEX;
534 y1 %= MAX_BUF_YSIZE * TILEY;
535 x2 %= MAX_BUF_XSIZE * TILEX;
536 y2 %= MAX_BUF_YSIZE * TILEY;
539 if (player_is_digging)
542 /* draw the field the player is moving from (under the player) */
543 DrawLevelField_EM(old_x, old_y, old_sx, old_sy, FALSE);
544 DrawLevelFieldCrumbled_EM(old_x, old_y, old_sx, old_sy, old_crm, FALSE);
547 /* draw the field the player is moving to (under the player) */
548 DrawLevelField_EM(new_x, new_y, new_sx, new_sy, FALSE);
549 DrawLevelFieldCrumbled_EM(new_x, new_y, new_sx, new_sy, new_crm, FALSE);
551 /* draw the player (masked) over the element he is just digging away */
552 DrawLevelPlayer_EM(x1, y1, ply->num, ply->anim, TRUE);
555 /* draw the field the player is moving from (masked over the player) */
556 DrawLevelField_EM(old_x, old_y, old_sx, old_sy, TRUE);
561 /* draw the player under the element which is on the same field */
562 DrawLevelPlayer_EM(x1, y1, ply->num, ply->anim, FALSE);
564 /* draw the field the player is moving from (masked over the player) */
565 DrawLevelField_EM(old_x, old_y, old_sx, old_sy, TRUE);
567 /* draw the field the player is moving to (masked over the player) */
568 DrawLevelField_EM(new_x, new_y, new_sx, new_sy, TRUE);
571 /* redraw screen tiles in the next frame (player may have left the tiles) */
572 screentiles[old_sy][old_sx] = -1;
573 screentiles[new_sy][new_sx] = -1;
575 /* mark screen tiles as dirty (force screen refresh with changed content) */
576 redraw[old_sx][old_sy] = TRUE;
577 redraw[new_sx][new_sy] = TRUE;
582 void game_initscreen(void)
585 int dynamite_state = ply[0].dynamite; /* !!! ONLY PLAYER 1 !!! */
586 int all_keys_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
592 game.centered_player_nr = getCenteredPlayerNr_EM();
595 player_nr = (game.centered_player_nr != -1 ? game.centered_player_nr : 0);
597 screen_x = VALID_SCREEN_X(PLAYER_SCREEN_X(player_nr));
598 screen_y = VALID_SCREEN_Y(PLAYER_SCREEN_Y(player_nr));
600 for (y = 0; y < MAX_BUF_YSIZE; y++)
602 for (x = 0; x < MAX_BUF_XSIZE; x++)
604 screentiles[y][x] = -1;
605 crumbled_state[y][x] = 0;
609 DrawAllGameValues(lev.required, dynamite_state, lev.score,
610 lev.time, all_keys_state);
614 void DrawRelocatePlayer(struct PlayerInfo *player, boolean quick_relocation)
616 boolean ffwd_delay = (tape.playing && tape.fast_forward);
617 boolean no_delay = (tape.warp_forward);
618 int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay);
619 int wait_delay_value = (no_delay ? 0 : frame_delay_value);
623 if (quick_relocation)
625 int offset = game.scroll_delay_value;
627 if (!IN_VIS_FIELD(SCREENX(jx), SCREENY(jy)))
629 scroll_x = (player->jx < SBX_Left + MIDPOSX ? SBX_Left :
630 player->jx > SBX_Right + MIDPOSX ? SBX_Right :
631 player->jx - MIDPOSX);
633 scroll_y = (player->jy < SBY_Upper + MIDPOSY ? SBY_Upper :
634 player->jy > SBY_Lower + MIDPOSY ? SBY_Lower :
635 player->jy - MIDPOSY);
639 if ((player->MovDir == MV_LEFT && scroll_x > jx - MIDPOSX + offset) ||
640 (player->MovDir == MV_RIGHT && scroll_x < jx - MIDPOSX - offset))
641 scroll_x = jx - MIDPOSX + (scroll_x < jx-MIDPOSX ? -offset : +offset);
643 if ((player->MovDir == MV_UP && scroll_y > jy - MIDPOSY + offset) ||
644 (player->MovDir == MV_DOWN && scroll_y < jy - MIDPOSY - offset))
645 scroll_y = jy - MIDPOSY + (scroll_y < jy-MIDPOSY ? -offset : +offset);
647 /* don't scroll over playfield boundaries */
648 if (scroll_x < SBX_Left || scroll_x > SBX_Right)
649 scroll_x = (scroll_x < SBX_Left ? SBX_Left : SBX_Right);
651 /* don't scroll over playfield boundaries */
652 if (scroll_y < SBY_Upper || scroll_y > SBY_Lower)
653 scroll_y = (scroll_y < SBY_Upper ? SBY_Upper : SBY_Lower);
656 RedrawPlayfield(TRUE, 0,0,0,0);
660 int scroll_xx = -999, scroll_yy = -999;
662 ScrollScreen(NULL, SCROLL_GO_ON); /* scroll last frame to full tile */
664 while (scroll_xx != scroll_x || scroll_yy != scroll_y)
667 int fx = FX, fy = FY;
669 scroll_xx = (player->jx < SBX_Left + MIDPOSX ? SBX_Left :
670 player->jx > SBX_Right + MIDPOSX ? SBX_Right :
671 player->jx - MIDPOSX);
673 scroll_yy = (player->jy < SBY_Upper + MIDPOSY ? SBY_Upper :
674 player->jy > SBY_Lower + MIDPOSY ? SBY_Lower :
675 player->jy - MIDPOSY);
677 dx = (scroll_xx < scroll_x ? +1 : scroll_xx > scroll_x ? -1 : 0);
678 dy = (scroll_yy < scroll_y ? +1 : scroll_yy > scroll_y ? -1 : 0);
680 if (dx == 0 && dy == 0) /* no scrolling needed at all */
686 fx += dx * TILEX / 2;
687 fy += dy * TILEY / 2;
692 /* scroll in two steps of half tile size to make things smoother */
693 BlitBitmap(drawto_field, window, fx, fy, SXSIZE, SYSIZE, SX, SY);
695 Delay(wait_delay_value);
697 /* scroll second step to align at full tile size */
699 Delay(wait_delay_value);
704 Delay(wait_delay_value);
709 static int getMaxCenterDistancePlayerNr(int center_x, int center_y)
711 int max_dx = 0, max_dy = 0;
712 int player_nr = game_em.last_moving_player;
715 for (i = 0; i < MAX_PLAYERS; i++)
719 int sx = PLAYER_SCREEN_X(i);
720 int sy = PLAYER_SCREEN_Y(i);
722 if (game_em.last_player_direction[i] != MV_NONE &&
723 (ABS(sx - center_x) > max_dx ||
724 ABS(sy - center_y) > max_dy))
726 max_dx = MAX(max_dx, ABS(sx - center_x));
727 max_dy = MAX(max_dy, ABS(sy - center_y));
737 static void setMinimalPlayerBoundaries(int *sx1, int *sy1, int *sx2, int *sy2)
739 boolean num_checked_players = 0;
742 for (i = 0; i < MAX_PLAYERS; i++)
746 int sx = PLAYER_SCREEN_X(i);
747 int sy = PLAYER_SCREEN_Y(i);
749 if (num_checked_players == 0)
756 *sx1 = MIN(*sx1, sx);
757 *sy1 = MIN(*sy1, sy);
758 *sx2 = MAX(*sx2, sx);
759 *sy2 = MAX(*sy2, sy);
762 num_checked_players++;
767 boolean checkIfAllPlayersFitToScreen()
769 int sx1 = 0, sy1 = 0, sx2 = 0, sy2 = 0;
771 setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
773 return (sx2 - sx1 <= SCR_FIELDX * TILEX &&
774 sy2 - sy1 <= SCR_FIELDY * TILEY);
777 static void setScreenCenteredToAllPlayers(int *sx, int *sy)
779 int sx1 = screen_x, sy1 = screen_y, sx2 = screen_x, sy2 = screen_y;
781 setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
783 *sx = (sx1 + sx2) / 2;
784 *sy = (sy1 + sy2) / 2;
787 static void setMaxCenterDistanceForAllPlayers(int *max_dx, int *max_dy,
788 int center_x, int center_y)
790 int sx1 = center_x, sy1 = center_y, sx2 = center_x, sy2 = center_y;
792 setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
794 *max_dx = MAX(ABS(sx1 - center_x), ABS(sx2 - center_x));
795 *max_dy = MAX(ABS(sy1 - center_y), ABS(sy2 - center_y));
798 static boolean checkIfAllPlayersAreVisible(int center_x, int center_y)
802 setMaxCenterDistanceForAllPlayers(&max_dx, &max_dy, center_x, center_y);
804 return (max_dx <= SCR_FIELDX * TILEX / 2 &&
805 max_dy <= SCR_FIELDY * TILEY / 2);
808 void RedrawPlayfield_EM(boolean force_redraw)
811 boolean all_players_visible = checkIfAllPlayersAreVisible();
813 boolean draw_new_player_location = FALSE;
814 boolean quick_relocation = setup.quick_switch;
816 boolean scrolling = (screen_x % TILEX != 0 || screen_y % TILEY != 0);
819 boolean game.set_centered_player = getSetCenteredPlayer_EM();
820 int game.centered_player_nr_next = getCenteredPlayerNr_EM();
823 int max_center_distance_player_nr =
824 getMaxCenterDistancePlayerNr(screen_x, screen_y);
826 int player_nr = game_em.last_moving_player;
828 int stepsize = TILEX / 8;
829 int offset = game.scroll_delay_value * TILEX;
830 int offset_x = offset;
831 int offset_y = offset;
832 int screen_x_old = screen_x;
833 int screen_y_old = screen_y;
837 if (game.set_centered_player)
839 boolean all_players_fit_to_screen = checkIfAllPlayersFitToScreen();
841 /* switching to "all players" only possible if all players fit to screen */
842 if (game.centered_player_nr_next == -1 && !all_players_fit_to_screen)
844 game.centered_player_nr_next = game.centered_player_nr;
845 game.set_centered_player = FALSE;
848 /* do not switch focus to non-existing (or non-active) player */
849 if (game.centered_player_nr_next >= 0 &&
850 !ply[game.centered_player_nr_next].alive)
852 game.centered_player_nr_next = game.centered_player_nr;
853 game.set_centered_player = FALSE;
858 /* also allow focus switching when screen is scrolled to half tile */
860 if (!scrolling) /* screen currently aligned at tile position */
864 if (game.set_centered_player)
866 if (game.centered_player_nr != game.centered_player_nr_next)
869 game.centered_player_nr = game.centered_player_nr_next;
871 draw_new_player_location = TRUE;
874 game.set_centered_player = FALSE;
878 if (game.centered_player_nr == -1)
881 if (draw_new_player_location || offset == 0)
883 if (draw_new_player_location)
886 setScreenCenteredToAllPlayers(&sx, &sy);
891 sx = PLAYER_SCREEN_X(max_center_distance_player_nr);
892 sy = PLAYER_SCREEN_Y(max_center_distance_player_nr);
894 sx = PLAYER_SCREEN_X(game_em.last_moving_player);
895 sy = PLAYER_SCREEN_Y(game_em.last_moving_player);
901 sx = PLAYER_SCREEN_X(game.centered_player_nr);
902 sy = PLAYER_SCREEN_Y(game.centered_player_nr);
905 if (draw_new_player_location && quick_relocation)
907 screen_x = VALID_SCREEN_X(sx);
908 screen_y = VALID_SCREEN_Y(sy);
909 screen_x_old = screen_x;
910 screen_y_old = screen_y;
918 if (draw_new_player_location && !quick_relocation)
921 unsigned long game_frame_delay_value = getGameFrameDelay_EM(20);
923 unsigned long game_frame_delay_value = getGameFrameDelay_EM(25);
925 int wait_delay_value = game_frame_delay_value;
926 int screen_xx = VALID_SCREEN_X(sx);
927 int screen_yy = VALID_SCREEN_Y(sy);
929 while (screen_x != screen_xx || screen_y != screen_yy)
931 int dx = (screen_xx < screen_x ? +1 : screen_xx > screen_x ? -1 : 0);
932 int dy = (screen_yy < screen_y ? +1 : screen_yy > screen_y ? -1 : 0);
933 int dxx = 0, dyy = 0;
935 if (dx == 0 && dy == 0) /* no scrolling needed at all */
940 if (ABS(screen_xx - screen_x) >= TILEX)
942 screen_x -= dx * TILEX;
943 dxx = dx * TILEX / 2;
947 screen_x = screen_xx;
951 if (ABS(screen_yy - screen_y) >= TILEY)
953 screen_y -= dy * TILEY;
954 dyy = dy * TILEY / 2;
958 screen_y = screen_yy;
965 if (ABS(screen_xx - screen_x) >= TILEX ||
966 ABS(screen_yy - screen_y) >= TILEY)
968 screen_x -= dx * TILEX;
969 screen_y -= dy * TILEY;
971 dxx = dx * TILEX / 2;
972 dyy = dy * TILEY / 2;
976 screen_x = screen_xx;
977 screen_y = screen_yy;
983 screen_x -= dx * TILEX;
984 screen_y -= dy * TILEY;
986 dxx += dx * TILEX / 2;
987 dyy += dy * TILEY / 2;
992 /* scroll in two steps of half tile size to make things smoother */
998 for (i = 0; i < MAX_PLAYERS; i++)
1003 Delay(wait_delay_value);
1005 /* scroll second step to align at full tile size */
1015 for (i = 0; i < MAX_PLAYERS; i++)
1016 blitplayer(&ply[i]);
1020 Delay(wait_delay_value);
1023 screen_x_old = screen_x;
1024 screen_y_old = screen_y;
1029 for (y = 0; y < MAX_BUF_YSIZE; y++)
1031 for (x = 0; x < MAX_BUF_XSIZE; x++)
1033 screentiles[y][x] = -1;
1034 crumbled_state[y][x] = 0;
1039 /* calculate new screen scrolling position, with regard to scroll delay */
1040 screen_x = VALID_SCREEN_X(sx + offset_x < screen_x ? sx + offset_x :
1041 sx - offset_x > screen_x ? sx - offset_x :
1043 screen_y = VALID_SCREEN_Y(sy + offset_y < screen_y ? sy + offset_y :
1044 sy - offset_y > screen_y ? sy - offset_y :
1048 printf("::: (%d, %d) => (%d, %d) [(%d, %d), (%d, %d)] [%d, %d] [%d / %d]\n",
1049 screen_x_old, screen_y_old,
1051 ply[max_center_distance_player_nr].oldx,
1052 ply[max_center_distance_player_nr].x,
1053 ply[max_center_distance_player_nr].oldy,
1054 ply[max_center_distance_player_nr].y,
1056 ABS(screen_x - screen_x_old),
1057 ABS(screen_y - screen_y_old));
1063 /* prevent scrolling further than double player step size when scrolling */
1064 if (ABS(screen_x - screen_x_old) > 2 * stepsize)
1066 int dx = SIGN(screen_x - screen_x_old);
1068 screen_x = screen_x_old + dx * 2 * stepsize;
1070 if (ABS(screen_y - screen_y_old) > 2 * stepsize)
1072 int dy = SIGN(screen_y - screen_y_old);
1074 screen_y = screen_y_old + dy * 2 * stepsize;
1077 /* prevent scrolling further than double player step size when scrolling */
1078 if (ABS(screen_x - screen_x_old) > 2 * stepsize ||
1079 ABS(screen_y - screen_y_old) > 2 * stepsize)
1081 int dx = SIGN(screen_x - screen_x_old);
1082 int dy = SIGN(screen_y - screen_y_old);
1084 screen_x = screen_x_old + dx * 2 * stepsize;
1085 screen_y = screen_y_old + dy * 2 * stepsize;
1090 /* prevent scrolling further than player step size when scrolling */
1091 if (ABS(screen_x - screen_x_old) > stepsize ||
1092 ABS(screen_y - screen_y_old) > stepsize)
1094 int dx = SIGN(screen_x - screen_x_old);
1095 int dy = SIGN(screen_y - screen_y_old);
1097 screen_x = screen_x_old + dx * stepsize;
1098 screen_y = screen_y_old + dy * stepsize;
1102 /* prevent scrolling away from the other players when focus on all players */
1103 if (game.centered_player_nr == -1)
1106 /* check if all players are still visible with new scrolling position */
1107 if (checkIfAllPlayersAreVisible(screen_x_old, screen_y_old) &&
1108 !checkIfAllPlayersAreVisible(screen_x, screen_y))
1110 /* reset horizontal scroll position to last value, if needed */
1111 if (!checkIfAllPlayersAreVisible(screen_x, screen_y_old))
1112 screen_x = screen_x_old;
1114 /* reset vertical scroll position to last value, if needed */
1115 if (!checkIfAllPlayersAreVisible(screen_x_old, screen_y))
1116 screen_y = screen_y_old;
1119 boolean all_players_visible = checkIfAllPlayersAreVisible();
1121 if (!all_players_visible)
1123 printf("::: not all players visible\n");
1125 screen_x = screen_x_old;
1126 screen_y = screen_y_old;
1131 /* prevent scrolling (for screen correcting) if no player is moving */
1132 if (!game_em.any_player_moving)
1134 screen_x = screen_x_old;
1135 screen_y = screen_y_old;
1139 /* prevent scrolling against the players move direction */
1141 int player_nr = game_em.last_moving_player;
1143 int player_nr = (game.centered_player_nr == -1 ?
1144 max_center_distance_player_nr : game.centered_player_nr);
1145 int player_move_dir = game_em.last_player_direction[player_nr];
1146 int dx = SIGN(screen_x - screen_x_old);
1147 int dy = SIGN(screen_y - screen_y_old);
1149 if ((dx < 0 && player_move_dir != MV_LEFT) ||
1150 (dx > 0 && player_move_dir != MV_RIGHT))
1151 screen_x = screen_x_old;
1153 if ((dy < 0 && player_move_dir != MV_UP) ||
1154 (dy > 0 && player_move_dir != MV_DOWN))
1155 screen_y = screen_y_old;
1160 for (i = 0; i < MAX_PLAYERS; i++)
1161 blitplayer(&ply[i]);
1172 void game_animscreen(void)
1174 RedrawPlayfield_EM(FALSE);
1177 void DrawGameDoorValues_EM()
1183 int dynamite_state = ply[0].dynamite; /* !!! ONLY PLAYER 1 !!! */
1184 int key_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
1188 if (game.centered_player_nr == -1)
1196 for (i = 0; i < MAX_PLAYERS; i++)
1198 dynamite_state += ply[i].dynamite;
1199 key_state |= ply[i].keys;
1204 dynamite_state = ply[0].dynamite; /* !!! ONLY PLAYER 1 !!! */
1205 key_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
1210 int player_nr = game.centered_player_nr;
1212 dynamite_state = ply[player_nr].dynamite;
1213 key_state = ply[player_nr].keys;
1218 DrawAllGameValues(lev.required, dynamite_state, lev.score,
1219 lev.time, key_state);
1221 DrawAllGameValues(lev.required, ply1.dynamite, lev.score,
1222 DISPLAY_TIME(lev.time), ply1.keys | ply2.keys);