]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_spri.c
__seguse.txt added to show _seg usage also OpenVGMFile needs to be ported to 16_snd...
[16.git] / src / lib / 16_spri.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2017 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_spri.h"\r
24 \r
25 char* get_curr_anim_name(struct sprite *spri)\r
26 {\r
27         // Retrive animation name list\r
28 #ifndef VRSVRLNONPTR\r
29         struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;\r
30 #else\r
31         struct vrs_header far *vrs = spri->spritesheet.vrs_hdr;\r
32 #endif\r
33         uint32_t far *anim_names_offsets =      (uint32_t far *)\r
34                                                 ((byte far *)vrs +\r
35                                                  vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_NAME_LIST]);\r
36 \r
37         return (char *)(vrs + anim_names_offsets[spri->curr_anim]);\r
38 }\r
39 \r
40 void init_anim(struct sprite *spri, int anim_index)\r
41 {\r
42 #ifndef VRSVRLNONPTR\r
43         struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;\r
44 #else\r
45         struct vrs_header far *vrs = spri->spritesheet.vrs_hdr;\r
46 #endif\r
47         uint32_t far *anim_lists_offsets =      (uint32_t far *)\r
48                                                 ((byte far *)vrs +\r
49                                                  vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_LIST]);\r
50         struct vrs_animation_list_entry_t far *anim_list =      (struct vrs_animation_list_entry_t far *)\r
51                                                                 ((byte far *)vrs +\r
52                                                                  anim_lists_offsets[anim_index]);\r
53 \r
54         // Upon new animation, start from the first sprite in it\r
55         spri->curr_anim = anim_index;\r
56         spri->curr_anim_spri = 0;\r
57         spri->curr_spri_id = anim_list[0].sprite_id;\r
58         spri->delay = anim_list[0].delay;\r
59 \r
60         spri->curr_anim_list = anim_list;\r
61 }\r
62 \r
63 int set_anim_by_id(struct sprite *spri, int anim_id)\r
64 {\r
65         int new_anim_index = 0;\r
66         int iter_id;\r
67 #ifndef VRSVRLNONPTR\r
68         struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;\r
69 #else\r
70         struct vrs_header far *vrs = spri->spritesheet.vrs_hdr;\r
71 #endif\r
72         // Retruve animation ids list\r
73         uint16_t far *anim_ids =        (uint16_t far *)\r
74                                         ((byte far *)vrs +\r
75                                          vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);\r
76 \r
77         // Loop through animation id untill match or end of list\r
78         while(iter_id = anim_ids[new_anim_index])\r
79         {\r
80                 // Return on successful match\r
81                 if (iter_id == anim_id)\r
82                 {\r
83                         init_anim(spri, new_anim_index);\r
84                         return 0;\r
85                 }\r
86                 new_anim_index++;\r
87         }\r
88         return -1;\r
89 }\r
90 \r
91 void print_anim_ids(struct sprite *spri)\r
92 {\r
93         int new_anim_index = 0;\r
94         int iter_id;\r
95 #ifndef VRSVRLNONPTR\r
96         struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;\r
97 #else\r
98         struct vrs_header far *vrs = spri->spritesheet.vrs_hdr;\r
99 #endif\r
100         // Retruve animation ids list\r
101         uint16_t far *anim_ids =        (uint16_t far *)\r
102                                         ((byte far *)vrs +\r
103                                          vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);\r
104 \r
105         if(!anim_ids[new_anim_index])\r
106                 return;\r
107         // Loop through animation id untill match or end of list\r
108         while(iter_id = anim_ids[new_anim_index])\r
109         {\r
110                 printf("s%d ", iter_id);\r
111                 new_anim_index++;\r
112         }\r
113 }\r
114 \r
115 void animate_spri(entity_t *enti, video_t *video)\r
116 {\r
117 #define INC_PER_FRAME if(enti->q&1) enti->persist_aniframe++; if(enti->persist_aniframe>4) enti->persist_aniframe = 1;\r
118         unsigned int i,o,o2; int j;\r
119         int x,y,rx,ry,w,h;\r
120         VGA_RAM_PTR omemptr = (VGA_RAM_PTR)video->page[0].data;// save original mem ptr\r
121 \r
122         x=enti->spri.x;\r
123         y=enti->spri.y;\r
124         VL_Initofs(video);\r
125 \r
126         // Depending on delay, update indices\r
127 //#define FRAME1 modexDrawSpriteRegion(pip[(pip->video->p)].page, x, y, 48, player[pn].enti.dire, 24, 32,       PLAYERBMPDATAPTR);\r
128 //#define FRAME2 modexDrawSpriteRegion(pip[(pip->video->p)].page, x, y, 24, player[pn].enti.dire, 24, 32,       PLAYERBMPDATAPTR); stand\r
129 //#define FRAME3 modexDrawSpriteRegion(pip[(pip->video->p)].page, x, y, 0, player[pn].enti.dire, 24, 32,        PLAYERBMPDATAPTR);\r
130 //#define FRAME4 modexDrawSpriteRegion(pip[(pip->video->p)].page, x, y, 24, player[pn].enti.dire, 24, 32,       PLAYERBMPDATAPTR); stand\r
131 \r
132         // copy active display (0) to offscreen buffer (0x4000)\r
133         /*vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = vga_state.vga_stride;\r
134         vga_setup_wm1_block_copy();\r
135         vga_wm1_mem_block_copy(copy_ofs,        display_ofs,    vga_state.vga_stride * vga_state.vga_height);\r
136         vga_restore_rm0wm0();*/\r
137 \r
138         switch(enti->spri.delay)\r
139         {\r
140                 // Delay = 0 means that sprite should loop. Nothing to change here\r
141                 case 0:\r
142                 break;\r
143 \r
144                 // Delay = 1 means that on next time unit sprite should be changed\r
145                 case 1:\r
146                         INC_PER_FRAME\r
147                         enti->spri.curr_anim_spri++;\r
148 \r
149                         // If we hit the end of an animation sequence, restart it\r
150                         if(!(   enti->spri.curr_spri_id = enti->spri.curr_anim_list[enti->spri.curr_anim_spri].sprite_id)){\r
151                                 enti->spri.curr_anim_spri = 0;\r
152                                 enti->spri.curr_spri_id = enti->spri.curr_anim_list[enti->spri.curr_anim_spri].sprite_id;\r
153                         }\r
154                         enti->spri.delay = enti->spri.curr_anim_list[enti->spri.curr_anim_spri].delay;\r
155 \r
156                 // Delay > 1 means that we should not change sprite yet. Decrease delay\r
157                 default:\r
158                         enti->spri.delay--;\r
159                 break;\r
160         }\r
161 \r
162         // Events go here\r
163 \r
164 \r
165 \r
166         // Draw sprite\r
167 #ifndef VRSVRLNONPTR\r
168         j = get_vrl_by_id(enti->spri.spritesheet, enti->spri.curr_spri_id, enti->spri.sprite_vrl_cont);\r
169 #else\r
170         j = get_vrl_by_id(&enti->spri.spritesheet, enti->spri.curr_spri_id, &enti->spri.sprite_vrl_cont);\r
171 #endif\r
172         if(j < 0)\r
173         {\r
174                 //Quit (gv, "Error retriving required sprite");\r
175                 return;\r
176         }\r
177 \r
178         // render box bounds. y does not need modification, but x and width must be multiple of 4\r
179         if(!video->rss)\r
180         {\r
181                 if (x >= enti->overdraww) rx = (x - enti->overdraww) & (~3);\r
182                         else rx = -(video->page[0].dx);\r
183                 if (y >= enti->overdrawh) ry = (y - enti->overdrawh);\r
184                         else ry = -(video->page[0].dy);\r
185 #ifndef VRSVRLNONPTR\r
186                 h = enti->spri.sprite_vrl_cont->vrl_header->height + enti->overdrawh + y - ry;\r
187                 w = (x + enti->spri.sprite_vrl_cont->vrl_header->width + (enti->overdraww*2) + 3 - rx) & (~3) - enti->overdraww;//round up\r
188 #else\r
189                 h = enti->spri.sprite_vrl_cont.vrl_header->height + enti->overdrawh + y - ry;\r
190                 w = (x + enti->spri.sprite_vrl_cont.vrl_header->width + (enti->overdraww*2) + 3 - rx) & (~3) - enti->overdraww;//round up\r
191 #endif\r
192                 if ((rx+w) > video->page[0].width) w = video->page[0].width-rx;\r
193                 if ((ry+h) > video->page[0].height) h = video->page[0].height-ry;\r
194 \r
195                 if(!video->bgps){\r
196                         // block copy pattern to where we will draw the sprite\r
197                         vga_setup_wm1_block_copy();\r
198                         o2 = video->ofs.offscreen_ofs;\r
199                         o = video->ofs.pattern_ofs + (ry * video->page[0].stridew) + (rx >> 2); // source offscreen\r
200                         for (i=0;i < h;i++,o += video->page[0].stridew,o2 += (w >> 2)) vga_wm1_mem_block_copy(o2,o,w >> 2);\r
201 \r
202                         // must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally\r
203                         vga_restore_rm0wm0();\r
204                 }\r
205                 // replace VGA stride with our own and mem ptr. then sprite rendering at this stage is just (0,0)\r
206                 vga_state.vga_draw_stride = w >> 2;\r
207                 vga_state.vga_graphics_ram = omemptr + video->ofs.offscreen_ofs;\r
208         }else{ rx=ry=w=h=0; vga_state.vga_graphics_ram = (VGA_RAM_PTR)video->page[0].data; }\r
209         vga_state.vga_draw_stride_limit = (video->page[0].width + 3 - x) >> 2;//round up\r
210 \r
211         // then the sprite. note modding ram ptr means we just draw to (x&3,0)\r
212 #ifndef SPRITE\r
213         modexClearRegion(&video->page[0], x, y, 16, 32, 1);\r
214 #else\r
215         draw_vrl1_vgax_modex(\r
216                 x-rx,\r
217                 y-ry,\r
218 #ifndef VRSVRLNONPTR\r
219                 enti->spri.sprite_vrl_cont->vrl_header,\r
220                 enti->spri.sprite_vrl_cont->line_offsets,\r
221                 enti->spri.sprite_vrl_cont->buffer + sizeof(struct vrl1_vgax_header),\r
222                 enti->spri.sprite_vrl_cont->data_size\r
223 #else\r
224                 enti->spri.sprite_vrl_cont.vrl_header,\r
225                 enti->spri.sprite_vrl_cont.line_offsets,\r
226                 enti->spri.sprite_vrl_cont.buffer + sizeof(struct vrl1_vgax_header),\r
227                 enti->spri.sprite_vrl_cont.data_size\r
228 #endif\r
229         );\r
230 #endif\r
231         if(!video->rss)\r
232         {\r
233                 // restore ptr\r
234                 vga_state.vga_graphics_ram = omemptr;\r
235 \r
236                 if(!video->bgps){\r
237                         // block copy to visible RAM from offscreen\r
238                         vga_setup_wm1_block_copy();\r
239                         o = video->ofs.offscreen_ofs; // source offscreen\r
240                         o2 = (ry * video->page[0].stridew) + (rx >> 2); // dest visible (original stride)\r
241                         for (i=0;i < h;i++,o += vga_state.vga_draw_stride,o2 += video->page[0].stridew) vga_wm1_mem_block_copy(o2,o,w >> 2);\r
242                         // must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally\r
243                         vga_restore_rm0wm0();\r
244                 }\r
245         }\r
246         // restore stride\r
247         vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = video->page[0].stridew;\r
248 \r
249         vga_state.vga_graphics_ram = video->omemptr;\r
250         video->dorender=1;\r
251 }\r