|
| 1 | +#include "animation_controller.h" |
| 2 | +#include "animation.h" |
| 3 | +#include "dimension.h" |
| 4 | +#include "linkedlist.h" |
| 5 | +#include "sprite.h" |
| 6 | +#include "texturecache.h" |
| 7 | +#include "util.h" |
| 8 | +#include <stdlib.h> |
| 9 | + |
| 10 | +struct { |
| 11 | + LinkedList *animations; |
| 12 | +} s_controller; |
| 13 | + |
| 14 | +static void |
| 15 | +create_explosion(Position pos) |
| 16 | +{ |
| 17 | + Animation *a = animation_create(8); |
| 18 | + linkedlist_append(&s_controller.animations, a); |
| 19 | + |
| 20 | + Texture *t = texturecache_add("Extras/Explosion.png"); |
| 21 | + sprite_set_texture(a->sprite, t, 0); |
| 22 | + a->sprite->pos.x = pos.x - 16; |
| 23 | + a->sprite->pos.y = pos.y - 16; |
| 24 | + a->sprite->dim = (Dimension) { 64, 64 }; |
| 25 | + a->sprite->clip = (SDL_Rect) { 0, 0, 32, 32 }; |
| 26 | + a->sprite->rotationPoint = (SDL_Point) { 32, 32 }; |
| 27 | + |
| 28 | + a->loop = false; |
| 29 | + a->running = true; |
| 30 | + animation_set_frames(a, (AnimationClip[]) { |
| 31 | + { 0, 0, 32, 32, 100 }, |
| 32 | + { 32, 0, 32, 32, 100 }, |
| 33 | + { 64, 0, 32, 32, 100 }, |
| 34 | + { 96, 0, 32, 32, 100 }, |
| 35 | + { 128, 0, 32, 32, 100 }, |
| 36 | + { 160, 0, 32, 32, 100 }, |
| 37 | + { 192, 0, 32, 32, 300 }, |
| 38 | + { 224, 0, 32, 32, 300 } |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +void |
| 43 | +animation_controller_create(enum AnimationType type, Position pos) |
| 44 | +{ |
| 45 | + switch (type) { |
| 46 | + case EXPLOSION_ANIMATION: |
| 47 | + create_explosion(pos); |
| 48 | + break; |
| 49 | + default: |
| 50 | + fatal("Invalid animation type: %d", type); |
| 51 | + break; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void |
| 56 | +animation_controller_init(void) |
| 57 | +{ |
| 58 | + s_controller.animations = NULL; |
| 59 | +} |
| 60 | + |
| 61 | +void |
| 62 | +animation_controller_update(void) |
| 63 | +{ |
| 64 | + LinkedList *node = s_controller.animations; |
| 65 | + LinkedList *prev = NULL; |
| 66 | + while (node != NULL) { |
| 67 | + Animation *a = node->data; |
| 68 | + if (!a->running) { |
| 69 | + if (prev) { |
| 70 | + prev->next = node->next; |
| 71 | + } else { |
| 72 | + s_controller.animations = node->next; |
| 73 | + } |
| 74 | + LinkedList *tmp = node; |
| 75 | + node = node->next; |
| 76 | + animation_destroy(tmp->data); |
| 77 | + free(tmp); |
| 78 | + continue; |
| 79 | + } |
| 80 | + animation_update(a); |
| 81 | + prev = node; |
| 82 | + node = node->next; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void |
| 87 | +animation_controller_render(Camera *cam) |
| 88 | +{ |
| 89 | + LinkedList *node = s_controller.animations; |
| 90 | + while (node) { |
| 91 | + animation_render(node->data, cam); |
| 92 | + node = node->next; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void |
| 97 | +animation_controller_close(void) |
| 98 | +{ |
| 99 | + while (s_controller.animations) { |
| 100 | + animation_destroy(linkedlist_pop(&s_controller.animations)); |
| 101 | + } |
| 102 | +} |
0 commit comments