Prev: 58329 Up: Map Next: 58527
58367: Draw Sprite
Used by the routine at UpdateLogoPiece.
Draws the sprite of an object in the screen buffer, with its mask. The sprite is clipped at the top and bottom of the window by its position. A sprite is drawn by popping its bytes off the stack, so the code that does the drawing changes itself to fit the sprite.
Only the rows of a player sprite that the timer at MaterialiseTimer allows are drawn, so the player appears from the bottom to the top while materialising.
The bottom of the buffer is the top of the window, and the rows of a sprite are stored from the bottom upwards, so a sprite starts at its Y position and is drawn towards the top of the screen.
Input
IX Object
DrawSprite 58367 CALL FlipSprite Turn the sprite round or upside down, if it needs it.
58370 LD A,(IX+14) Work out the position of the sprite in a character, in pixels (0, 2, 4 or 6).
58373 AND 6
58375 JP Z,DrawSprite_Aligned Jump to DrawSprite_Aligned if the sprite is on a character boundary.
58378 OR 248 Make the high byte of the address of the table for shifting the sprite.
58380 LD H,A
58381 LD A,(DE) Fetch the width of the sprite, and add one for the part that is shifted into the next character.
58382 INC DE
58383 AND 7
58385 INC A
58386 LD B,A
58387 LD (48066),A Keep the number of characters to draw.
58390 DEC A Double the width of the sprite in characters.
58391 AND 7
58393 ADD A,A
58394 LD C,A
58395 ADD A,A Multiply it by nine, to make eighteen bytes of code for each character.
58396 ADD A,A
58397 ADD A,A
58398 ADD A,C
58399 NEG Subtract the length of the code from the end of the shifted row.
58401 ADD A,90
Change the code to jump into the row.
DrawSprite_Patch 58403 LD (58572),A Set the offset of the jump in the code.
58406 LD A,B Work out the number of bytes to move on to the next row, and set it in the code.
58407 CPL
58408 ADD A,26
58410 LD (58666),A
58413 EX DE,HL Start with the sprite.
58414 LD A,(IX+0) Check whether the object is one of the legs or the body of the player.
58417 SUB 16
58419 CP 32
58421 LD A,112 Use a height of 112 (the whole window).
58423 JR NC,DrawSprite_Clip Jump to DrawSprite_Clip unless it is the player.
58425 LD A,(48130) Use the height of the player while materialising.
Clip the sprite at the top and bottom of the window.
DrawSprite_Clip 58428 LD C,A Keep the height.
58429 LD A,(IX+15) Work out the position of the sprite in the window, and jump to DrawSprite_Below if it starts below the window.
58432 SUB 72
58434 JR C,DrawSprite_Below
58436 SUB C Return if it starts above the height of the window.
58437 RET NC
58438 NEG Work out the number of rows that fit between the sprite and the top of the window.
58440 CP (HL) Jump to DrawSprite_Rows if fewer rows fit than there are in the sprite.
58441 JR C,DrawSprite_Rows
58443 LD A,(HL) Otherwise use the number of rows of the sprite.
DrawSprite_Rows 58444 INC HL Point at the first row of the sprite, and keep the number of rows to draw.
58445 LD (48067),A
Set up the registers, and draw the rows of the sprite.
DrawSprite_Draw 58448 EX DE,HL Keep the address of the sprite.
58449 PUSH HL
58450 LD L,(IX+14) Fetch the position of the sprite.
58453 LD H,(IX+15)
58456 CALL CalculateBufferAddress Find the address of the position in the screen buffer.
58459 LD C,L Move it into BC.
58460 LD B,H
58461 POP HL Get the address of the sprite back.
58462 LD (48046),SP Keep the stack pointer.
58466 EX DE,HL Point the stack pointer at the sprite.
58467 LD SP,HL
58468 EX DE,HL
58469 SET 1,(IX+7) Flag that the object has been drawn.
58473 LD A,(48067) Fetch the number of rows to draw.
58476 JR DrawSpriteRow Jump to DrawSpriteRow to draw them.
The sprite starts below the window, so skip the rows that are not visible.
DrawSprite_Below 58478 NEG Work out the number of rows to skip.
58480 LD C,A
58481 LD A,(HL) Work out the number of rows that are left.
58482 SUB C
58483 RET M Return if no rows are left.
58484 RET Z
58485 LD (48067),A Keep the number of rows to draw, and clear B.
58488 LD B,0
58490 DEC HL Fetch the width of the sprite.
58491 LD A,(HL)
58492 AND 63
58494 INC HL Point at the first row.
58495 INC HL
58496 SLA C Work out the number of bytes that make up a row, and double it.
58498 RL B
DrawSprite_Skip 58500 ADD HL,BC Skip the rows, one at a time.
58501 DEC A
58502 JR NZ,DrawSprite_Skip
58504 LD (IX+15),72 Start the sprite at the bottom of the window.
58508 JR DrawSprite_Draw Jump to DrawSprite_Draw to draw the sprite.
The sprite is on a character boundary.
DrawSprite_Aligned 58510 LD A,(DE) Fetch the width of the sprite.
58511 INC DE
58512 AND 15
58514 LD (48066),A Keep the number of characters to draw.
58517 LD B,A Make it the count of characters.
58518 ADD A,A Multiply it by eight.
58519 ADD A,A
58520 ADD A,A
58521 NEG Negate it.
58523 SUB 6 Take off the length of the code that is left after the characters.
58525 JR DrawSprite_Patch Jump to DrawSprite_Patch to change the code.
Prev: 58329 Up: Map Next: 58527