Copies a masked sprite from an array to video memory (or to a screen buffer), using mask as transparency information, to prevent erasing the background.
void cpct_drawSpriteMasked (void* sprite, void* memory, u8 width, u8 height) __z88dk_callee;
(2B HL) sprite | Source Sprite Pointer (array with pixel and mask data) |
(2B DE) memory | Destination video memory pointer |
(1B C ) width | Sprite Width in bytes (>0) (Beware, not in pixels!) |
(1B B ) height | Sprite Height in bytes (>0) |
call cpct_drawSpriteMasked_asm
mode 0 | 1 byte = 2 pixels |
modes 1 / 3 | 1 byte = 4 pixels |
mode 2 | 1 byte = 8 pixels |
This function copies a generic WxH bytes sprite from memory to a video-memory location (either present video-memory or software / hardware backbuffer). The original sprite must be stored as an array (i.e. with all of its pixels stored as consecutive bytes in memory). It works in a similar way to cpct_drawSprite, but taking care about transparency information encoded in mask bytes. For detailed information about how sprite copying works, and how video memory is formatted, take a look at cpct_drawSprite.
For this routine to work, the sprite must contain mask information: inside the sprite array, for each byte containing screen colour information, there must be a preceding byte with mask information. So, the format is as depicted in example 1:
Array storage format: <-byte 1-> <-byte 2-> <-byte 3-> <-byte 4-> <- mask -> <-colour-> <- mask -> <-colour-> ------------------------------------------------------------------------------ u8* sprite = { 0xFF, 0x00, 0x00, 0xC2, .... }; ------------------------------------------------------------------------------ Video memory output: <- 1st Screen byte -> <- 2nd Screen byte -> ______________________________________________________________________________ Example 1: Definition of a masked sprite and byte format
In example 1, each “Screen byte” will become 1 byte outputted to video memory resulting of the combination of 3 bytes: 1-byte mask, 1-byte sprite colour data and 1-byte previous screen colour data. The combination of these 3 bytes results in sprite colour data being “blended” with previous screen colour data, adding sprite pixels with background pixels (the ones over transparent pixels).
The way this function works is by getting sprite bytes two by two, operating with them, and copying them to video memory (or backbuffer). Each two bytes got (mask + sprite colour information) are mixed with an AND opreation to remove (set to 0) sprite background pixels. After that, an OR operation between the resulting byte and the background (the present byte at video memory location where we want to write) is performed. That effectively mixes sprite colours with background colours, after removing background pixels from the sprite.
AF, BC, DE, HL
C-bindings | 47 bytes |
ASM-bindints | 42 bytes |
Case | microSecs (us) | CPU Cycles ---------------------------------------------------------------- Best | 21 + (22 + 18W)H + 10HH | 84 + (88 + 72W)H + 40HH Worst | Best + 10 | Best + 40 ---------------------------------------------------------------- W=2,H=16 | 957 / 967 | 3828 / 3868 W=4,H=32 | 3057 / 3067 | 12228 / 12268 ---------------------------------------------------------------- Asm saving | -16 | -64 ----------------------------------------------------------------
W = width in bytes, H = height in bytes, HH = [(H-1)/8]