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