Prev: 53591 Up: Map Next: 53739
53620: Flip Sprite
Used by the routine at GetSpriteAddress.
Flips the data of a sprite in place, if its flags are not the same as the flags of the object that is going to be drawn. The width byte of a sprite has the vertical flip in bit 7 and the horizontal flip in bit 6. The sprite is flipped vertically by swapping its lines, and horizontally by reversing the bits of each byte and the order of the bytes of each line, with ReversedByteTable.
Input
DE The address of the sprite
IX The address of the object
Output
DE The address of the sprite
Flip the sprite from top to bottom if needed.
FlipSprite 53620 PUSH DE Stash DE on the stack.
53621 LD A,(DE) Fetch the width byte of the sprite.
53622 XOR (IX+7) Merge the flags of the object.
53625 AND %10000000 Keep bit 7.
53627 JR Z,FlipSprite_Horizontal Jump to FlipSprite_Horizontal if it is zero.
53629 LD A,(DE) Fetch the width byte again.
53630 XOR %10000000 Toggle bit 7.
53632 LD (DE),A Write the width byte.
53633 RLCA Double it.
53634 AND %01111110 Keep bits 1 to 6.
53636 LD B,A Copy it to B.
53637 INC DE Increment DE.
53638 LD A,(DE) Fetch the height.
53639 LD C,A Copy it to C.
53640 INC DE Increment DE.
53641 PUSH DE Stash DE on the stack.
53642 LD E,B Set DE to the line length.
53643 LD D,0
53645 CALL MultiplyAByDE Call MultiplyAByDE.
53648 POP DE Restore DE from the stack.
53649 ADD HL,DE Add DE to HL.
53650 EX DE,HL Exchange DE and HL.
53651 LD A,B Fetch B.
53652 CALL AddAToHL Call AddAToHL.
53655 DEC DE Decrement DE.
53656 DEC HL Decrement HL.
53657 SRL C Halve C.
FlipSprite_Lines 53659 PUSH BC Stash BC on the stack.
FlipSprite_Bytes 53660 LD A,(DE) Fetch a byte of the last line.
53661 LD C,(HL) Fetch a byte of the first line.
53662 LD (HL),A Write the last-line byte.
53663 LD A,C Fetch the first-line byte.
53664 LD (DE),A Write it to the last line.
53665 DEC HL Decrement HL.
53666 DEC DE Decrement DE.
53667 DJNZ FlipSprite_Bytes Jump to FlipSprite_Bytes until B is zero.
53669 POP BC Restore BC from the stack.
53670 LD A,B Fetch B.
53671 SLA A Double it.
53673 CALL AddAToHL Call AddAToHL.
53676 DEC C Decrement C.
53677 JR NZ,FlipSprite_Lines Jump to FlipSprite_Lines if it is not zero.
Flip the sprite from left to right if needed.
FlipSprite_Horizontal 53679 POP DE Restore DE from the stack and stash it again.
53680 PUSH DE
53681 LD A,(DE) Fetch the width byte of the sprite.
53682 XOR (IX+7) Merge the flags of the object.
53685 AND %01000000 Keep bit 6.
53687 JR Z,FlipSprite_Finish Jump to FlipSprite_Finish if it is zero.
53689 LD A,(DE) Fetch the width byte again.
53690 XOR %01000000 Toggle bit 6.
53692 LD (DE),A Write the width byte.
53693 AND %00001111 Keep bits 0 to 3.
53695 LD B,A Copy it to B.
53696 LD C,A Copy it to C.
53697 INC DE Increment DE.
53698 LD A,(DE) Fetch the height.
53699 EX AF,AF' Copy it to A'.
53700 INC DE Increment DE.
53701 EX DE,HL Exchange DE and HL.
53702 PUSH HL Stash HL on the stack.
53703 EXX Switch to the shadow registers.
53704 POP HL Restore HL' from the stack.
53705 LD B,241 Set B' to 241.
53707 EXX Switch back to the main registers.
FlipSprite_Line 53708 EXX Switch to the shadow registers.
53709 LD C,(HL) Fetch the mask byte.
53710 LD A,(BC) Fetch the reversed byte.
53711 LD E,A Copy it to E.
53712 INC HL Increment HL.
53713 LD C,(HL) Fetch the data byte.
53714 LD A,(BC) Fetch the reversed byte.
53715 LD D,A Copy it to D.
53716 INC HL Increment HL.
53717 PUSH DE Stash DE on the stack.
53718 EXX Switch back to the main registers.
53719 DJNZ FlipSprite_Line Jump to FlipSprite_Line until B is zero.
53721 LD B,C Copy C to B.
FlipSprite_Column 53722 POP DE Restore DE from the stack.
53723 LD (HL),E Write E.
53724 INC HL Increment HL.
53725 LD (HL),D Write D.
53726 INC HL Increment HL.
53727 DJNZ FlipSprite_Column Jump to FlipSprite_Column until B is zero.
53729 EX AF,AF' Fetch A'.
53730 DEC A Decrement A.
53731 JR Z,FlipSprite_Finish Jump to FlipSprite_Finish if it is zero.
53733 EX AF,AF' Copy it to A'.
53734 LD B,C Copy C to B.
53735 JR FlipSprite_Line Jump to FlipSprite_Line.
FlipSprite_Finish 53737 POP DE Restore DE from the stack.
53738 RET Return.
View the equivalent code in;
Prev: 53591 Up: Map Next: 53739