Prev: 56095 Up: Map Next: 56309
56216: Draw/erase graphics - screen boundary checks
Used by the routines at 49215, 53885, 54342, 54450, 55885, 58476, 59783 and 59869.
Input
HL Address pointer for graphics to draw, from address pointer table at 38851
The first check is to see if the item's horizontal/vertical pixel co-ordinates fall within the bounds of the visible playing area.
The checks also test for cases where part of the graphic is on-screen - it's only considered 'off-screen' if none of it needs drawing.
56216 LD E,(HL) Transfer the address (from 38851) pointed to by HL into the DE register
56217 INC HL
56218 LD D,(HL)
56219 EX DE,HL Swap back so HL now points to the graphics data, e.g. 39248
56220 LD A,0 This is the flag indicating whether (any part of) the graphic is visible on-screen (0 = not visible, 1 = visible)
It'll be updated shortly once we've worked out if any part of the graphic is on-screen or not
56222 LD (60117),A This is set to not visible (0) for off-screen objects, but if any part of the item needs drawing, it's set to 1 (visible) at 56309
56225 LD A,(HL) Number of tiles that make up the graphic/sprite
56226 LD (60112),A ...and store...
56229 INC HL HL now at the start of the graphic's first tile data
This entry point is used by the routine at 56439.
56230 LD C,(HL) Store this byte in C register. This is the horizontal pixel offset for drawing this tile from its screen X/Y co-ordinates
56231 INC HL Move onto the next byte
56232 EX DE,HL Swap the graphics address pointer back into the DE register
Calculate horizontal (X) pixel offset, check for out of bounds
56233 LD HL,(60115) Get graphic X horizontal (pixel) position from character column 1 on the left of the screen (the right hand part of the left border).
56236 LD A,C Graphic's horizontal offset in pixels
56237 RLA If the offset is >=128, it's a negative pixel offset (e.g. 253 = -3 pixels)
56238 SBC A,A ...So if the RLA creates a carry, the SBC will set A to to 255 (-1). Otherwise 0.
56239 LD B,A Transfer to B. This determines if the offset value gets subtracted or added in the next instruction.
56240 ADD HL,BC Add horizontal pixel offset to horizontal pixel drawing position
56241 EX DE,HL Swap into DE
56242 LD A,D Get the high byte of the horizontal position as just calculated
56243 CP 0 If high byte changed from 0 to 1, the graphic has gone out of the screen's viewport boundary
56245 JP NZ,56439 ...if so, skip out of this routine to deal with the out-of-bounds graphic
56248 LD A,E Get horizontal pixel position
56249 CP 232 Check to see if this is in the right hand border screen position - 232/8 = 29, but as the counting starts at pixel column 8 (character column 1, rather than 0), this will be character column 30 in the right hand border
56251 JP NC,56439 If there's no carry, jump out of this routine to deal with the out-of-bounds graphic
56254 ADD A,8 The screen display address is calculated (in the next routine) from the leftmost screen column - i.e. column 0, not column 1 as currently stored - so add 8 so that this is taken into account.
56256 LD E,A Store in E register ready for the next part of the routine at 56309
56257 AND 7 Take lowest 3 bits (values 0-7)
56259 LD B,A Store in B register - this is the number of pixel shifts needed for the graphic at 56370
Calculate vertical (Y) pixel offset, check for out of bounds
56260 LD A,(60114) Get graphic Y vertical (pixel) position, e.g. 80 for character row 10 (80/8)
56263 ADD A,(HL) Add to the graphic vertical offset, e.g. an offset of 231 means a -25 vertical pixel offset to the existing co-ordinate
56264 INC HL Move HL to the next pre-graphics byte address
56265 LD C,(HL) Number of bytes in the next tile graphic
56266 INC HL HL now points to first graphic tile byte ready for drawing
56267 CP 112 Vertical pixel position 112 is the top of the scroll, which is out of bounds for drawing graphics
56269 JP C,56298 Carry means (at least the top part of) the item is within the screen viewport and drawable, so jump to 56298
A no-carry indicates the vertical-pixel-position-plus-offset is either above the top of the playing area or into the scroll area.
However, some part of the item may be visible; for example the vertical corner floor/wall connectors can be partly off the top of the playing area, but the rest of them may be visible on-screen.
56272 LD D,A Store vertical (Y) pixel position
56273 ADD A,C Add the number of bytes in the tile to the current out-of-bounds position
56274 JP NC,56442
56277 JP Z,56442 A no-carry or zero result means that no part of the object is visible, so this graphic doesn't need drawing. Jump out here to re-calculate address pointers etc.
Some (lower) part of this graphic is visible at the top of the screen. Put the graphics address pointer to the right part of the graphics and set the pixel row number in the line.
56280 LD (60113),A Store the number of bytes in the visible part of the graphic tile left to draw
56283 LD A,D Retrieve the vertical pixel co-ordinate (plus offset). This will be a negative number...
56284 NEG So make it positive...
56286 ADD A,L HL is pointing at the address for the tile graphics...
56287 LD L,A
56288 LD A,0
56290 ADC A,H ...So increase it by the number of bytes that don't need to be drawn as they're in/above the decorative border
56291 LD H,A
56292 LD D,16 Set the vertical screen position to 16 pixel rows down - the first byte of character row 2, just below the top border
56294 LD C,0 C register contains the pixel row; as we're starting from character row 2 means that the first byte will be pixel row 0
56296 JR 56309 Ready to calculate screen address low/high bytes
Jump from 56269. The graphic (or at least the top of it) are drawable. Set the screen vertical pixel position and pixel row in the line (0-7).
56298 ADD A,16 Vertical pixel position starts from 0 (16 pixels from top of screen...
56300 LD D,A D contains vertical Y screen pixel position
56301 LD A,C Get number of tile bytes to draw
56302 LD (60113),A ...and store...
56305 LD A,D Get A back from D (mystery value - now 71)
56306 AND 7 Take lowest 3 bits (0,1,2) - pixel row number in line
56308 LD C,A Store in C register for the next part of the routine
Prev: 56095 Up: Map Next: 56309