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