Mercurial > molko
diff libcore/core/animation.c @ 121:789b23e01f52
misc: reorganize hierarchy, closes #2490
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 05 Oct 2020 13:25:06 +0200 |
parents | src/core/animation.c@52792b863ff7 |
children | 6691c9f69028 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libcore/core/animation.c Mon Oct 05 13:25:06 2020 +0200 @@ -0,0 +1,84 @@ +/* + * animation.c -- basic animations + * + * Copyright (c) 2020 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <assert.h> + +#include "animation.h" +#include "sprite.h" + +void +animation_init(struct animation *an, struct sprite *sprite, unsigned int delay) +{ + assert(an); + assert(sprite); + + an->sprite = sprite; + an->row = 0; + an->column = 0; + an->delay = delay; + an->elapsed = 0; +} + +bool +animation_is_complete(const struct animation *an) +{ + assert(an); + + return an->row == an->sprite->nrows && + an->column == an->sprite->ncols && + an->elapsed >= an->delay; +} + +void +animation_start(struct animation *an) +{ + assert(an); + + an->row = 0; + an->column = 0; + an->elapsed = 0; +} + +void +animation_update(struct animation *an, unsigned int ticks) +{ + assert(an); + + an->elapsed += ticks; + + if (an->elapsed < an->delay) + return; + + /* Increment column first */ + if (++an->column >= an->sprite->ncols) { + /* + * Increment row, if we reach the last row it means we are + * at the last frame. + */ + if (++an->row >= an->sprite->nrows) + an->row = an->sprite->nrows; + else + an->column = 0; + } +} + +void +animation_draw(struct animation *an, int x, int y) +{ + sprite_draw(an->sprite, an->row, an->column, x, y); +}