Prev: A302 Up: Map Next: A3CF
A303: Rub out an object's old image and draw its new one, without flicker
Used by the routines at draw_object_images and draw_star_layer.
The sprite compositor, which draws every ground object and sky object, and the stars (draw_star_layer): about a third of all the game's time goes through here. Both images are worked on in a buffer, never on the screen, and the screen is written once, so nothing flickers. The steps:
1. The box: the smallest rectangle, whole bytes wide, covering both images: its left edge (piece_x) and bottom line (piece_y), and its width in bytes (at most 7) and height plus one, kept in piece_dx and piece_rise. If only one image is present, the other's position is taken as its own.
2. Copy the box from the screen into the buffer (copy_box_in), bottom row first, rows packed at the box's width. The buffer is page $F8, in the middle of the start-up code that runs once from build_shifted_pieces and is no longer wanted; the drawing addresses it by L alone, so the box can be no more than 256 bytes.
3. Rub out the old image in the buffer: AND each byte with the complement of the graphic, shifted to the image's x (erase_old_draw_new, erase_aligned_row, erase_row_shifted). This clears every pixel the graphic sets, whatever was under it; there is no mask, and nothing restores what the old image covered.
4. Draw the new image into the buffer: OR (draw_aligned_row, draw_row_shifted). Then, if the object is dissolving, AND its rows with that step's mask (dissolve_sprite).
5. Copy the box back to the screen (the end of dissolve_sprite onwards), clipped to the play area: column 0 is never written, nor anything right of x=$E8 (the fuel gauge's columns).
Graphics are not pre-shifted. An image at x AND 6 = 2, 4 or 6 is shifted as it is drawn, each byte through a pair of 256-byte tables (shift_tables); x is always even. The loops are unrolled, with patched entry points: the row loops are entered so many steps from their ends (erase_row_routines, draw_row_routines), and the copies are LDI chains entered by DJNZs whose displacements are patched (see copy_box_in).
A sprite straddling the left edge of the play area (sprite_left_clip) is handled by taking $60 off both x positions, which moves positions either side of 0 into one unbroken range; only the part of the box right of the edge, at x=8, is copied in and out (copy_in_clipped, copy_out_part_row).
The cost, worked out from the instructions: for a 3-byte-wide sprite 16 rows tall, moved and shifted, a 5-byte box of 17 rows, about 2,500 T-states to copy in, 4,500 to rub out, 4,100 to draw, 2,500 to copy out -- some 13,500 in all, a fifth of a frame.
Input
DE The new image's first row (new_sprite_header)
B The new image's shift page (set_new_sprite)
IY The old image's erase-row routine (set_old_sprite)
IX The new image's draw-row routine (set_new_sprite)
DE' The old image's first row
B' The old image's shift page
draw_sprite_pair A303 PUSH DE Keep the new image's graphic and page (restored at erase_old_draw_new)
A304 PUSH BC
A305 LD HL,(sprite_width) Keep both widths in bytes; turn the working copies into pixels
A308 LD (old_sprite_width),HL
A30B ADD HL,HL
A30C ADD HL,HL
A30D ADD HL,HL
A30E LD (sprite_width),HL
A311 LD A,(old_sprite_rows) No old image: give it the new one's position, so the box is the new image's alone
A314 AND A
A315 JR NZ,draw_sprite_pair_0
A317 LD HL,(new_sprite_x)
A31A LD (old_sprite_x),HL
draw_sprite_pair_0 A31D LD A,(new_sprite_rows) No new image: give it the old one's
A320 AND A
A321 JR NZ,draw_sprite_pair_1
A323 LD HL,(old_sprite_x)
A326 LD (new_sprite_x),HL
draw_sprite_pair_1 A329 LD A,(sprite_left_clip) Left-clip mode: take $60 off both x positions
A32C AND A
A32D JR Z,draw_sprite_pair_2
A32F LD A,(old_sprite_x)
A332 SUB $60
A334 LD (old_sprite_x),A
A337 LD A,(new_sprite_x)
A33A SUB $60
A33C LD (new_sprite_x),A
draw_sprite_pair_2 A33F LD A,(old_sprite_y) The box's bottom line is the lower of the two bottom lines
A342 LD E,A
A343 LD A,(new_sprite_y)
A346 LD D,A
A347 CP E
A348 JR NC,draw_sprite_pair_3
A34A LD A,E
draw_sprite_pair_3 A34B LD (piece_y),A
A34E LD H,A
A34F LD BC,(old_sprite_rows) The line above the box's top: the smaller of each image's y less its height
A353 LD A,E
A354 SUB C
A355 LD E,A
A356 LD A,D
A357 SUB B
A358 CP E
A359 JR NC,draw_sprite_pair_4
A35B LD E,A
draw_sprite_pair_4 A35C LD A,H The box's rows, plus one
A35D SUB E
A35E INC A
A35F LD (piece_rise),A
A362 LD A,(old_sprite_x) The box's left edge: the smaller x, rounded down to a byte
A365 LD E,A
A366 LD A,(new_sprite_x)
A369 LD D,A
A36A CP E
A36B JR C,draw_sprite_pair_5
A36D LD A,E
draw_sprite_pair_5 A36E AND $F8
A370 LD (piece_x),A
A373 LD L,A
A374 LD BC,(sprite_width) The further right edge of the two, from the box's left edge
A378 LD A,E
A379 SUB L
A37A ADD A,C
A37B LD E,A
A37C LD A,D
A37D SUB L
A37E ADD A,B
A37F CP E
A380 JR NC,draw_sprite_pair_6
A382 LD A,E
draw_sprite_pair_6 A383 ADD A,$06 The box's width in bytes, rounding up (x is always even, so 6 is enough)
A385 LD C,A
A386 SRL A
A388 SRL A
A38A SRL A
A38C LD (piece_dx),A
A38F LD A,(sprite_left_clip) Not straddling the left edge: copy the whole box in (copy_box_in)
A392 AND A
A393 JP Z,copy_box_in
A396 LD A,L Straddling: the bytes of the box right of the play area's edge (x=8, now $A8)
A397 ADD A,C
A398 SUB $A8
A39A SRL A
A39C SRL A
A39E SRL A
A3A0 LD B,A
A3A1 LD A,(piece_dx) Skip the rest in the buffer at the end of each row; start that far into the buffer
A3A4 SUB B
A3A5 LD ($A531),A
A3A8 LD ($A3DC),A
A3AB LD E,A
A3AC LD A,B Patch the DJNZs of the clipped copies in and out to copy that many bytes
A3AD XOR $07
A3AF DEC A
A3B0 ADD A,A
A3B1 ADD A,$03
A3B3 LD ($A520),A
A3B6 LD ($A3CB),A
A3B9 LD C,$FF The screen address of the box's bottom row at the play area's left edge
A3BB LD D,$F8
A3BD LD L,$08
A3BF CALL pixel_to_screen
A3C2 LD (piece_page_x),HL
A3C5 LD A,(piece_rise) The box's rows plus one
A3C8 LD B,A
This entry point is used by the routine at copy_in_clipped.
draw_sprite_pair_7 A3C9 PUSH HL Keep this row's screen address
draw_sprite_pair_8 A3CA DJNZ draw_sprite_pair_8 The DJNZ whose patched displacement enters the chain of LDIs at copy_in_clipped, and leaves when the rows are done
A3CC JP erase_old_draw_new Then rub out and draw
Prev: A302 Up: Map Next: A3CF