Prev: 48489 Up: Map Next: 48731
48558: Draw Sprite
Used by the routine at DrawSpriteList.
Draws a sprite into the buffer, which is the picture of the play area, with twenty four bytes in each of one hundred and twelve rows. The sprite data is a byte for the number of columns, a byte for the number of rows, and for each row, from the bottom to the top, a mask byte and a pixel byte for each column. The mask clears the bits under the sprite and the pixels are then added.
Input
A The number of the sprite
IX The address of the object
Bit 1 of IX+7 is set to show that a sprite has been drawn. The sprite is shifted by the horizontal pixel position, which has to be even, and is flipped if bit 6 of the flags of the object is set. A shifted sprite is one byte wider, and the shifting is done by tables, which are built by BuildShiftTables. The row of pixels is drawn by code for the right number of columns: the code was unrolled so that it can be fast, and the address in the jump at SpriteJumpOpcode is set to the right place in it. The sprite data is taken off the stack. A sprite which is partly below the bottom or above the top of the play area has the rows which are outside it left out.
DrawSprite 48558 LD BC,26612 Point BC at SpritePointers.
Fetch the address of the sprite data.
48561 CALL GetTableEntry Call GetTableEntry.
48564 EX DE,HL Swap the address into DE.
48565 BIT 6,(IX+7) Jump to DrawFlippedSprite to draw the flipped sprite if the object is flipped.
48569 JP NZ,DrawFlippedSprite
Fetch the horizontal pixel position.
48572 LD A,(41504) Fetch SpriteHorizontalPosition.
48575 AND %00000110 Keep the shift, which is zero, two, four or six pixels.
Draw the sprite unshifted.
48577 JP Z,DrawSprite_Aligned Jump to DrawSprite_Aligned if there is no shift.
48580 OR %11111000 Make the high byte of the address of the table for the shift.
48582 LD H,A Set H.
48583 LD A,(DE) Fetch the number of columns of the sprite.
48584 INC DE Move on to the next byte.
48585 AND %00000111 Keep the low three bits.
48587 INC A Add one, as a shifted sprite is one byte wider.
48588 LD B,A Keep the widened column count in B.
Store the number of bytes in a row of the sprite.
48589 LD (41506),A Set SpriteRowBytes.
48592 DEC A Take one off again.
48593 AND %00000111 Keep the low three bits.
48595 ADD A,A Double it.
48596 LD C,A Keep twice the column count in C.
48597 ADD A,A Multiply it by eight to make sixteen times the number of columns.
48598 ADD A,A
48599 ADD A,A
48600 ADD A,C Add the double to make eighteen times.
48601 NEG Make it negative.
48603 ADD A,93 Add 93 which gives the distance from DrawSprite_Jump to the code for the number of columns. Each column has eighteen bytes of code.
DrawSprite_SetJump 48605 PUSH BC Stash the number of columns on the stack.
48606 PUSH HL Stash the high byte of the shift table on the stack.
48607 LD H,0 Clear H.
48609 AND A Test the sign of the distance.
48610 JP P,DrawSprite_SetJumpAddress Jump to DrawSprite_SetJumpAddress if it is positive.
48613 DEC H Make HL negative.
DrawSprite_SetJumpAddress 48614 LD L,A Put the distance in L.
48615 LD BC,48775 Point BC at DrawSprite_Jump.
48618 ADD HL,BC Add the distance to give the address to jump to.
Set the address of the jump at SpriteJumpOpcode.
48619 LD (41509),HL Set SpriteJumpAddress.
48622 POP HL Restore the shift table from the stack.
48623 POP BC Restore the number of bytes in a row from the stack.
48624 LD A,B Fetch the number of bytes in a row.
48625 CPL Invert it.
48626 ADD A,26 Add 26 to give twenty five minus the number of bytes.
Store the distance from the end of a row to the start of the next.
48628 LD (41511),A Set SpriteRowStep.
48631 EX DE,HL Swap the address of the sprite data into HL.
48632 LD C,112 Set the number of rows in the buffer.
Fetch the vertical position.
48634 LD A,(41505) Fetch SpriteVerticalPosition.
48637 SUB 72 Subtract the position of the bottom row of the buffer.
48639 JR C,DrawSprite_Below Jump to DrawSprite_Below if the sprite starts below it.
48641 SUB C Subtract the number of rows in the buffer.
48642 RET NC Return if the sprite starts above the top.
48643 NEG Make it positive, which is the number of rows up to the top.
48645 CP (HL) Compare it with the number of rows in the sprite.
48646 JR C,DrawSprite_FirstByte Jump to DrawSprite_FirstByte if there is room for fewer rows.
48648 LD A,(HL) Use the number of rows in the sprite.
DrawSprite_FirstByte 48649 INC HL Move on to the first byte of data.
Store the number of rows to draw.
48650 LD (41507),A Set SpriteRowsLeft.
DrawSprite_StartRows 48653 EX DE,HL Swap the address of the data into DE.
48654 PUSH HL Stash the high byte of the shift table on the stack.
Fetch the pixel position.
48655 LD HL,(41504) Fetch SpriteHorizontalPosition.
Find the address in the buffer.
48658 CALL CalculateBufferAddress Call CalculateBufferAddress.
48661 LD C,L Keep the buffer address in BC.
48662 LD B,H
48663 POP HL Restore the shift table from the stack.
Save the stack pointer.
48664 LD (41501),SP Set SavedStackPointer.
48668 EX DE,HL Point the stack pointer at the sprite data.
48669 LD SP,HL
48670 EX DE,HL
48671 SET 1,(IX+7) Set the flag which shows that a sprite has been drawn.
Fetch the number of rows to draw.
48675 LD A,(41507) Fetch SpriteRowsLeft.
Draw the first row.
48678 JR DrawSpriteRow Jump to DrawSpriteRow.
DrawSprite_Below 48680 NEG Make the number of rows below the bottom positive.
48682 LD C,A Keep the rows below the bottom of the sprite in C.
48683 LD A,(HL) Fetch the number of rows in the sprite.
48684 SUB C Subtract the number below the bottom.
48685 RET M Return if the sprite is all below the bottom.
48686 RET Z Return if there is nothing left.
Store the number of rows to draw.
48687 LD (41507),A Set SpriteRowsLeft.
48690 LD B,0 Clear B.
48692 DEC HL Move back to the first byte of the sprite data.
48693 LD A,(HL) Fetch the number of columns.
48694 AND %00111111 Keep the number of columns.
48696 INC HL Move on to the data.
48697 INC HL
48698 SLA C Double the number of rows to skip.
48700 RL B
DrawSprite_SkipRows 48702 ADD HL,BC Skip two bytes for each row below the bottom in each column.
48703 DEC A
48704 JR NZ,DrawSprite_SkipRows
Start drawing at the bottom of the buffer.
48706 LD A,72 Set SpriteVerticalPosition to the bottom of the buffer.
48708 LD (41505),A
Start the rows.
48711 JR DrawSprite_StartRows Jump to DrawSprite_StartRows.
DrawSprite_Aligned 48713 LD A,(DE) Fetch the number of columns of the sprite.
48714 INC DE Move on to the next byte.
48715 AND %00001111 Keep the low four bits.
Store the number of bytes in a row of the sprite.
48717 LD (41506),A Set SpriteRowBytes.
48720 LD B,A Keep the bytes in a row in B.
48721 ADD A,A Multiply it by eight.
48722 ADD A,A
48723 ADD A,A
48724 NEG Make it negative.
48726 SUB 4 Subtract four, which gives the distance from DrawSprite_Jump to the code for the number of columns. Each column has eight bytes of code.
Set the address of the jump.
48728 JP DrawSprite_SetJump Jump to DrawSprite_SetJump.
Prev: 48489 Up: Map Next: 48731