Prev: E32C Up: Map Next: E3D6
E353: Flip Sprite
Used by the routine at DrawSprite.
Turns a sprite upside down or round (or back again) when the flags of an object say that it should be the other way up or round from how it was last time. The sprite is changed in place, with bits 7 (upside down) and 6 (reversed) of its first byte showing how it is.
A sprite is made of a byte for the width in characters (bits 0 to 3) and the flags, a byte for the number of rows, and then the rows, with the bottom row first. Each row has a pair of bytes for each character: the mask and then the pixels.
Input
IX Object
Output
DE Address of the sprite
FlipSprite E353 LD A,(IX+$00) Fetch the type of the object.
E356 LD BC,$6E9E Find the address of the sprite in the table of sprites.
E359 CALL GetTableEntry
E35C EX DE,HL Move the address of the sprite into DE and keep it.
E35D PUSH DE
E35E LD A,(DE) Jump to FlipSprite_Reverse if the sprite is already the right way up.
E35F XOR (IX+$07)
E362 AND $80
E364 JR Z,FlipSprite_Reverse
Turn the sprite upside down, by swapping the rows.
E366 LD A,(DE) Flip the flag for being upside down.
E367 XOR $80
E369 LD (DE),A
E36A RLCA Work out the number of bytes in a row (two for each character).
E36B AND $1E
E36D LD B,A
E36E INC DE Fetch the number of rows of the sprite.
E36F LD A,(DE)
E370 LD C,A
E371 INC DE Point at the first row, and keep the address.
E372 PUSH DE
E373 LD E,B Set the number of bytes in a row.
E374 LD D,$00
E376 CALL MultiplyAByDE Work out the number of bytes in the sprite.
E379 POP DE Work out the address of the end of the sprite, and move it into DE.
E37A ADD HL,DE
E37B EX DE,HL
E37C LD A,B Point HL at the start of the second row.
E37D CALL AddAToHL
E380 DEC DE Point HL at the end of the first row, and DE at the end of the last row.
E381 DEC HL
E382 SRL C Work out the number of pairs of rows to swap.
FlipSprite_Rows E384 PUSH BC Keep the number of bytes in a row and the number of rows.
FlipSprite_Bytes E385 LD A,(DE) Swap the bytes of the two rows, from the end backwards, until the whole of both rows has been swapped.
E386 LD C,(HL)
E387 LD (HL),A
E388 LD A,C
E389 LD (DE),A
E38A DEC HL
E38B DEC DE
E38C DJNZ FlipSprite_Bytes
E38E POP BC Get the number of bytes in a row and the number of rows back.
E38F LD A,B Move HL on to the end of the next row down.
E390 CALL AddAToHL
E393 LD A,B
E394 CALL AddAToHL
E397 DEC C Loop back until all of the pairs of rows have been swapped.
E398 JR NZ,FlipSprite_Rows
Turn the sprite round, by reversing the rows.
FlipSprite_Reverse E39A POP DE Fetch the address of the sprite.
E39B PUSH DE
E39C LD A,(DE) Jump to FlipSprite_Done if the sprite is already the right way round.
E39D XOR (IX+$07)
E3A0 AND $40
E3A2 JR Z,FlipSprite_Done
This entry point is used by the routine at MirrorSprite.
FlipSprite_Mirror E3A4 LD A,(DE) Flip the flag for being reversed.
E3A5 XOR $40
E3A7 LD (DE),A
E3A8 AND $0F Fetch the width of the sprite in characters.
E3AA LD B,A
E3AB LD C,A
E3AC INC DE Fetch the number of rows of the sprite, and keep it.
E3AD LD A,(DE)
E3AE EX AF,AF'
E3AF INC DE Start with the first row.
E3B0 EX DE,HL
E3B1 PUSH HL Point HL' at the first row, and B' at the table for reversing bits.
E3B2 EXX
E3B3 POP HL
E3B4 LD B,$F9
E3B6 EXX
FlipSprite_Row E3B7 EXX Reverse the bits of the mask of a character, using the table.
E3B8 LD C,(HL)
E3B9 LD A,(BC)
E3BA LD E,A
E3BB INC HL Reverse the bits of the pixels of the character.
E3BC LD C,(HL)
E3BD LD A,(BC)
E3BE LD D,A
E3BF INC HL Keep them on the stack, and loop back until the row has been done.
E3C0 PUSH DE
E3C1 EXX
E3C2 DJNZ FlipSprite_Row
E3C4 LD B,C Count the characters again.
FlipSprite_Store E3C5 POP DE Put the characters back in the row, in the reverse order.
E3C6 LD (HL),E
E3C7 INC HL
E3C8 LD (HL),D
E3C9 INC HL
E3CA DJNZ FlipSprite_Store
E3CC EX AF,AF' Jump to FlipSprite_Done when all the rows have been done.
E3CD DEC A
E3CE JR Z,FlipSprite_Done
E3D0 EX AF,AF' Count the characters again, and loop back to do the next row.
E3D1 LD B,C
E3D2 JR FlipSprite_Row
FlipSprite_Done E3D4 POP DE Get the address of the sprite back, and return.
E3D5 RET
View the equivalent code in;
Prev: E32C Up: Map Next: E3D6