Prev: 58156 Up: Map Next: 58326
58195: 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 58195 LD A,(IX+0) Fetch the type of the object.
58198 LD BC,28318 Find the address of the sprite in the table of sprites.
58201 CALL GetTableEntry
58204 EX DE,HL Move the address of the sprite into DE and keep it.
58205 PUSH DE
58206 LD A,(DE) Jump to FlipSprite_Reverse if the sprite is already the right way up.
58207 XOR (IX+7)
58210 AND 128
58212 JR Z,FlipSprite_Reverse
Turn the sprite upside down, by swapping the rows.
58214 LD A,(DE) Flip the flag for being upside down.
58215 XOR 128
58217 LD (DE),A
58218 RLCA Work out the number of bytes in a row (two for each character).
58219 AND 30
58221 LD B,A
58222 INC DE Fetch the number of rows of the sprite.
58223 LD A,(DE)
58224 LD C,A
58225 INC DE Point at the first row, and keep the address.
58226 PUSH DE
58227 LD E,B Set the number of bytes in a row.
58228 LD D,0
58230 CALL MultiplyAByDE Work out the number of bytes in the sprite.
58233 POP DE Work out the address of the end of the sprite, and move it into DE.
58234 ADD HL,DE
58235 EX DE,HL
58236 LD A,B Point HL at the start of the second row.
58237 CALL AddAToHL
58240 DEC DE Point HL at the end of the first row, and DE at the end of the last row.
58241 DEC HL
58242 SRL C Work out the number of pairs of rows to swap.
FlipSprite_Rows 58244 PUSH BC Keep the number of bytes in a row and the number of rows.
FlipSprite_Bytes 58245 LD A,(DE) Swap the bytes of the two rows, from the end backwards, until the whole of both rows has been swapped.
58246 LD C,(HL)
58247 LD (HL),A
58248 LD A,C
58249 LD (DE),A
58250 DEC HL
58251 DEC DE
58252 DJNZ FlipSprite_Bytes
58254 POP BC Get the number of bytes in a row and the number of rows back.
58255 LD A,B Move HL on to the end of the next row down.
58256 CALL AddAToHL
58259 LD A,B
58260 CALL AddAToHL
58263 DEC C Loop back until all of the pairs of rows have been swapped.
58264 JR NZ,FlipSprite_Rows
Turn the sprite round, by reversing the rows.
FlipSprite_Reverse 58266 POP DE Fetch the address of the sprite.
58267 PUSH DE
58268 LD A,(DE) Jump to FlipSprite_Done if the sprite is already the right way round.
58269 XOR (IX+7)
58272 AND 64
58274 JR Z,FlipSprite_Done
This entry point is used by the routine at MirrorSprite.
FlipSprite_Mirror 58276 LD A,(DE) Flip the flag for being reversed.
58277 XOR 64
58279 LD (DE),A
58280 AND 15 Fetch the width of the sprite in characters.
58282 LD B,A
58283 LD C,A
58284 INC DE Fetch the number of rows of the sprite, and keep it.
58285 LD A,(DE)
58286 EX AF,AF'
58287 INC DE Start with the first row.
58288 EX DE,HL
58289 PUSH HL Point HL' at the first row, and B' at the table for reversing bits.
58290 EXX
58291 POP HL
58292 LD B,249
58294 EXX
FlipSprite_Row 58295 EXX Reverse the bits of the mask of a character, using the table.
58296 LD C,(HL)
58297 LD A,(BC)
58298 LD E,A
58299 INC HL Reverse the bits of the pixels of the character.
58300 LD C,(HL)
58301 LD A,(BC)
58302 LD D,A
58303 INC HL Keep them on the stack, and loop back until the row has been done.
58304 PUSH DE
58305 EXX
58306 DJNZ FlipSprite_Row
58308 LD B,C Count the characters again.
FlipSprite_Store 58309 POP DE Put the characters back in the row, in the reverse order.
58310 LD (HL),E
58311 INC HL
58312 LD (HL),D
58313 INC HL
58314 DJNZ FlipSprite_Store
58316 EX AF,AF' Jump to FlipSprite_Done when all the rows have been done.
58317 DEC A
58318 JR Z,FlipSprite_Done
58320 EX AF,AF' Count the characters again, and loop back to do the next row.
58321 LD B,C
58322 JR FlipSprite_Row
FlipSprite_Done 58324 POP DE Get the address of the sprite back, and return.
58325 RET
View the equivalent code in;
Prev: 58156 Up: Map Next: 58326