![]() |
PicoVGA
1.0-cmake
VGA/TV display on Raspberry Pico
|
Sprites can be used in overlay planes with KEY, BLACK and WHITE programs. There are two ways to use the sprites:
When using sprites, the first step will be to specify the LAYERMODE_*SPRITE* layer mode for the VgaCfg() initialization function.
The second step will be to build an array of sprite pattern line starts and lengths using the SpritePrepLines() function. The function will be passed a pointer to the image of each sprite (only 8-bit sprites are supported), the image dimensions, the pointers to the array of origin and line lengths (the array dimensions correspond to the height of the sprite), and the key transparency color. The function searches for line starts and line ends and writes them into the fields. The 'fast' parameter specifies whether the tables are generated for fast sprites, in which case the line starts and lengths are divided by 4. For slow sprites, the sprite width must be limited to 255 pixels.
The third step is to build a list of sprites and initialize the sprites - especially the pointer to the image, the dimensions and coordinates of the sprites. The sprite list is an array of pointers to the sprite. Each sprite can only be in the list once, but multiple sprite can share the same sprite image and the same array of line starts and lengths. Slow sprites can have coordinates outside the allowed range (they will be cropped), but for fast sprites I recommend not to exceed the horizontal limits of the screen, the cropping of the image is not yet properly tuned and the program might crash.
While the sprites don't have a parameter to turn them off, they can be turned off by setting the Y coordinate out off screen. During rendering, visible sprites are searched for by the Y coordinate, an invalid Y coordinate will ensure that the sprite is safely disabled.
Fast sprites require sorting the list by the X coordinate. This is done by the SortSprite() function, which is passed a pointer to the list of sprites and the number of sprites in the list. This function should be called whenever you change the X coordinate of the sprite. Transient conditions (e.g. momentary mis-overlapping of sprites) do not matter, they are just short-term optical errors, they do not compromise the program. The function sorts using the bubble method, so it is quite slow, but so far it does not seem to harm anything (there are not many sprays).
The last step is the initialization of the layer with the sprite. The function was described in the previous chapter.
The next step is to turn on layer visibility with LayerOn() and control the sprites by changing their X and Y coordinates and changing their img images.
Functions | |
void | LayerSpriteSetup (u8 inx, sSprite **sprite, u16 spritenum, const sVmode *vmode, s16 x, s16 y, u16 w, u16 h, u8 col=0) |
Setup overlapped layer 1..3 for LAYERMODE_SPRITE* and LAYERMODE_FASTSPRITE* modes. More... | |
void | SpritePrepLines (const u8 *img, u8 *x0, u8 *w0, u16 w, u16 h, u16 wb, u8 col, Bool fast) |
Prepare array of start and length of lines (detects transparent pixels) More... | |
void | SortSprite (sSprite **list, int num) |
Sort fast sprite list by X coordinate. More... | |
void LayerSpriteSetup | ( | u8 | inx, |
sSprite ** | sprite, | ||
u16 | spritenum, | ||
const sVmode * | vmode, | ||
s16 | x, | ||
s16 | y, | ||
u16 | w, | ||
u16 | h, | ||
u8 | col = 0 |
||
) |
Setup overlapped layer 1..3 for LAYERMODE_SPRITE* and LAYERMODE_FASTSPRITE* modes.
It differs from the other setup functions by specifying the coordinate of the sprite area, the pointer to the sprite address array and the number of sprites.
inx | Layer index 1..3 |
sprite | Pointer to list of sprites (array of pointers to sprites; sorted by X on LAYERMODE_FASTSPRITE* modes) |
spritenum | Number of sprites in the list (to turn sprite off, you can set its coordinate Y out of the screen) |
vmode | Pointer to initialized video configuration |
x | Start coordinate X of area with sprites |
y | Start coordinate Y of area with sprites |
w | Width of area with sprites (must be multiple of 4) |
h | Height of area with sprites |
col | Key color (needed for LAYERMODE_SPRITEKEY and LAYERMODE_FASTSPRITEKEY layer mode) |
void SpritePrepLines | ( | const u8 * | img, |
u8 * | x0, | ||
u8 * | w0, | ||
u16 | w, | ||
u16 | h, | ||
u16 | wb, | ||
u8 | col, | ||
Bool | fast | ||
) |
Prepare array of start and length of lines (detects transparent pixels)
The function will be passed a pointer to the image of each sprite (only 8-bit sprites are supported), the image dimensions, the pointers to the array of origin and line lengths (the array dimensions correspond to the height of the sprite), and the key transparency color. The function searches for line starts and line ends and writes them into the fields. The 'fast' parameter specifies whether the tables are generated for fast sprites, in which case the line starts and lengths are divided by 4. For slow sprites, the sprite width must be limited to 255 pixels.
img | Pointer to image |
x0 | Array of start of lines |
w0 | Array of length of lines |
w | Sprite width (slow sprite: max. width 255) |
h | Sprite height |
wb | Sprite pitch (bytes between lines) |
col | Key color |
fast | Fast sprite, divide start and length of line by 4 |
void SortSprite | ( | sSprite ** | list, |
int | num | ||
) |
Sort fast sprite list by X coordinate.
Fast sprites require sorting the list by the X coordinate. Pass a pointer to the list of sprites and the number of sprites in the list. This function should be called whenever you change the X coordinate of the sprite. Transient conditions (e.g. momentary mis-overlapping of sprites) do not matter, they are just short-term optical errors, they do not compromise the program. The function sorts using the bubble method, so it is quite slow, but so far it does not seem to harm anything (there are not many sprays).
list | Sprite list |
num | Number of sprites |