]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_sprit.c
wwww
[16.git] / src / lib / 16_sprit.c
1 /* Project 16 Source Code~
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
3  *
4  * This file is part of Project 16.
5  *
6  * Project 16 is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Project 16 is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,
19  * Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22 #include "src/lib/16_sprit.h"
23
24 char* get_curr_anim_name(struct sprite *spri)
25 {
26         // Retrive animation name list
27         struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr;
28         uint32_t huge *anim_names_offsets =     (uint32_t huge *)
29                                                 ((byte huge *)vrs +
30                                                  vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_NAME_LIST]);
31
32         return (char *)(vrs + anim_names_offsets[spri->curr_anim]);
33 }
34
35 void init_anim(struct sprite *spri, int anim_index)
36 {
37         struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr;
38         uint32_t huge *anim_lists_offsets =     (uint32_t huge *)
39                                                 ((byte huge *)vrs +
40                                                  vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_LIST]);
41         struct vrs_animation_list_entry_t *anim_list =  (struct vrs_animation_list_entry_t huge *)
42                                                         ((byte huge *)vrs +
43                                                          anim_lists_offsets[anim_index]);
44
45         // Upon new animation, start from the first sprite in it
46         spri->curr_anim = anim_index;
47         spri->curr_anim_spri = 0;
48         spri->curr_spri_id = anim_list[0].sprite_id;
49         spri->delay = anim_list[0].delay;
50
51         spri->curr_anim_list = anim_list;
52 }
53
54
55 int set_anim_by_id(struct sprite *spri, int anim_id)
56 {
57         int new_anim_index = 0;
58         int iter_id;
59         struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr;
60         // Retruve animation ids list
61         uint16_t huge *anim_ids =       (uint16_t huge *)
62                                         ((byte huge *)vrs +
63                                          vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);
64
65         // Loop through animation id untill match or end of list
66         while(iter_id = anim_ids[new_anim_index])
67         {
68                 // Return on successful match
69                 if (iter_id == anim_id)
70                 {
71                         init_anim(spri, new_anim_index);
72                         return 0;
73                 }
74                 new_anim_index++;
75         }
76         return -1;
77 }
78
79 void print_anim_ids(struct sprite *spri)
80 {
81         int new_anim_index = 0;
82         int iter_id;
83         struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr;
84         // Retruve animation ids list
85         uint16_t huge *anim_ids =       (uint16_t huge *)
86                                         ((byte huge *)vrs +
87                                          vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);
88
89         if(!anim_ids[new_anim_index])
90                 exit(3);
91         // Loop through animation id untill match or end of list
92         while(iter_id = anim_ids[new_anim_index])
93         {
94                 // Return on successful match
95                 new_anim_index++;
96                 printf("%d, ", iter_id);
97         }
98 }
99
100
101 void animate_spri(struct sprite *spri)
102 {
103         // Events go here
104
105         // Draw sprite
106         get_vrl_by_id(spri->spritesheet, spri->curr_spri_id, spri->sprite_vrl_cont);
107         draw_vrl1_vgax_modex(   spri->x, spri->y,
108                                 spri->sprite_vrl_cont->vrl_header, spri->sprite_vrl_cont->line_offsets,
109                                 spri->sprite_vrl_cont->buffer + sizeof(struct vrl1_vgax_header),
110                                 spri->sprite_vrl_cont->data_size);
111
112         // Depending on delay, update indices
113         switch(spri->delay){
114                 // Delay = 0 means that sprite should loop. Nothing to change here
115                 case 0:
116                         break;
117
118                 // Delay = 1 means that on next time unit sprite should be changed
119                 case 1:
120                         spri->curr_anim_spri++;
121
122                         // If we hit the end of an animation sequence, restart it
123                         if(!(spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id)){
124                                 spri->curr_anim_spri = 0;
125                                 spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id;
126                         }
127                         spri->delay = spri->curr_anim_list[spri->curr_anim_spri].delay;
128
129                 // Delay > 1 means that we should not change sprite yet. Decrease delay
130                 default:
131                         spri->delay--;
132                         break;
133         }
134 }