Prev: D157 Up: Map Next: D1EB
D174: 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 D174 PUSH DE Stash DE on the stack.
D175 LD A,(DE) Fetch the width byte of the sprite.
D176 XOR (IX+$07) Merge the flags of the object.
D179 AND %10000000 Keep bit 7.
D17B JR Z,FlipSprite_Horizontal Jump to FlipSprite_Horizontal if it is zero.
D17D LD A,(DE) Fetch the width byte again.
D17E XOR %10000000 Toggle bit 7.
D180 LD (DE),A Write the width byte.
D181 RLCA Double it.
D182 AND %01111110 Keep bits 1 to 6.
D184 LD B,A Copy it to B.
D185 INC DE Increment DE.
D186 LD A,(DE) Fetch the height.
D187 LD C,A Copy it to C.
D188 INC DE Increment DE.
D189 PUSH DE Stash DE on the stack.
D18A LD E,B Set DE to the line length.
D18B LD D,$00
D18D CALL MultiplyAByDE Call MultiplyAByDE.
D190 POP DE Restore DE from the stack.
D191 ADD HL,DE Add DE to HL.
D192 EX DE,HL Exchange DE and HL.
D193 LD A,B Fetch B.
D194 CALL AddAToHL Call AddAToHL.
D197 DEC DE Decrement DE.
D198 DEC HL Decrement HL.
D199 SRL C Halve C.
FlipSprite_Lines D19B PUSH BC Stash BC on the stack.
FlipSprite_Bytes D19C LD A,(DE) Fetch a byte of the last line.
D19D LD C,(HL) Fetch a byte of the first line.
D19E LD (HL),A Write the last-line byte.
D19F LD A,C Fetch the first-line byte.
D1A0 LD (DE),A Write it to the last line.
D1A1 DEC HL Decrement HL.
D1A2 DEC DE Decrement DE.
D1A3 DJNZ FlipSprite_Bytes Jump to FlipSprite_Bytes until B is zero.
D1A5 POP BC Restore BC from the stack.
D1A6 LD A,B Fetch B.
D1A7 SLA A Double it.
D1A9 CALL AddAToHL Call AddAToHL.
D1AC DEC C Decrement C.
D1AD JR NZ,FlipSprite_Lines Jump to FlipSprite_Lines if it is not zero.
Flip the sprite from left to right if needed.
FlipSprite_Horizontal D1AF POP DE Restore DE from the stack and stash it again.
D1B0 PUSH DE
D1B1 LD A,(DE) Fetch the width byte of the sprite.
D1B2 XOR (IX+$07) Merge the flags of the object.
D1B5 AND %01000000 Keep bit 6.
D1B7 JR Z,FlipSprite_Finish Jump to FlipSprite_Finish if it is zero.
D1B9 LD A,(DE) Fetch the width byte again.
D1BA XOR %01000000 Toggle bit 6.
D1BC LD (DE),A Write the width byte.
D1BD AND %00001111 Keep bits 0 to 3.
D1BF LD B,A Copy it to B.
D1C0 LD C,A Copy it to C.
D1C1 INC DE Increment DE.
D1C2 LD A,(DE) Fetch the height.
D1C3 EX AF,AF' Copy it to A'.
D1C4 INC DE Increment DE.
D1C5 EX DE,HL Exchange DE and HL.
D1C6 PUSH HL Stash HL on the stack.
D1C7 EXX Switch to the shadow registers.
D1C8 POP HL Restore HL' from the stack.
D1C9 LD B,$F1 Set B' to F1.
D1CB EXX Switch back to the main registers.
FlipSprite_Line D1CC EXX Switch to the shadow registers.
D1CD LD C,(HL) Fetch the mask byte.
D1CE LD A,(BC) Fetch the reversed byte.
D1CF LD E,A Copy it to E.
D1D0 INC HL Increment HL.
D1D1 LD C,(HL) Fetch the data byte.
D1D2 LD A,(BC) Fetch the reversed byte.
D1D3 LD D,A Copy it to D.
D1D4 INC HL Increment HL.
D1D5 PUSH DE Stash DE on the stack.
D1D6 EXX Switch back to the main registers.
D1D7 DJNZ FlipSprite_Line Jump to FlipSprite_Line until B is zero.
D1D9 LD B,C Copy C to B.
FlipSprite_Column D1DA POP DE Restore DE from the stack.
D1DB LD (HL),E Write E.
D1DC INC HL Increment HL.
D1DD LD (HL),D Write D.
D1DE INC HL Increment HL.
D1DF DJNZ FlipSprite_Column Jump to FlipSprite_Column until B is zero.
D1E1 EX AF,AF' Fetch A'.
D1E2 DEC A Decrement A.
D1E3 JR Z,FlipSprite_Finish Jump to FlipSprite_Finish if it is zero.
D1E5 EX AF,AF' Copy it to A'.
D1E6 LD B,C Copy C to B.
D1E7 JR FlipSprite_Line Jump to FlipSprite_Line.
FlipSprite_Finish D1E9 POP DE Restore DE from the stack.
D1EA RET Return.
View the equivalent code in;
Prev: D157 Up: Map Next: D1EB