1 /* 2000-08-13T14:36:17Z
3 * graphics manipulation crap
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))
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)
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))
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)
33 int frame; /* current screen frame */
35 int screen_x; /* current scroll position */
38 int screen_x; /* current scroll position */
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];
46 static boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
49 /* copy the entire screen to the window at the scroll position
51 * perhaps use mit-shm to speed this up
54 void BlitScreenToBitmap_EM(Bitmap *target_bitmap)
56 int x = screen_x % (MAX_BUF_XSIZE * TILEX);
57 int y = screen_y % (MAX_BUF_YSIZE * TILEY);
59 if (x < 2 * TILEX && y < 2 * TILEY)
61 BlitBitmap(screenBitmap, target_bitmap, x, y,
62 SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
64 else if (x < 2 * TILEX && y >= 2 * TILEY)
66 BlitBitmap(screenBitmap, target_bitmap, x, y,
67 SCR_FIELDX * TILEX, MAX_BUF_YSIZE * TILEY - y,
69 BlitBitmap(screenBitmap, target_bitmap, x, 0,
70 SCR_FIELDX * TILEX, y - 2 * TILEY,
71 SX, SY + MAX_BUF_YSIZE * TILEY - y);
73 else if (x >= 2 * TILEX && y < 2 * TILEY)
75 BlitBitmap(screenBitmap, target_bitmap, x, y,
76 MAX_BUF_XSIZE * TILEX - x, SCR_FIELDY * TILEY,
78 BlitBitmap(screenBitmap, target_bitmap, 0, y,
79 x - 2 * TILEX, SCR_FIELDY * TILEY,
80 SX + MAX_BUF_XSIZE * TILEX - x, SY);
84 BlitBitmap(screenBitmap, target_bitmap, x, y,
85 MAX_BUF_XSIZE * TILEX - x, MAX_BUF_YSIZE * TILEY - y,
87 BlitBitmap(screenBitmap, target_bitmap, 0, y,
88 x - 2 * TILEX, MAX_BUF_YSIZE * TILEY - y,
89 SX + MAX_BUF_XSIZE * TILEX - x, SY);
90 BlitBitmap(screenBitmap, target_bitmap, x, 0,
91 MAX_BUF_XSIZE * TILEX - x, y - 2 * TILEY,
92 SX, SY + MAX_BUF_YSIZE * TILEY - y);
93 BlitBitmap(screenBitmap, target_bitmap, 0, 0,
94 x - 2 * TILEX, y - 2 * TILEY,
95 SX + MAX_BUF_XSIZE * TILEX - x, SY + MAX_BUF_YSIZE * TILEY - y);
103 static boolean scrolling_last = FALSE;
104 int left = screen_x / TILEX;
105 int top = screen_y / TILEY;
106 boolean scrolling = (screen_x % TILEX != 0 || screen_y % TILEY != 0);
111 if (redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
113 /* blit all (up to four) parts of the scroll buffer to the backbuffer */
114 BlitScreenToBitmap_EM(backbuffer);
116 /* blit the completely updated backbuffer to the window (in one blit) */
117 BlitBitmap(backbuffer, window, SX, SY, SXSIZE, SYSIZE, SX, SY);
121 for (x = 0; x < SCR_FIELDX; x++)
123 for (y = 0; y < SCR_FIELDY; y++)
125 int xx = (left + x) % MAX_BUF_XSIZE;
126 int yy = (top + y) % MAX_BUF_YSIZE;
129 BlitBitmap(screenBitmap, window,
130 xx * TILEX, yy * TILEY, TILEX, TILEY,
131 SX + x * TILEX, SY + y * TILEY);
136 for (x = 0; x < MAX_BUF_XSIZE; x++)
137 for (y = 0; y < MAX_BUF_YSIZE; y++)
138 redraw[x][y] = FALSE;
141 scrolling_last = scrolling;
145 /* blit all (up to four) parts of the scroll buffer to the window */
146 BlitScreenToBitmap_EM(window);
151 static void DrawLevelField_EM(int x, int y, int sx, int sy,
154 int tile = Draw[y][x];
155 struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
156 int src_x = g->src_x + g->src_offset_x;
157 int src_y = g->src_y + g->src_offset_y;
158 int dst_x = sx * TILEX + g->dst_offset_x;
159 int dst_y = sy * TILEY + g->dst_offset_y;
160 int width = g->width;
161 int height = g->height;
165 if (width > 0 && height > 0)
167 SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
168 dst_x - src_x, dst_y - src_y);
169 BlitBitmapMasked(g->bitmap, screenBitmap,
170 src_x, src_y, width, height, dst_x, dst_y);
175 if ((width != TILEX || height != TILEY) && !g->preserve_background)
176 ClearRectangle(screenBitmap, sx * TILEX, sy * TILEY, TILEX, TILEY);
178 if (width > 0 && height > 0)
179 BlitBitmap(g->bitmap, screenBitmap,
180 src_x, src_y, width, height, dst_x, dst_y);
184 static void DrawLevelFieldCrumbled_EM(int x, int y, int sx, int sy,
185 int crm, boolean draw_masked)
187 int tile = Draw[y][x];
188 struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
191 if (crm == 0) /* no crumbled edges for this tile */
194 for (i = 0; i < 4; i++)
198 int width, height, cx, cy;
200 if (i == 1 || i == 2)
202 width = g->crumbled_border_size;
204 cx = (i == 2 ? TILEX - g->crumbled_border_size : 0);
210 height = g->crumbled_border_size;
212 cy = (i == 3 ? TILEY - g->crumbled_border_size : 0);
215 if (width > 0 && height > 0)
217 int src_x = g->crumbled_src_x + cx;
218 int src_y = g->crumbled_src_y + cy;
219 int dst_x = sx * TILEX + cx;
220 int dst_y = sy * TILEY + cy;
224 SetClipOrigin(g->crumbled_bitmap, g->crumbled_bitmap->stored_clip_gc,
225 dst_x - src_x, dst_y - src_y);
226 BlitBitmapMasked(g->crumbled_bitmap, screenBitmap,
227 src_x, src_y, width, height, dst_x, dst_y);
230 BlitBitmap(g->crumbled_bitmap, screenBitmap,
231 src_x, src_y, width, height, dst_x, dst_y);
237 static void DrawLevelPlayer_EM(int x1, int y1, int player_nr, int anim,
240 struct GraphicInfo_EM *g = &graphic_info_em_player[player_nr][anim][frame];
242 int src_x = g->src_x, src_y = g->src_y;
247 /* draw the player to current location */
250 SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
251 dst_x - src_x, dst_y - src_y);
252 BlitBitmapMasked(g->bitmap, screenBitmap,
253 src_x, src_y, TILEX, TILEY, dst_x, dst_y);
255 /* draw the player to opposite wrap-around column */
256 dst_x = x1 - MAX_BUF_XSIZE * TILEX;
258 SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
259 dst_x - src_x, dst_y - src_y);
260 BlitBitmapMasked(g->bitmap, screenBitmap,
261 g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
263 /* draw the player to opposite wrap-around row */
265 dst_y = y1 - MAX_BUF_YSIZE * TILEY;
266 SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
267 dst_x - src_x, dst_y - src_y);
268 BlitBitmapMasked(g->bitmap, screenBitmap,
269 g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
273 /* draw the player to current location */
276 BlitBitmap(g->bitmap, screenBitmap,
277 g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
279 /* draw the player to opposite wrap-around column */
280 dst_x = x1 - MAX_BUF_XSIZE * TILEX;
282 BlitBitmap(g->bitmap, screenBitmap,
283 g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
285 /* draw the player to opposite wrap-around row */
287 dst_y = y1 - MAX_BUF_YSIZE * TILEY;
288 BlitBitmap(g->bitmap, screenBitmap,
289 g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
293 /* draw differences between game tiles and screen tiles
295 * implicitly handles scrolling and restoring background under the sprites
297 * perhaps use mit-shm to speed this up
300 static void animscreen(void)
303 int left = screen_x / TILEX;
304 int top = screen_y / TILEY;
305 static int xy[4][2] =
313 for (y = top; y < top + MAX_BUF_YSIZE; y++)
315 for (x = left; x < left + MAX_BUF_XSIZE; x++)
317 int sx = x % MAX_BUF_XSIZE;
318 int sy = y % MAX_BUF_YSIZE;
319 int tile = Draw[y][x];
320 struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
321 int obj = g->unique_identifier;
324 /* re-calculate crumbled state of this tile */
325 if (g->has_crumbled_graphics)
327 for (i = 0; i < 4; i++)
329 int xx = x + xy[i][0];
330 int yy = y + xy[i][1];
333 if (xx < 0 || xx >= EM_MAX_CAVE_WIDTH ||
334 yy < 0 || yy >= EM_MAX_CAVE_HEIGHT)
337 tile_next = Draw[yy][xx];
339 if (!graphic_info_em_object[tile_next][frame].has_crumbled_graphics)
344 /* only redraw screen tiles if they (or their crumbled state) changed */
345 if (screentiles[sy][sx] != obj || crumbled_state[sy][sx] != crm)
347 DrawLevelField_EM(x, y, sx, sy, FALSE);
348 DrawLevelFieldCrumbled_EM(x, y, sx, sy, crm, FALSE);
350 screentiles[sy][sx] = obj;
351 crumbled_state[sy][sx] = crm;
353 redraw[sx][sy] = TRUE;
361 /* blit players to the screen
363 * handles transparency and movement
366 static void blitplayer(struct PLAYER *ply)
373 /* x1/y1 are left/top and x2/y2 are right/down part of the player movement */
374 x1 = (frame * ply->oldx + (8 - frame) * ply->x) * TILEX / 8;
375 y1 = (frame * ply->oldy + (8 - frame) * ply->y) * TILEY / 8;
379 if ((int)(x2 - screen_x) < ((MAX_BUF_XSIZE - 1) * TILEX - 1) &&
380 (int)(y2 - screen_y) < ((MAX_BUF_YSIZE - 1) * TILEY - 1))
382 /* some casts to "int" are needed because of negative calculation values */
383 int dx = (int)ply->x - (int)ply->oldx;
384 int dy = (int)ply->y - (int)ply->oldy;
385 int old_x = (int)ply->oldx + (7 - (int)frame) * dx / 8;
386 int old_y = (int)ply->oldy + (7 - (int)frame) * dy / 8;
387 int new_x = old_x + SIGN(dx);
388 int new_y = old_y + SIGN(dy);
389 int old_sx = old_x % MAX_BUF_XSIZE;
390 int old_sy = old_y % MAX_BUF_XSIZE;
391 int new_sx = new_x % MAX_BUF_XSIZE;
392 int new_sy = new_y % MAX_BUF_XSIZE;
394 int old_crm = crumbled_state[old_sy][old_sx];
396 int new_crm = crumbled_state[new_sy][new_sx];
398 /* only diggable elements can be crumbled in the classic EM engine */
399 boolean player_is_digging = (new_crm != 0);
401 x1 %= MAX_BUF_XSIZE * TILEX;
402 y1 %= MAX_BUF_YSIZE * TILEY;
403 x2 %= MAX_BUF_XSIZE * TILEX;
404 y2 %= MAX_BUF_YSIZE * TILEY;
406 if (player_is_digging)
409 /* draw the field the player is moving from (under the player) */
410 DrawLevelField_EM(old_x, old_y, old_sx, old_sy, FALSE);
411 DrawLevelFieldCrumbled_EM(old_x, old_y, old_sx, old_sy, old_crm, FALSE);
414 /* draw the field the player is moving to (under the player) */
415 DrawLevelField_EM(new_x, new_y, new_sx, new_sy, FALSE);
416 DrawLevelFieldCrumbled_EM(new_x, new_y, new_sx, new_sy, new_crm, FALSE);
418 /* draw the player (masked) over the element he is just digging away */
419 DrawLevelPlayer_EM(x1, y1, ply->num, ply->anim, TRUE);
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);
428 /* draw the player under the element which is on the same field */
429 DrawLevelPlayer_EM(x1, y1, ply->num, ply->anim, FALSE);
431 /* draw the field the player is moving from (masked over the player) */
432 DrawLevelField_EM(old_x, old_y, old_sx, old_sy, TRUE);
434 /* draw the field the player is moving to (masked over the player) */
435 DrawLevelField_EM(new_x, new_y, new_sx, new_sy, TRUE);
438 /* mark screen tiles as dirty */
439 screentiles[old_sy][old_sx] = -1;
440 screentiles[new_sy][new_sx] = -1;
444 void game_initscreen(void)
447 int dynamite_state = ply[0].dynamite; /* !!! ONLY PLAYER 1 !!! */
448 int all_keys_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
449 int player_nr = 0; /* !!! FIX THIS (CENTERED TO PLAYER 1) !!! */
453 screen_x = VALID_SCREEN_X(PLAYER_SCREEN_X(player_nr));
454 screen_y = VALID_SCREEN_Y(PLAYER_SCREEN_Y(player_nr));
460 for (y = 0; y < MAX_BUF_YSIZE; y++)
462 for (x = 0; x < MAX_BUF_XSIZE; x++)
464 screentiles[y][x] = -1;
465 crumbled_state[y][x] = 0;
470 DrawAllGameValues(lev.required, dynamite_state, lev.score,
471 lev.time, all_keys_state);
473 DrawAllGameValues(lev.required, ply1.dynamite, lev.score,
474 DISPLAY_TIME(lev.time + 4), ply1.keys | ply2.keys);
478 void RedrawPlayfield_EM()
480 int player_nr = 0; /* !!! FIX THIS (CENTERED TO PLAYER 1) !!! */
481 int sx = PLAYER_SCREEN_X(player_nr);
482 int sy = PLAYER_SCREEN_Y(player_nr);
487 int offset = (setup.scroll_delay ? 3 : 0) * TILEX;
489 /* calculate new screen scrolling position, with regard to scroll delay */
490 screen_x = VALID_SCREEN_X(sx + offset < screen_x ? sx + offset :
491 sx - offset > screen_x ? sx - offset : screen_x);
492 screen_y = VALID_SCREEN_Y(sy + offset < screen_y ? sy + offset :
493 sy - offset > screen_y ? sy - offset : screen_y);
497 if (sx > lev.width * TILEX)
498 sx = lev.width * TILEX;
499 if (sy > lev.height * TILEY)
500 sy = lev.height * TILEY;
502 if (sx < SCR_FIELDX * TILEX)
503 sx = SCR_FIELDX * TILEY;
504 if (sy < SCR_FIELDY * TILEY)
505 sy = SCR_FIELDY * TILEY;
507 screen_x = sx - (SCR_FIELDX - 1) * TILEX;
508 screen_y = sy - (SCR_FIELDY - 1) * TILEY;
514 for (i = 0; i < MAX_PLAYERS; i++)
522 void game_animscreen(void)
524 RedrawPlayfield_EM();
527 void DrawGameDoorValues_EM()
529 int dynamite_state = ply[0].dynamite; /* !!! ONLY PLAYER 1 !!! */
530 int all_keys_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
533 DrawAllGameValues(lev.required, dynamite_state, lev.score,
534 lev.time, all_keys_state);
536 DrawAllGameValues(lev.required, ply1.dynamite, lev.score,
537 DISPLAY_TIME(lev.time), ply1.keys | ply2.keys);