sprite

struct sprite

struct sprite

Sprites use images sliced into frames to play short animations. Sprites can be used for game characters, props, tiles and titles. To create a sprite, you will need to create an image containing a set of fixed-sized frames and then pass it to create_sprite():

struct sprite* wizard = NULL;
wizard = create_sprite(create_image("wizard.png", screen), 32, 32);

Note that by creating both the sprite and the image, you are responsible to the clean-up once you’re done:

destroy_image(wizard->image);
destroy_sprite(wizard);

To animate a sprite, you need to create animation instances, indicating the frames to play. To actually play an animation, use play_animation():

play_animation(wizard, walk);

Finally, to update the sprite animation, call animate_sprite():

void* data;
data = animate_sprite(wizard, elapsed_ms);

Once a frame that has a user-data associated with it is played, you will get the data pointer back.

create_sprite

struct sprite *create_sprite(struct image *image, int w, int h)

allocate and initialize a new sprite

Return
valid sprite pointer or NULL on error
Parameters
  • image -

    sprite image

  • w -

    sprite frame width

  • h -

    sprite frame height

destroy_sprite

void destroy_sprite(struct sprite *sprite)

destroy an allocated sprite

Parameters

prepare_sprite

int prepare_sprite(struct sprite *sprite, struct image *image, int frame_width, int frame_height)

Build a sprite from a sprite image file containing frame tiles.

Return
-1 on error
Parameters
  • sprite -

    sprite resource to generate

  • image -

    image containing the sprite frames.

  • frame_width -

    width of each frame tile

  • frame_height -

    height of each frame tile

draw_sprite

int draw_sprite(struct sprite *sprite, int x, int y)

Draw the active frame of the sprite. If no sprite animation is active, the first frame will be drawn. If an animation is active, the animation state will determine the frame to draw.

Return
rendered sprite frame index
Parameters
  • sprite -

    sprite to use

  • x -

    x coordinates

  • y -

    y coordinates

draw_sprite_frame

void draw_sprite_frame(struct sprite *sprite, int x, int y, int frame)

Draw the specified frame of the sprite. This function can be used to override any animation or just use a sprite for its sprite sheet.

Parameters
  • sprite -

    sprite to use

  • x -

    x coordinates

  • y -

    y coordinates

  • frame -

    frame index to draw from the sprite sheet

animate_sprite

void *animate_sprite(struct sprite *sprite, uint32_t elapsed_ms)

Update the sprite animation state

Parameters
  • sprite -

    sprite to animate

  • elapsed_ms -

    time since last rendered frame (usually elapsed_ms)

play_animation

void play_animation(struct sprite *sprite, struct animation *animation)

Play a any binded sprite animation

Note
If a previous animation keyframe is not through playing, the requested animation will be defered until the previous keyframe is completed.
Parameters
  • sprite -

    /ref sprite to use the animation for.

  • animation -

    /ref animation to play.

stop_animation

void stop_animation(struct sprite *sprite)

Stop any playing animation Use this function to stop any currently playing or staged animation. This will clear any running animation and so any new animation activated using /ref play_animation will start immediately.

Parameters
  • sprite -

    /ref sprite to work on.