Allegro Manual: Compiled sprites

# Compiled sprites

- destroy_compiled_sprite — Destroys a compiled sprite.
- draw_compiled_sprite — Draws a compiled sprite.
- get_compiled_sprite — Creates a compiled sprite using a bitmap as source.

Compiled sprites are stored as actual machine code instructions that draw a specific image onto a bitmap, using mov instructions with immediate data values. This is the fastest way to draw a masked image: on slow machines, up to and including a 486, drawing compiled sprites can be about to five times as fast as using draw_sprite() with a regular bitmap. On newer machines the difference is usually negligible.

Compiled sprites are big, so if memory is tight you should use RLE sprites instead, and what you can do with them is even more restricted than with RLE sprites, because they don't support clipping. If you try to draw one off the edge of a bitmap, you will corrupt memory and probably crash the system. You can convert bitmaps into compiled sprites at runtime, or you can create compiled sprite structures in grabber datafiles by making a new object of type 'Compiled sprite' or 'Compiled x-sprite'.

COMPILED_SPRITE *get_compiled_sprite(BITMAP *bitmap, int planar);
Creates a compiled sprite based on the specified bitmap (which must be a memory bitmap). Compiled sprites are device-dependent, so you have to specify whether to compile it into a linear or planar format. Pass FALSE as the second parameter if you are going to be drawing it onto memory bitmaps or mode 13h and SVGA screen bitmaps, and pass TRUE if you are going to draw it onto mode-X or Xtended mode screen bitmaps. Example:

>           COMPILED_SPRITE *cspr;
>           BITMAP *bmp;
>           ...
>           /* Create compiled sprite from an existent bitmap. */
>           cspr = get_compiled_sprite(bmp, 0);
>           if (!cspr)
>              abort_on_error("Couldn't create compiled sprite!");

>           /* We don't need the bitmap any more.*/
>           destroy_bitmap(bmp);

>           /* Use the compiled sprite. */
>           ...
>           /* Destroy it when we don't need it any more. */
>           destroy_compiled_sprite(cspr);

Returns a pointer to the created compiled sprite, or NULL if the compiled sprite could not be created. Remember to free this compiled sprite later to avoid memory leaks.

> See also: draw_compiled_sprite, destroy_compiled_sprite.

void destroy_compiled_sprite(COMPILED_SPRITE *sprite);
Destroys a compiled sprite structure previously returned by get_compiled_sprite(). If you pass a NULL pointer this function won't do anything. Use this once you are done with a compiled sprite to avoid memory leaks in your program.

> See also: get_compiled_sprite.

void draw_compiled_sprite(BITMAP *bmp, const COMPILED_SPRITE *sprite, int x, int y);
Draws a compiled sprite onto a bitmap at the specified position. The sprite must have been compiled for the correct type of bitmap (linear or planar). This function does not support clipping.

Hint: if not being able to clip compiled sprites is a problem, a neat trick is to set up a work surface (memory bitmap, mode-X virtual screen, or whatever) a bit bigger than you really need, and use the middle of it as your screen. That way you can draw slightly off the edge without any trouble...

> See also: get_compiled_sprite, draw_sprite, draw_rle_sprite, bitmap_mask_color.

Back to contents