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