Draws sprites blending them with current contents of screen video memory. It uses XOR by default, but many other blending modes are available.
void cpct_drawSpriteBlended (void* memory, u8 height, u8 width, void* sprite) __z88dk_callee;
(2B DE) memory | Destination video memory pointer |
(1B C ) height | Sprite Height in bytes (>0) |
(1B B ) width | Sprite Width in bytes (>0) (Beware, not in pixels!) |
(2B HL) sprite | Source Sprite Pointer (array with pixel data) |
call cpct_drawSpriteBlended_asm
mode 0 | 1 byte = 2 pixels |
modes 1 / 3 | 1 byte = 4 pixels |
mode 2 | 1 byte = 8 pixels |
This function performs, by default, a XOR blending operation. However, This function can perform many blending operations. Consult all available blending modes at CPCT_BlendMode documentation.
Standard modes included: XOR (default), OR, AND, ADD, ADC, SUB, SBC, LDI and NOT.
This function blends a generic WxH bytes sprite from memory to a video-memory location (either present video-memory or software / hardware backbuffer). It performs similar to cpct_drawSprite but instead of copying bytes and overwrite video-memory, it blends them with present contents of video-memory destination (background).
The process is as follows. The function gets 1 byte from the destination video-memory and 1 byte from the sprite. It then performs a blending operation between both bytes and stores the result in the same place where it got the byte from video memory. The process is repeated for every byte from the sprite.
The blending operation that this function performs is selectable. By default, this operation will be a XOR between both bytes. To select a different operation mode, the function cpct_setBlendMode should be called.
This function may be used directly to draw a sprite in video-memory:
// This function will draw the sprite of the main Character void drawCharacterSprite(u8 x, u8 y) { // First, calculate video-memory location of the (x,y) // coordinates where the character is located u8 *pmem = cpct_getScreenPtr(CPCT_VMEM_START, x, y); // Then, draw the sprite, blending it with the background cpct_drawSpriteBlended(pmem, 24, 8, sprite); }
Previous function drawCharacterSprite could also be used to erase the sprite, leaving video-memory as it was before drawing the sprite, if the selected blending mode is XOR. Calling the function again with same coordinates will do the trick, as M XOR S XOR S = M (Doing a double XOR operation to any (M)emory byte against any (S)prite byte will leave M untouched).
This function could even be used as a normal drawSprite function without width limits (as cpct_drawSprite cannot draw sprites wider than 63 bytes). This can be performed using the LDI mode:
// Draw the user interface of the application, that is composed // of several sprites void drawUserInterface() { // First, select LDI as blending mode for cpct_drawSpriteBlended cpct_setBlendMode(CPCT_BLEND_LDI); // Now print all sprites of the UI at their pre-calculated locations // Note: assume *LOCATION macros are just absolute video-memory addresses cpct_drawSpriteBlended( VMEM, 80, 200, background); cpct_drawSpriteBlended( SCOREBOARD_LOCATION, 80, 20, score_board); cpct_drawSpriteBlended( LEFT_COLUMN_LOCATION, 10, 100, left_column); cpct_drawSpriteBlended(RIGHT_COLUMN_LOCATION, 10, 100, right_column); }
AF, BC, DE, HL
C-bindings | 43 bytes |
ASM-bindings | 39 bytes |
Case | microSecs (us) | CPU Cycles | ------------------------------------------------------------------------- Best | 32 + 21H + (12+B)WH + 10J | 128 + 84H + (48+4B)WH + 40J | Worst | Best + 10 | Best + 40 | ------------------------------------------------------------------------- W=2,H=16,B=2 | 826 / 836 | 3304 / 3344 | W=4,H=32,B=2 | 2526 / 2536 | 10104 / 10144 | ------------------------------------------------------------------------- Asm saving | -15 | -60 | -------------------------------------------------------------------------
W = width in bytes, H = height in bytes
B = Blend operation nanoseconds, WH = W * H, J = [(H-1)/8]
Standard Blend operations take 2 nanoseconds except NOP and LDI, which take 1