1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
\r
4 * This file is part of Project 16.
\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
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
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
23 #include "src/lib/16_sprit.h"
\r
25 char* get_curr_anim_name(struct sprite *spri)
\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
31 vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_NAME_LIST]);
\r
33 return (char *)(vrs + anim_names_offsets[spri->curr_anim]);
\r
36 void init_anim(struct sprite *spri, int anim_index)
\r
38 struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;
\r
39 uint32_t far *anim_lists_offsets = (uint32_t far *)
\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
44 anim_lists_offsets[anim_index]);
\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
52 spri->curr_anim_list = anim_list;
\r
56 int set_anim_by_id(struct sprite *spri, int anim_id)
\r
58 int new_anim_index = 0;
\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
64 vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);
\r
66 // Loop through animation id untill match or end of list
\r
67 while(iter_id = anim_ids[new_anim_index])
\r
69 // Return on successful match
\r
70 if (iter_id == anim_id)
\r
72 init_anim(spri, new_anim_index);
\r
80 void print_anim_ids(struct sprite *spri)
\r
82 int new_anim_index = 0;
\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
88 vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);
\r
90 if(!anim_ids[new_anim_index])
\r
92 // Loop through animation id untill match or end of list
\r
93 while(iter_id = anim_ids[new_anim_index])
\r
95 printf("s%d ", iter_id);
\r
101 void animate_spri(struct sprite *spri, global_game_variables_t *gv)
\r
108 i = get_vrl_by_id(spri->spritesheet, spri->curr_spri_id, spri->sprite_vrl_cont);
\r
111 Quit (gv, "Error retriving required sprite");
\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
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
127 vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = gv->video.page[0].stridew;
\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
135 // Delay = 1 means that on next time unit sprite should be changed
\r
137 spri->curr_anim_spri++;
\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
144 spri->delay = spri->curr_anim_list[spri->curr_anim_spri].delay;
\r
146 // Delay > 1 means that we should not change sprite yet. Decrease delay
\r
153 void animate_spri(struct sprite *spri, global_game_variables_t *gv)
\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
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
166 omemptr = vga_state.vga_graphics_ram; // save original mem ptr
\r
170 j = get_vrl_by_id(spri->spritesheet, spri->curr_spri_id, spri->sprite_vrl_cont);
\r
173 Quit (gv, "Error retriving required sprite");
\r
176 // render box bounds. y does not need modification, but x and width must be multiple of 4
\r
177 if (x >= overdraw) rx = (x - overdraw) & (~3);
\r
178 else rx = -(gv->video.page[0].dx);
\r
179 if (spri->y >= overdraw) ry = (spri->y - overdraw);
\r
180 else ry = -(gv->video.page[0].dy);
\r
181 h = spri->sprite_vrl_cont->vrl_header->height + overdraw + spri->y - ry;
\r
182 w = (x + spri->sprite_vrl_cont->vrl_header->width + (overdraw*2) + 3 - rx) & (~3);//round up
\r
183 if ((rx+w) > gv->video.page[0].width) w = gv->video.page[0].width-rx;
\r
184 if ((ry+h) > gv->video.page[0].height) h = gv->video.page[0].height-ry;
\r
186 // block copy pattern to where we will draw the sprite
\r
187 vga_setup_wm1_block_copy();
\r
188 o2 = VMEMPAGESIZE2;
\r
189 o = (0x10000UL - (uint16_t)VMEMPAGEDATA2) + (ry * gv->video.page[0].stridew) + (rx >> 2); // source offscreen
\r
190 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 // must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally
\r
193 vga_restore_rm0wm0();
\r
195 // replace VGA stride with our own and mem ptr. then sprite rendering at this stage is just (0,0)
\r
196 vga_state.vga_draw_stride_limit = (gv->video.page[0].width + 3 - x) >> 2;//round up
\r
197 vga_state.vga_draw_stride = w >> 2;
\r
198 vga_state.vga_graphics_ram = omemptr + VMEMPAGESIZE2;
\r
200 // then the sprite. note modding ram ptr means we just draw to (x&3,0)
\r
201 draw_vrl1_vgax_modex(
\r
204 spri->sprite_vrl_cont->vrl_header,
\r
205 spri->sprite_vrl_cont->line_offsets,
\r
206 spri->sprite_vrl_cont->buffer + sizeof(struct vrl1_vgax_header),
\r
207 spri->sprite_vrl_cont->data_size
\r
211 vga_state.vga_graphics_ram = omemptr;
\r
213 // block copy to visible RAM from offscreen
\r
214 vga_setup_wm1_block_copy();
\r
215 o = VMEMPAGESIZE2; // source offscreen
\r
216 o2 = (ry * gv->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 += gv->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
222 vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = gv->video.page[0].stridew;
\r
224 // Depending on delay, update indices
\r
225 switch(spri->delay){
\r
226 // Delay = 0 means that sprite should loop. Nothing to change here
\r
230 // Delay = 1 means that on next time unit sprite should be changed
\r
232 spri->curr_anim_spri++;
\r
234 // If we hit the end of an animation sequence, restart it
\r
235 if(!(spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id)){
\r
236 spri->curr_anim_spri = 0;
\r
237 spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id;
\r
239 spri->delay = spri->curr_anim_list[spri->curr_anim_spri].delay;
\r
241 // Delay > 1 means that we should not change sprite yet. Decrease delay
\r