1 // ============================================================================
2 // Mirror Magic -- McDuffin's Revenge
3 // ----------------------------------------------------------------------------
4 // (c) 1994-2017 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
18 void SetDrawtoField_MM(int mode)
20 int full_xsize = lev_fieldx * TILESIZE_VAR;
21 int full_ysize = lev_fieldy * TILESIZE_VAR;
23 // distance (delta) from screen border (SX/SY) to centered level playfield
24 dSX = (full_xsize < SXSIZE ? (SXSIZE - full_xsize) / 2 : 0);
25 dSY = (full_ysize < SYSIZE ? (SYSIZE - full_ysize) / 2 : 0);
27 // for convenience, absolute screen position to centered level playfield
30 cSX2 = SX + dSX + 2; // including playfield border
31 cSY2 = SY + dSY + 2; // including playfield border
33 if (mode == DRAW_TO_BACKBUFFER)
39 SetTileCursorSXSY(cSX, cSY);
42 void ClearWindow(void)
44 ClearRectangle(backbuffer, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
46 SetDrawtoField(DRAW_TO_BACKBUFFER);
47 SetDrawtoField_MM(DRAW_TO_BACKBUFFER);
49 redraw_mask |= REDRAW_FIELD;
52 void DrawGraphicAnimation_MM(int x, int y, int graphic, int frame)
57 getGraphicSource(graphic, frame, &bitmap, &src_x, &src_y);
59 BlitBitmap(bitmap, drawto_field, src_x, src_y, TILEX, TILEY,
60 cFX + x * TILEX, cFY + y * TILEY);
63 void DrawGraphic_MM(int x, int y, int graphic)
66 if (!IN_SCR_FIELD(x,y))
68 printf("DrawGraphic_MM(): x = %d, y = %d, graphic = %d\n",x,y,graphic);
69 printf("DrawGraphic_MM(): This should never happen!\n");
74 DrawGraphicExt_MM(drawto_field, cFX + x * TILEX, cFY + y * TILEY, graphic);
79 void DrawGraphicExt_MM(DrawBuffer *d, int x, int y, int graphic)
84 getGraphicSource(graphic, 0, &bitmap, &src_x, &src_y);
86 BlitBitmap(bitmap, d, src_x, src_y, TILEX, TILEY, x, y);
89 void DrawGraphicThruMask_MM(int x, int y, int graphic)
92 if (!IN_SCR_FIELD(x,y))
94 printf("DrawGraphicThruMask_MM(): x = %d,y = %d, graphic = %d\n",x,y,graphic);
95 printf("DrawGraphicThruMask_MM(): This should never happen!\n");
100 DrawGraphicThruMaskExt_MM(drawto_field, cFX + x * TILEX, cFY + y * TILEY,
106 void DrawGraphicThruMaskExt_MM(DrawBuffer *d, int dest_x, int dest_y,
112 if (graphic == IMG_EMPTY)
115 getGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
117 BlitBitmapMasked(src_bitmap, d, src_x, src_y, TILEX, TILEY, dest_x, dest_y);
120 void DrawMiniGraphic_MM(int x, int y, int graphic)
122 DrawMiniGraphicExt_MM(drawto, cSX + x * MINI_TILEX, cSY + y * MINI_TILEY,
125 MarkTileDirty(x / 2, y / 2);
129 static void getMicroGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
131 getSizedGraphicSource(graphic, 0, TILESIZE / 4, bitmap, x, y);
135 void DrawMiniGraphicExt_MM(DrawBuffer *d, int x, int y, int graphic)
140 getMiniGraphicSource(graphic, &bitmap, &src_x, &src_y);
142 BlitBitmap(bitmap, d, src_x, src_y, MINI_TILEX, MINI_TILEY, x, y);
145 void DrawGraphicShifted_MM(int x,int y, int dx,int dy, int graphic,
146 int cut_mode, int mask_mode)
148 int width = TILEX, height = TILEY;
150 int src_x, src_y, dest_x, dest_y;
155 DrawGraphic_MM(x, y, graphic);
160 if (dx || dy) // Verschiebung der Grafik?
162 if (x < BX1) // Element kommt von links ins Bild
169 else if (x > BX2) // Element kommt von rechts ins Bild
175 else if (x==BX1 && dx < 0) // Element verläßt links das Bild
181 else if (x==BX2 && dx > 0) // Element verläßt rechts das Bild
183 else if (dx) // allg. Bewegung in x-Richtung
184 MarkTileDirty(x + SIGN(dx), y);
186 if (y < BY1) // Element kommt von oben ins Bild
188 if (cut_mode==CUT_BELOW) // Element oberhalb des Bildes
196 else if (y > BY2) // Element kommt von unten ins Bild
202 else if (y==BY1 && dy < 0) // Element verläßt oben das Bild
208 else if (dy > 0 && cut_mode == CUT_ABOVE)
210 if (y == BY2) // Element unterhalb des Bildes
216 MarkTileDirty(x, y + 1);
217 } // Element verläßt unten das Bild
218 else if (dy > 0 && (y == BY2 || cut_mode == CUT_BELOW))
222 else if (dy) // allg. Bewegung in y-Richtung
224 MarkTileDirty(x, y + SIGN(dy));
228 getGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
233 dest_x = cFX + x * TILEX + dx;
234 dest_y = cFY + y * TILEY + dy;
237 if (!IN_SCR_FIELD(x,y))
239 printf("DrawGraphicShifted_MM(): x = %d, y = %d, graphic = %d\n",x,y,graphic);
240 printf("DrawGraphicShifted_MM(): This should never happen!\n");
245 if (mask_mode == USE_MASKING)
246 BlitBitmapMasked(src_bitmap, drawto_field,
247 src_x, src_y, TILEX, TILEY, dest_x, dest_y);
249 BlitBitmap(src_bitmap, drawto_field,
250 src_x, src_y, width, height, dest_x, dest_y);
255 void DrawGraphicShiftedThruMask_MM(int x,int y, int dx,int dy, int graphic,
258 DrawGraphicShifted_MM(x, y, dx, dy, graphic, cut_mode, USE_MASKING);
261 void DrawScreenElementExt_MM(int x, int y, int dx, int dy, int element,
262 int cut_mode, int mask_mode)
264 int ux = LEVELX(x), uy = LEVELY(y);
265 int graphic = el2gfx(element);
266 int phase8 = ABS(MovPos[ux][uy]) / (TILEX / 8);
267 int phase2 = phase8 / 4;
268 int dir = MovDir[ux][uy];
270 if (element == EL_PACMAN)
272 graphic = (phase2 ? IMG_MM_PACMAN_RIGHT : IMG_MM_PACMAN_EATING_RIGHT);
276 else if (dir == MV_LEFT)
278 else if (dir == MV_DOWN)
283 DrawGraphicShifted_MM(x, y, dx, dy, graphic, cut_mode, mask_mode);
284 else if (mask_mode == USE_MASKING)
285 DrawGraphicThruMask_MM(x, y, graphic);
287 DrawGraphic_MM(x, y, graphic);
290 void DrawLevelElementExt_MM(int x, int y, int dx, int dy, int element,
291 int cut_mode, int mask_mode)
293 if (IN_LEV_FIELD(x, y) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
294 DrawScreenElementExt_MM(SCREENX(x), SCREENY(y), dx, dy, element,
295 cut_mode, mask_mode);
298 void DrawScreenElementShifted_MM(int x, int y, int dx, int dy, int element,
301 DrawScreenElementExt_MM(x, y, dx, dy, element, cut_mode, NO_MASKING);
304 void DrawLevelElementShifted_MM(int x, int y, int dx, int dy, int element,
307 DrawLevelElementExt_MM(x, y, dx, dy, element, cut_mode, NO_MASKING);
310 void DrawScreenElementThruMask_MM(int x, int y, int element)
312 DrawScreenElementExt_MM(x, y, 0, 0, element, NO_CUTTING, USE_MASKING);
315 void DrawLevelElementThruMask_MM(int x, int y, int element)
317 DrawLevelElementExt_MM(x, y, 0, 0, element, NO_CUTTING, USE_MASKING);
320 void DrawLevelFieldThruMask_MM(int x, int y)
322 DrawLevelElementExt_MM(x, y, 0, 0, Feld[x][y], NO_CUTTING, USE_MASKING);
325 void DrawScreenElement_MM(int x, int y, int element)
327 DrawScreenElementExt_MM(x, y, 0, 0, element, NO_CUTTING, NO_MASKING);
330 void DrawLevelElement_MM(int x, int y, int element)
332 if (IN_LEV_FIELD(x, y) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
333 DrawScreenElement_MM(SCREENX(x), SCREENY(y), element);
336 void DrawScreenField_MM(int x, int y)
338 int element = Feld[x][y];
340 if (!IN_LEV_FIELD(x, y))
345 int horiz_move = (MovDir[x][y] == MV_LEFT || MovDir[x][y] == MV_RIGHT);
347 DrawScreenElement_MM(x, y, EL_EMPTY);
350 DrawScreenElementShifted_MM(x, y, MovPos[x][y], 0, element, NO_CUTTING);
352 DrawScreenElementShifted_MM(x, y, 0, MovPos[x][y], element, NO_CUTTING);
354 else if (IS_BLOCKED(x, y))
360 Blocked2Moving(x, y, &oldx, &oldy);
364 horiz_move = (MovDir[oldx][oldy] == MV_LEFT ||
365 MovDir[oldx][oldy] == MV_RIGHT);
367 DrawScreenElement_MM(x, y, EL_EMPTY);
369 element = Feld[oldx][oldy];
372 DrawScreenElementShifted_MM(sx, sy, MovPos[oldx][oldy], 0, element,
375 DrawScreenElementShifted_MM(sx, sy, 0, MovPos[oldx][oldy], element,
378 else if (IS_DRAWABLE(element))
380 DrawScreenElement_MM(x, y, element);
384 DrawScreenElement_MM(x, y, EL_EMPTY);
388 void DrawLevelField_MM(int x, int y)
390 DrawScreenField_MM(x, y);
393 void DrawMiniElement_MM(int x, int y, int element)
399 DrawMiniGraphic_MM(x, y, IMG_EMPTY);
404 graphic = el2gfx(element);
406 DrawMiniGraphic_MM(x, y, graphic);
409 void DrawMiniElementOrWall_MM(int sx, int sy, int scroll_x, int scroll_y)
411 int x = sx + scroll_x, y = sy + scroll_y;
413 if (x < -1 || x > lev_fieldx || y < -1 || y > lev_fieldy)
414 DrawMiniElement_MM(sx, sy, EL_EMPTY);
415 else if (x > -1 && x < lev_fieldx && y > -1 && y < lev_fieldy)
416 DrawMiniElement_MM(sx, sy, Feld[x][y]);
419 void DrawField_MM(int x, int y)
421 int element = Feld[x][y];
423 DrawElement_MM(x, y, element);
426 void DrawLevel_MM(void)
432 for (x = 0; x < lev_fieldx; x++)
433 for (y = 0; y < lev_fieldy; y++)
436 redraw_mask |= REDRAW_FIELD;
439 void DrawWallsExt_MM(int x, int y, int element, int draw_mask)
442 int graphic = el2gfx(WALL_BASE(element));
446 getMiniGraphicSource(graphic, &bitmap, &gx, &gy);
448 DrawGraphic_MM(x, y, IMG_EMPTY);
451 if (IS_WALL_WOOD(element) || IS_WALL_AMOEBA(element) ||
452 IS_DF_WALL_WOOD(element))
454 if (IS_WALL_ICE(element) || IS_WALL_AMOEBA(element))
458 for (i = 0; i < 4; i++)
460 int dest_x = cSX + x * TILEX + MINI_TILEX * (i % 2);
461 int dest_y = cSY + y * TILEY + MINI_TILEY * (i / 2);
463 if (!((1 << i) & draw_mask))
466 if (element & (1 << i))
467 BlitBitmap(bitmap, drawto, gx, gy, MINI_TILEX, MINI_TILEY,
470 ClearRectangle(drawto, dest_x, dest_y, MINI_TILEX, MINI_TILEY);
476 void DrawWalls_MM(int x, int y, int element)
478 DrawWallsExt_MM(x, y, element, HIT_MASK_ALL);
481 void DrawWallsAnimation_MM(int x, int y, int element, int phase, int bit_mask)
487 DrawWalls_MM(x, y, element);
492 for (i = 0; i < 4; i++)
494 if (element & (1 << i))
500 int dst_x = cSX + x * TILEX + (i % 2) * MINI_TILEX;
501 int dst_y = cSY + y * TILEY + (i / 2) * MINI_TILEY;
503 if (bit_mask & (1 << i))
505 graphic = (IS_WALL_AMOEBA(element) ?
506 IMG_MM_AMOEBA_WALL_GROWING :
507 IMG_MM_ICE_WALL_SHRINKING);
512 graphic = (IS_WALL_AMOEBA(element) ?
518 getSizedGraphicSource(graphic, frame, MINI_TILESIZE, &bitmap,
521 BlitBitmap(bitmap, drawto, src_x, src_y, MINI_TILEX, MINI_TILEY,
529 void DrawElement_MM(int x, int y, int element)
531 if (element == EL_EMPTY)
532 DrawGraphic_MM(x, y, IMG_EMPTY);
533 else if (IS_WALL(element))
534 DrawWalls_MM(x, y, element);
536 else if (IS_WALL_CHANGING(element) && IS_WALL_CHANGING(Feld[x][y]))
538 int wall_element = Feld[x][y] - EL_WALL_CHANGING + Store[x][y];
540 DrawWalls_MM(x, y, wall_element);
543 else if (element == EL_PACMAN)
544 DrawLevelField_MM(x, y);
545 else if (element == EL_FUSE_ON &&
549 DrawGraphic_MM(x, y, IMG_MM_FUSE);
551 DrawGraphic_MM(x, y, el2gfx(element));
555 static void DrawMicroWalls_MM(int x, int y, int element)
558 int graphic = el2gfx(WALL_BASE(element));
562 getMicroGraphicSource(graphic, &bitmap, &gx, &gy);
564 for (i = 0; i < 4; i++)
566 int xpos = MICROLEV_XPOS + x * MICRO_TILEX + MICRO_WALLX * (i % 2);
567 int ypos = MICROLEV_YPOS + y * MICRO_TILEY + MICRO_WALLY * (i / 2);
569 if (element & (1 << i))
570 BlitBitmap(bitmap, drawto, gx, gy, MICRO_WALLX, MICRO_WALLY, xpos, ypos);
572 ClearRectangle(drawto, xpos, ypos, MICRO_WALLX, MICRO_WALLY);
576 static void DrawMicroElement_MM(int x, int y, int element)
579 int graphic = el2gfx(element);
582 if (element == EL_EMPTY)
585 if (IS_WALL(element))
587 DrawMicroWalls_MM(x, y, element);
592 getMicroGraphicSource(graphic, &bitmap, &gx, &gy);
594 BlitBitmap(bitmap, drawto, gx, gy, MICRO_TILEX, MICRO_TILEY,
595 MICROLEV_XPOS + x * MICRO_TILEX, MICROLEV_YPOS + y * MICRO_TILEY);
598 static void DrawMicroLevelExt_MM(int xpos, int ypos)
602 ClearRectangle(drawto, xpos, ypos, MICROLEV_XSIZE, MICROLEV_YSIZE);
604 for (x = 0; x < STD_LEV_FIELDX; x++)
605 for (y = 0; y < STD_LEV_FIELDY; y++)
606 DrawMicroElement_MM(x, y, Ur[x][y]);
608 redraw_mask |= REDRAW_FIELD;
612 void DrawMiniLevel_MM(int size_x, int size_y, int scroll_x, int scroll_y)
616 for (x = 0; x < size_x; x++)
617 for (y = 0; y < size_y; y++)
618 DrawMiniElementOrWall_MM(x, y, scroll_x, scroll_y);
620 redraw_mask |= REDRAW_FIELD;
624 static int REQ_in_range(int x, int y)
626 if (y > DY + 249 && y < DY + 278)
628 if (x > DX + 1 && x < DX + 48)
630 else if (x > DX + 51 && x < DX + 98)
638 Pixel ReadPixel(DrawBuffer *bitmap, int x, int y)
640 return GetPixel(bitmap, x, y);
643 void SetRGB(unsigned int pixel,
644 unsigned short red, unsigned short green, unsigned short blue)
648 int get_base_element(int element)
650 if (IS_MIRROR(element))
651 return EL_MIRROR_START;
652 else if (IS_MIRROR_FIXED(element))
653 return EL_MIRROR_FIXED_START;
654 else if (IS_POLAR(element))
655 return EL_POLAR_START;
656 else if (IS_POLAR_CROSS(element))
657 return EL_POLAR_CROSS_START;
658 else if (IS_BEAMER(element))
659 return EL_BEAMER_RED_START + BEAMER_NR(element) * 16;
660 else if (IS_FIBRE_OPTIC(element))
661 return EL_FIBRE_OPTIC_START + FIBRE_OPTIC_NR(element) * 2;
662 else if (IS_MCDUFFIN(element))
663 return EL_MCDUFFIN_START;
664 else if (IS_LASER(element))
665 return EL_LASER_START;
666 else if (IS_RECEIVER(element))
667 return EL_RECEIVER_START;
668 else if (IS_DF_MIRROR(element))
669 return EL_DF_MIRROR_START;
670 else if (IS_DF_MIRROR_AUTO(element))
671 return EL_DF_MIRROR_AUTO_START;
672 else if (IS_PACMAN(element))
673 return EL_PACMAN_START;
674 else if (IS_GRID_STEEL(element))
675 return EL_GRID_STEEL_START;
676 else if (IS_GRID_WOOD(element))
677 return EL_GRID_WOOD_START;
678 else if (IS_GRID_STEEL_FIXED(element))
679 return EL_GRID_STEEL_FIXED_START;
680 else if (IS_GRID_WOOD_FIXED(element))
681 return EL_GRID_WOOD_FIXED_START;
682 else if (IS_GRID_STEEL_AUTO(element))
683 return EL_GRID_STEEL_AUTO_START;
684 else if (IS_GRID_WOOD_AUTO(element))
685 return EL_GRID_WOOD_AUTO_START;
686 else if (IS_WALL_STEEL(element))
687 return EL_WALL_STEEL_START;
688 else if (IS_WALL_WOOD(element))
689 return EL_WALL_WOOD_START;
690 else if (IS_WALL_ICE(element))
691 return EL_WALL_ICE_START;
692 else if (IS_WALL_AMOEBA(element))
693 return EL_WALL_AMOEBA_START;
694 else if (IS_DF_WALL_STEEL(element))
695 return EL_DF_WALL_STEEL_START;
696 else if (IS_DF_WALL_WOOD(element))
697 return EL_DF_WALL_WOOD_START;
698 else if (IS_CHAR(element))
699 return EL_CHAR_START;
704 int get_element_phase(int element)
706 return element - get_base_element(element);
709 int get_num_elements(int element)
711 if (IS_MIRROR(element) ||
713 IS_BEAMER(element) ||
714 IS_DF_MIRROR(element) ||
715 IS_DF_MIRROR_AUTO(element))
717 else if (IS_GRID_STEEL_FIXED(element) ||
718 IS_GRID_WOOD_FIXED(element) ||
719 IS_GRID_STEEL_AUTO(element) ||
720 IS_GRID_WOOD_AUTO(element))
722 else if (IS_MIRROR_FIXED(element) ||
723 IS_POLAR_CROSS(element) ||
724 IS_MCDUFFIN(element) ||
726 IS_RECEIVER(element) ||
727 IS_PACMAN(element) ||
728 IS_GRID_STEEL(element) ||
729 IS_GRID_WOOD(element))
735 int get_rotated_element(int element, int step)
737 int base_element = get_base_element(element);
738 int num_elements = get_num_elements(element);
739 int element_phase = element - base_element;
741 return base_element + (element_phase + step + num_elements) % num_elements;
744 static int map_element(int element)
748 case EL_WALL_STEEL: return EL_STEEL_WALL;
749 case EL_WALL_WOOD: return EL_WOODEN_WALL;
750 case EL_WALL_ICE: return EL_ICE_WALL;
751 case EL_WALL_AMOEBA: return EL_AMOEBA_WALL;
752 case EL_DF_WALL_STEEL: return EL_DF_STEEL_WALL;
753 case EL_DF_WALL_WOOD: return EL_DF_WOODEN_WALL;
755 default: return element;
759 int el2gfx(int element)
761 element = map_element(element);
766 return IMG_MM_LIGHTBALL_RED + RND(3);
769 return el2img_mm(element);
773 void RedrawPlayfield_MM(void)
779 void BlitScreenToBitmap_MM(Bitmap *target_bitmap)
781 BlitBitmap(drawto_field, target_bitmap,
782 REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE, REAL_SX, REAL_SY);