]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_sprit.c
palllist not being worked on right now... i will work on sprite animation VERY soon
[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         // Draw sprite\r
108         i = get_vrl_by_id(spri->spritesheet, spri->curr_spri_id, spri->sprite_vrl_cont);\r
109         if(i < 0)\r
110         {\r
111                 Quit (gv, "Error retriving required sprite");\r
112         }\r
113 \r
114         // replace VGA stride with our own and mem ptr. then sprite rendering at this stage is just (0,0)\r
115         vga_state.vga_draw_stride_limit = (gv->video.page[0].width + 3 - spri->x) >> 2;\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
152 */\r
153 void animate_spri(struct sprite *spri, global_game_variables_t *gv)\r
154 {\r
155 #define VMEMPAGESIZE2   gv->video.page[0].pagesize+gv->video.page[1].pagesize\r
156 #define VMEMPAGEDATA2   gv->video.page[2].data\r
157         unsigned int i,o,o2; int j;\r
158         int x,y,rx,ry,w,h;\r
159         int overdraw = 1;       // how many pixels to "overdraw" so that moving sprites with edge pixels don't leave streaks.\r
160                                                 // if the sprite's edge pixels are clear anyway, you can set this to 0.\r
161         VGA_RAM_PTR omemptr;\r
162 \r
163         // Events go here\r
164 \r
165 \r
166         omemptr = vga_state.vga_graphics_ram; // save original mem ptr\r
167         x=spri->x-4;\r
168         y=spri->y;\r
169 \r
170         // Draw sprite\r
171         j = get_vrl_by_id(spri->spritesheet, spri->curr_spri_id, spri->sprite_vrl_cont);\r
172         if(j < 0)\r
173         {\r
174                 Quit (gv, "Error retriving required sprite");\r
175         }\r
176 \r
177         // render box bounds. y does not need modification, but x and width must be multiple of 4\r
178         if (x >= overdraw) rx = (x - overdraw) & (~3);\r
179                 else rx = -(gv->video.page[0].dx);\r
180         if (y >= overdraw) ry = (y - overdraw);\r
181                 else ry = -(gv->video.page[0].dy);\r
182         h = spri->sprite_vrl_cont->vrl_header->height + overdraw + y - ry;\r
183         w = (x + spri->sprite_vrl_cont->vrl_header->width + (overdraw*2) + 3 - rx) & (~3);//round up\r
184         if ((rx+w) > gv->video.page[0].width) w = gv->video.page[0].width-rx;\r
185         if ((ry+h) > gv->video.page[0].height) h = gv->video.page[0].height-ry;\r
186 \r
187         // block copy pattern to where we will draw the sprite\r
188         vga_setup_wm1_block_copy();\r
189         o2 = VMEMPAGESIZE2;\r
190         o = (0x10000UL - (uint16_t)VMEMPAGEDATA2) + (ry * gv->video.page[0].stridew) + (rx >> 2); // source offscreen\r
191         for (i=0;i < h;i++,o += gv->video.page[0].stridew,o2 += (w >> 2)) vga_wm1_mem_block_copy(o2,o,w >> 2);\r
192 \r
193         // must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally\r
194         vga_restore_rm0wm0();\r
195 \r
196         // replace VGA stride with our own and mem ptr. then sprite rendering at this stage is just (0,0)\r
197         vga_state.vga_draw_stride_limit = (gv->video.page[0].width + 3 - x) >> 2;//round up\r
198         vga_state.vga_draw_stride = w >> 2;\r
199         vga_state.vga_graphics_ram = omemptr + VMEMPAGESIZE2;\r
200 \r
201         // then the sprite. note modding ram ptr means we just draw to (x&3,0)\r
202         draw_vrl1_vgax_modex(\r
203                 x-rx,\r
204                 y-ry,\r
205                 spri->sprite_vrl_cont->vrl_header,\r
206                 spri->sprite_vrl_cont->line_offsets,\r
207                 spri->sprite_vrl_cont->buffer + sizeof(struct vrl1_vgax_header),\r
208                 spri->sprite_vrl_cont->data_size\r
209         );\r
210 \r
211         // restore ptr\r
212         vga_state.vga_graphics_ram = omemptr;\r
213 \r
214         // block copy to visible RAM from offscreen\r
215         vga_setup_wm1_block_copy();\r
216         o = VMEMPAGESIZE2; // source offscreen\r
217         o2 = (ry * gv->video.page[0].stridew) + (rx >> 2); // dest visible (original stride)\r
218         for (i=0;i < h;i++,o += vga_state.vga_draw_stride,o2 += gv->video.page[0].stridew) vga_wm1_mem_block_copy(o2,o,w >> 2);\r
219         // must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally\r
220         vga_restore_rm0wm0();\r
221 \r
222         // restore stride\r
223         vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = gv->video.page[0].stridew;\r
224 \r
225         // Depending on delay, update indices\r
226         switch(spri->delay){\r
227                 // Delay = 0 means that sprite should loop. Nothing to change here\r
228                 case 0:\r
229                         break;\r
230 \r
231                 // Delay = 1 means that on next time unit sprite should be changed\r
232                 case 1:\r
233                         spri->curr_anim_spri++;\r
234 \r
235                         // If we hit the end of an animation sequence, restart it\r
236                         if(!(spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id)){\r
237                                 spri->curr_anim_spri = 0;\r
238                                 spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id;\r
239                         }\r
240                         spri->delay = spri->curr_anim_list[spri->curr_anim_spri].delay;\r
241 \r
242                 // Delay > 1 means that we should not change sprite yet. Decrease delay\r
243                 default:\r
244                         spri->delay--;\r
245                         break;\r
246         }\r
247 }\r