]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_sprite.c
7667c25da8f482c6107a6312b8130935cd30d91c
[16.git] / src / lib / 16_sprite.c
1 #include "src/lib/16_sprite.h"
2
3 char* get_curr_anim_name(struct sprite *spri)
4 {
5         // Retrive animation name list
6         struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr;
7         uint32_t huge *anim_names_offsets =     (uint32_t huge *)
8                                                 ((byte huge *)vrs + 
9                                                  vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_NAME_LIST]);
10
11         return (char *)(vrs + anim_names_offsets[spri->curr_anim_spri]);
12 }
13
14 void init_anim(struct sprite *spri, int anim_index)
15 {
16         struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr;
17         uint32_t huge *anim_lists_offsets =     (uint32_t huge *)
18                                                 ((byte huge *)vrs + 
19                                                  vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_LIST]);
20         struct vrs_animation_list_entry_t *anim_list =  (struct vrs_animation_list_entry_t huge *)
21                                                         ((byte huge *)vrs + 
22                                                          anim_lists_offsets[anim_index]);
23
24         // Upon new animation, start from the first sprite in it
25         spri->curr_anim_spri = 0;
26         spri->curr_spri_id = anim_list[0].sprite_id;
27         spri->delay = anim_list[0].delay;
28
29         spri->curr_anim_list = anim_list;
30 }
31
32
33 int set_anim_by_id(struct sprite *spri, int anim_id)
34 {
35         int new_anim_index = 0;
36         int iter_id;
37         struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr;
38         // Retruve animation ids list
39         uint16_t huge *anim_ids =       (uint16_t huge *)
40                                         ((byte huge *)vrs + 
41                                          vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);
42
43         // Loop through animation id untill match or end of list
44         while(iter_id = anim_ids[new_anim_index])
45         {
46                 // Return on successful match
47                 if (iter_id = anim_id)
48                 {
49                         init_anim(spri, new_anim_index);
50                         return 0;
51                 }
52                 new_anim_index++;
53         }
54         return -1;
55 }
56
57 void print_anim_ids(struct sprite *spri)
58 {
59         int new_anim_index = 0;
60         int iter_id;
61         struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr;
62         // Retruve animation ids list
63         uint16_t huge *anim_ids =       (uint16_t huge *)
64                                         ((byte huge *)vrs + 
65                                          vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);
66
67         printf("\nPos %lld off %lld\n", (uint32_t)vrs, vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);
68         if(!anim_ids[new_anim_index])
69                 exit(3);
70         // Loop through animation id untill match or end of list
71         while(iter_id = anim_ids[new_anim_index])
72         {
73                 // Return on successful match
74                 new_anim_index++;
75                 printf("%d, ", iter_id);
76         }
77 }
78
79
80 void animate_spri(struct sprite *spri)
81 {
82         struct vrl_container *vrl_cont;
83         // Events go here
84         
85         // Draw sprite
86         vrl_cont = get_vrl_by_id(spri->spritesheet, spri->curr_spri_id);
87         draw_vrl1_vgax_modex(   spri->x, spri->y, 
88                                 vrl_cont->vrl_header, vrl_cont->line_offsets,   
89                                 vrl_cont->buffer + sizeof(struct vrl1_vgax_header), 
90                                 vrl_cont->size - sizeof(struct vrl1_vgax_header));
91
92         // Depending on delay, update indices
93         switch(spri->delay){
94                 // Delay = 0 means that sprite should loop. Nothing to change here
95                 case 0:
96                         break;
97
98                 // Delay = 1 means that on next time unit sprite should be changed
99                 case 1:
100                         spri->curr_anim_spri++;
101
102                         // If we hit the end of an animation sequence, restart it
103                         if(!(spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id)){
104                                 spri->curr_anim_spri = 0;
105                                 spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id;
106                         }
107                         spri->delay = spri->curr_anim_list[spri->curr_anim_spri].delay;
108
109                 // Delay > 1 means that we should not change sprite yet. Decrease delay
110                 default:
111                         spri->delay--;
112                         break;
113         }
114 }