Returns a byte-pointer to a screen memory location, given its X, Y coordinates. (in bytes, NOT in pixels!)
u8* cpct_getScreenPtr (void* screen_start, u8 x, u8 y)
(2B DE) screen_start | Pointer to the start of the screen (or a backbuffer) |
(1B C ) x | [0-79] Byte-aligned column starting from 0 (x coordinate, |
(1B B ) y | [0-199] row starting from 0 (y coordinate) in bytes) |
call cpct_getScreenPtr_asm
(HL) u8* | Pointer to the (x,*y*) byte in the screen memory that starts at screen_start. |
This function returns a pointer to the byte whose screen coordinates are (x, y) in bytes, being screen_start the screen origin (0, 0) or, what is the same, a pointer to the first byte of the screen. The “screen” may be the actual screen or a backbuffer with same layout.
What this function exactly does is this calculation,
return screen_start + 80 * ((int)(y / 8)) + 2048 * (y % 8) + x
This calculation is based on the default layout of the screen, which is 80 bytes wide (80 bytes per pixel line) and 200 pixel lines high. Pixel lines are grouped into 25 groups of 8 pixel lines, as it is explained in cpct_drawSprite. Each group of 8 pixel lines starts 0x50 bytes away from the previous one (0x50 = 80, hence the term [80 * ((int)(y / 8))]) and the each lines inside a group is separated by 0x800 bytes from its predecessor (0x800 = 2048, and we have the other term [2048 * (y % 8)]).
All the previous paragraph explains how much we have to displace from screen_start to arrive to the start of the y pixel line. Once we are there, the byte x is just x bytes away, so we only have to add x to the formula, and we are done.
AF, BC, DE, HL
C-bindints | 32 bytes |
ASM-bindints | 28 bytes |
Case | microSecs (us) | CPU Cycles ----------------------------------------- Any | 53 | 212 ----------------------------------------- Asm saving | -13 | -52 -----------------------------------------