You are following a tutorial that teach you the animation with LIBDRAGON. You'll learn how to create an animator and use it.
With LIBDRAGON, load a spritesheet is really easy.
typedef struct data {
dg_spritesheet_t *ss;
} data_t;
void *dg_init(dg_window_t *w)
{
data_t *data = malloc(sizeof(data_t));
data->ss = dg_spritesheet_create("res/bat.png", 32, 32);
return data;
}
Here are the parameters :
int dg_loop(dg_window_t *w, void *data, sfTime dt)
{
data_t *d = (data_t *)(data);
sfSprite *sprite = sfSprite_create();
sfRenderWindow_clear(w->window, sfBlack);
dg_spritesheet_to_sprite(d->ss, sprite, 0);
sfRenderWindow_drawSprite(w->window, sprite, NULL);
sfSprite_destroy(sprite);
return 0;
}
How does it works :
void dg_end(void *data, int id)
{
data_t *d = (data_t *)(data);
dg_spritesheet_free(d->ss);
free(data);
}
Now let's combine the tiles to make an animation.
First, you'll need to create the animation.
typedef struct data {
dg_spritesheet_t *ss;
dg_animation_t *animation;
} data_t;
void *dg_init(dg_window_t *w)
{
data_t *data = malloc(sizeof(data_t));
data->ss = dg_spritesheet_create("res/bat.png", 32, 32);
data->animation = dg_animation_create(data->ss, 10);
dg_animation_add(data->animation, 1);
dg_animation_add(data->animation, 2);
dg_animation_add(data->animation, 3);
return data;
}
parameters of dg_animation_create:
typedef struct data {
dg_spritesheet_t *ss;
dg_animation_t *animation;
float i;
} data_t;
void *dg_init(dg_window_t *w)
{
data_t *data = malloc(sizeof(data_t));
data->ss = dg_spritesheet_create("res/bat.png", 32, 32);
data->animation = dg_animation_create(data->ss, 10);
dg_animation_add(data->animation, 1);
dg_animation_add(data->animation, 2);
dg_animation_add(data->animation, 3);
data->i = 0;
return data;
}
Now, let's animate !
int dg_loop(dg_window_t *w, void *data, sfTime dt)
{
data_t *d = (data_t *)(data);
sfSprite *sprite = sfSprite_create();
sfRenderWindow_clear(w->window, sfBlack);
dg_animation_update_sprite(d->animation, sprite, ((int)(d->i)));
sfRenderWindow_drawSprite(w->window, sprite, NULL);
d->i += dt.microseconds / 1000000.0 * d->animation->speed;
sfSprite_destroy(sprite);
return 0;
}
parameters of dg_animation_update_sprite:
Let's add an animation to use the animator.
typedef struct data {
dg_spritesheet_t *ss;
dg_animation_t *animation;
dg_animation_t *animation2;
float i;
} data_t;
void *dg_init(dg_window_t *w)
{
data_t *data = malloc(sizeof(data_t));
data->ss = dg_spritesheet_create("res/bat.png", 32, 32);
data->animation = dg_animation_create(data->ss, 10);
data->animation2 = dg_animation_create(data->ss, 10);
dg_animation_add(data->animation, 1);
dg_animation_add(data->animation, 2);
dg_animation_add(data->animation, 3);
dg_animation_add(data->animation, 5);
dg_animation_add(data->animation, 6);
dg_animation_add(data->animation, 7);
data->i = 0;
return data;
}
Now, let's manage them with an animator :
typedef struct data {
dg_spritesheet_t *ss;
dg_animation_t *animation;
dg_animation_t *animation2;
dg_animator_t *animator;
float i;
} data_t;
void *dg_init(dg_window_t *w)
{
data_t *data = malloc(sizeof(data_t));
data->ss = dg_spritesheet_create("res/bat.png", 32, 32);
data->animation = dg_animation_create(data->ss, 10);
data->animation2 = dg_animation_create(data->ss, 10);
data->animator = dg_animator_create();
dg_animation_add(data->animation, 1);
dg_animation_add(data->animation, 2);
dg_animation_add(data->animation, 3);
dg_animation_add(data->animation, 5);
dg_animation_add(data->animation, 6);
dg_animation_add(data->animation, 7);
dg_animator_add(data->animator, "first", data->animation);
dg_animator_add(data->animator, "second", data->animation2);
data->i = 0;
return data;
}
parameters of dg_animation_add:
int dg_loop(dg_window_t *w, void *data, sfTime dt)
{
data_t *d = (data_t *)(data);
sfSprite *sprite = sfSprite_create();
sfRenderWindow_clear(w->window, sfBlack);
dg_animator_update_sprite(d->animator, sprite, dt.microseconds);
sfRenderWindow_drawSprite(w->window, sprite, NULL);
d->i += dt.microseconds / 100000.0;
if (d->i > 50)
dg_animator_set_animation(d->animator, "second");
sfSprite_destroy(sprite);
return 0;
}
parameters of dg_animator_update_sprite:
#include
#include "libdragon.h"
typedef struct data {
dg_spritesheet_t *ss;
dg_animation_t *animation;
dg_animation_t *animation2;
dg_animator_t *animator;
float i;
} data_t;
void *dg_init(dg_window_t *w)
{
data_t *data = malloc(sizeof(data_t));
data->ss = dg_spritesheet_create("res/bat.png", 32, 32);
data->animation = dg_animation_create(data->ss, 10);
data->animation2 = dg_animation_create(data->ss, 10);
data->animator = dg_animator_create();
dg_animation_add(data->animation, 1);
dg_animation_add(data->animation, 2);
dg_animation_add(data->animation, 3);
dg_animation_add(data->animation2, 5);
dg_animation_add(data->animation2, 6);
dg_animation_add(data->animation2, 7);
dg_animator_add(data->animator, "first", data->animation);
dg_animator_add(data->animator, "second", data->animation2);
data->i = 0;
return data;
}
int dg_loop(dg_window_t *w, void *data, sfTime dt)
{
data_t *d = (data_t *)(data);
sfSprite *sprite = sfSprite_create();
sfRenderWindow_clear(w->window, sfBlack);
dg_animator_update_sprite(d->animator, sprite, dt.microseconds);
sfRenderWindow_drawSprite(w->window, sprite, NULL);
d->i += dt.microseconds / 100000.0;
if (d->i > 50)
dg_animator_set_animation(d->animator, "second");
sfSprite_destroy(sprite);
return 0;
}
void dg_end(void *data, int id)
{
data_t *d = (data_t *)(data);
dg_spritesheet_free(d->ss);
dg_animator_free(d->animator);
free(data);
}
int main(int argc, char **argv)
{
dg_play(1920, 1080, "LibDragon", 200);
return 0;
}