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