Prev: 55368 Up: Map Next: 55518
55397: 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 the table of reversed bytes.
Input
DE Address of the sprite
IX Address of the object
Output
DE Address of the sprite
Flip the sprite vertically if the object's flags say so.
FlipSprite 55397 PUSH DE Stash the address of the sprite on the stack.
55398 LD A,(DE) Fetch the width of the sprite.
55399 XOR (IX+7) Compare the vertical flip with the one of the object.
55402 AND %10000000 Keep it.
55404 JR Z,FlipSprite_Horizontal Jump to FlipSprite_Horizontal if they are the same.
55406 LD A,(DE) Fetch the width byte of the sprite.
55407 XOR %10000000 Toggle the vertical flip.
55409 LD (DE),A Store the width.
55410 RLCA Double the width, for the mask and the data.
55411 AND %00011110 Keep the doubled width and drop the flags.
55413 LD B,A Stash it in B.
55414 INC DE Point DE at the height of the sprite.
Fetch the height and stash it in C.
55415 LD A,(DE) Fetch the height.
55416 LD C,A Copy it to C.
55417 INC DE Point DE at the data.
55418 PUSH DE Stash the address of the data on the stack.
55419 LD E,B Set DE to the number of bytes in a line.
55420 LD D,0
55422 CALL MultiplyAByDE Multiply it by the height.
55425 POP DE Restore the address of the data from the stack.
55426 ADD HL,DE Add the number of bytes of data.
55427 EX DE,HL Point DE at the end of the data, and HL at the start.
55428 LD A,B Fetch B.
55429 CALL AddAToHL Call AddAToHL.
55432 DEC DE Point DE at the last byte of the last line.
55433 DEC HL Point HL at the last byte of the first line.
55434 SRL C Halve the height to get the number of pairs of lines.
FlipSprite_Lines 55436 PUSH BC Stash the number of bytes in a line and the number of pairs on the stack.
FlipSprite_Bytes 55437 LD A,(DE) Fetch a byte of the last line.
55438 LD C,(HL) Fetch a byte of the first line.
55439 LD (HL),A Store the byte of the last line in the first line.
55440 LD A,C Fetch the byte of the first line.
55441 LD (DE),A Store it in the last line.
55442 DEC HL Go back a byte in the first line.
55443 DEC DE Go back a byte in the last line.
55444 DJNZ FlipSprite_Bytes Jump to FlipSprite_Bytes until the lines are swapped.
55446 POP BC Restore the number of bytes in a line and the number of pairs from the stack.
55447 LD A,B Fetch B.
55448 CALL AddAToHL Call AddAToHL.
55451 LD A,B Add it again, to go to the end of the next line.
55452 CALL AddAToHL
55455 DEC C Count down the pairs of lines.
55456 JR NZ,FlipSprite_Lines Jump to FlipSprite_Lines until all of the pairs are swapped.
Flip the sprite horizontally if the object's flags say so.
FlipSprite_Horizontal 55458 POP DE Fetch the address of the sprite and keep it on the stack.
55459 PUSH DE
55460 LD A,(DE) Fetch the width of the sprite.
55461 XOR (IX+7) Compare the horizontal flip with the one of the object.
55464 AND %01000000 Keep it.
55466 JR Z,FlipSprite_Done Jump to FlipSprite_Done if they are the same.
55468 LD A,(DE) Fetch the width byte of the sprite.
55469 XOR %01000000 Toggle the horizontal flip.
55471 LD (DE),A Store the width.
55472 AND %00001111 Keep the width.
55474 LD B,A Copy it to B.
55475 LD C,A Copy it to C.
55476 INC DE Point DE at the height of the sprite.
55477 LD A,(DE) Fetch the height.
55478 EX AF,AF' Stash it in the alternate A.
55479 INC DE Point DE at the data.
55480 EX DE,HL Point HL at the data.
55481 PUSH HL Stash the address of the data on the stack.
55482 EXX Switch to the alternate registers.
55483 POP HL Restore the address of the data from the stack.
55484 LD B,241 Point BC at the table of reversed bytes.
55486 EXX Switch back to the main registers.
Reverse the bits of each pair and store the line back in reverse order.
FlipSprite_Line 55487 EXX Switch to the alternate registers.
55488 LD C,(HL) Fetch the mask byte of a pair.
55489 LD A,(BC) Find the reversed byte.
55490 LD E,A Stash it in E.
55491 INC HL Move on.
55492 LD C,(HL) Fetch the data byte of the pair.
55493 LD A,(BC) Find the reversed byte.
55494 LD D,A Stash it in D.
55495 INC HL Move on.
55496 PUSH DE Stash the reversed pair on the stack.
55497 EXX Switch back to the main registers.
55498 DJNZ FlipSprite_Line Jump to FlipSprite_Line until all of the pairs of the line are on the stack.
55500 LD B,C Set the number of pairs.
FlipSprite_Store 55501 POP DE Restore a reversed pair from the stack, which comes off in the opposite order.
Store the mask byte, and move on.
55502 LD (HL),E Store the mask byte.
55503 INC HL Move on.
55504 LD (HL),D Store the data byte.
55505 INC HL Move on.
55506 DJNZ FlipSprite_Store Jump to FlipSprite_Store until the line is stored.
55508 EX AF,AF' Fetch the number of lines left.
55509 DEC A Count down the lines.
55510 JR Z,FlipSprite_Done Jump to FlipSprite_Done if all of the lines are done.
55512 EX AF,AF' Stash the number of lines left.
55513 LD B,C Set the number of pairs.
55514 JR FlipSprite_Line Jump to FlipSprite_Line.
FlipSprite_Done 55516 POP DE Restore the address of the sprite from the stack.
55517 RET Return.
View the equivalent code in;
Prev: 55368 Up: Map Next: 55518