wwww
Revision | 56d64244b2a13bc4aabb73d6392ad968528feea8 (tree) |
---|---|
Time | 2016-08-22 01:54:17 |
Author | sparky4 <sparky4@cock...> |
Commiter | sparky4 |
added files from branch wwww
@@ -0,0 +1,114 @@ | ||
1 | +#include "src/lib/16_sprite.h" | |
2 | + | |
3 | +char* get_curr_anim_name(struct sprite *spri) | |
4 | +{ | |
5 | + // Retrive animation name list | |
6 | + struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr; | |
7 | + uint32_t huge *anim_names_offsets = (uint32_t huge *) | |
8 | + ((byte huge *)vrs + | |
9 | + vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_NAME_LIST]); | |
10 | + | |
11 | + return (char *)(vrs + anim_names_offsets[spri->curr_anim_spri]); | |
12 | +} | |
13 | + | |
14 | +void init_anim(struct sprite *spri, int anim_index) | |
15 | +{ | |
16 | + struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr; | |
17 | + uint32_t huge *anim_lists_offsets = (uint32_t huge *) | |
18 | + ((byte huge *)vrs + | |
19 | + vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_LIST]); | |
20 | + struct vrs_animation_list_entry_t *anim_list = (struct vrs_animation_list_entry_t huge *) | |
21 | + ((byte huge *)vrs + | |
22 | + anim_lists_offsets[anim_index]); | |
23 | + | |
24 | + // Upon new animation, start from the first sprite in it | |
25 | + spri->curr_anim_spri = 0; | |
26 | + spri->curr_spri_id = anim_list[0].sprite_id; | |
27 | + spri->delay = anim_list[0].delay; | |
28 | + | |
29 | + spri->curr_anim_list = anim_list; | |
30 | +} | |
31 | + | |
32 | + | |
33 | +int set_anim_by_id(struct sprite *spri, int anim_id) | |
34 | +{ | |
35 | + int new_anim_index = 0; | |
36 | + int iter_id; | |
37 | + struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr; | |
38 | + // Retruve animation ids list | |
39 | + uint16_t huge *anim_ids = (uint16_t huge *) | |
40 | + ((byte huge *)vrs + | |
41 | + vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]); | |
42 | + | |
43 | + // Loop through animation id untill match or end of list | |
44 | + while(iter_id = anim_ids[new_anim_index]) | |
45 | + { | |
46 | + // Return on successful match | |
47 | + if (iter_id = anim_id) | |
48 | + { | |
49 | + init_anim(spri, new_anim_index); | |
50 | + return 0; | |
51 | + } | |
52 | + new_anim_index++; | |
53 | + } | |
54 | + return -1; | |
55 | +} | |
56 | + | |
57 | +void print_anim_ids(struct sprite *spri) | |
58 | +{ | |
59 | + int new_anim_index = 0; | |
60 | + int iter_id; | |
61 | + struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr; | |
62 | + // Retruve animation ids list | |
63 | + uint16_t huge *anim_ids = (uint16_t huge *) | |
64 | + ((byte huge *)vrs + | |
65 | + vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]); | |
66 | + | |
67 | + printf("\nPos %lld off %lld\n", (uint32_t)vrs, vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]); | |
68 | + if(!anim_ids[new_anim_index]) | |
69 | + exit(3); | |
70 | + // Loop through animation id untill match or end of list | |
71 | + while(iter_id = anim_ids[new_anim_index]) | |
72 | + { | |
73 | + // Return on successful match | |
74 | + new_anim_index++; | |
75 | + printf("%d, ", iter_id); | |
76 | + } | |
77 | +} | |
78 | + | |
79 | + | |
80 | +void animate_spri(struct sprite *spri) | |
81 | +{ | |
82 | + struct vrl_container *vrl_cont; | |
83 | + // Events go here | |
84 | + | |
85 | + // Draw sprite | |
86 | + vrl_cont = get_vrl_by_id(spri->spritesheet, spri->curr_spri_id); | |
87 | + draw_vrl1_vgax_modex( spri->x, spri->y, | |
88 | + vrl_cont->vrl_header, vrl_cont->line_offsets, | |
89 | + vrl_cont->buffer + sizeof(struct vrl1_vgax_header), | |
90 | + vrl_cont->size - sizeof(struct vrl1_vgax_header)); | |
91 | + | |
92 | + // Depending on delay, update indices | |
93 | + switch(spri->delay){ | |
94 | + // Delay = 0 means that sprite should loop. Nothing to change here | |
95 | + case 0: | |
96 | + break; | |
97 | + | |
98 | + // Delay = 1 means that on next time unit sprite should be changed | |
99 | + case 1: | |
100 | + spri->curr_anim_spri++; | |
101 | + | |
102 | + // If we hit the end of an animation sequence, restart it | |
103 | + if(!(spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id)){ | |
104 | + spri->curr_anim_spri = 0; | |
105 | + spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id; | |
106 | + } | |
107 | + spri->delay = spri->curr_anim_list[spri->curr_anim_spri].delay; | |
108 | + | |
109 | + // Delay > 1 means that we should not change sprite yet. Decrease delay | |
110 | + default: | |
111 | + spri->delay--; | |
112 | + break; | |
113 | + } | |
114 | +} |
@@ -0,0 +1,72 @@ | ||
1 | +/* Project 16 Source Code~ | |
2 | + * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover | |
3 | + * | |
4 | + * This file is part of Project 16. | |
5 | + * | |
6 | + * Project 16 is free software; you can redistribute it and/or modify | |
7 | + * it under the terms of the GNU General Public License as published by | |
8 | + * the Free Software Foundation; either version 3 of the License, or | |
9 | + * (at your option) any later version. | |
10 | + * | |
11 | + * Project 16 is distributed in the hope that it will be useful, | |
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | + * GNU General Public License for more details. | |
15 | + * | |
16 | + * You should have received a copy of the GNU General Public License | |
17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>, or | |
18 | + * write to the Free Software Foundation, Inc., 51 Franklin Street, | |
19 | + * Fifth Floor, Boston, MA 02110-1301 USA. | |
20 | + * | |
21 | + */ | |
22 | +#ifndef __16_SPRI__ | |
23 | +#define __16_SPRI__ | |
24 | + | |
25 | +#include "src/lib/16_vrs.h" | |
26 | +#include "src/lib/typdefst.h" | |
27 | + | |
28 | +struct sprite | |
29 | +{ | |
30 | + // VRS container from which we will extract animation and image data | |
31 | + struct vrs_container *spritesheet; | |
32 | + // Current sprite id | |
33 | + int curr_spri_id; | |
34 | + // Index of a current sprite in an animation sequence | |
35 | + int curr_anim_spri; | |
36 | + // Current animation sequence | |
37 | + struct vrs_animation_list_entry_t *curr_anim_list; | |
38 | + // Index of current animation in relevant VRS offsets table | |
39 | + int curr_anim; | |
40 | + // Delay in time units untill we should change sprite | |
41 | + int delay; | |
42 | + // Position of sprite on screen | |
43 | + int x, y; | |
44 | +}; | |
45 | + | |
46 | +/* Retrive current animation name of sprite | |
47 | +* In: | |
48 | +* + struct sprite *spri - sprite to retrive current animation sequence name from | |
49 | +* Out: | |
50 | +* + char* - animation sequence name | |
51 | +*/ | |
52 | +char* get_curr_anim_name(struct sprite *spri); | |
53 | + | |
54 | +/* Change sprite's current animation to the one given by id | |
55 | + * In: | |
56 | + * struct sprite *spri - sprite to manipulate on | |
57 | + * int id - id of a new animation sequence of th sprite | |
58 | + * Out: | |
59 | + * int - 0 on success, -1 on error | |
60 | + */ | |
61 | +int set_anim_by_id(struct sprite *spri, int id); | |
62 | + | |
63 | +/* Animate sprite, triggering any events and changing indices if necessary | |
64 | + * NB: if you want to change animation sequence after a specific sprite is shown, you should call animate_spri first | |
65 | + * In: | |
66 | + * + struct sprite *spri - sprite to animate | |
67 | + */ | |
68 | +void animate_spri(struct sprite *spri); | |
69 | + | |
70 | +void print_anim_ids(struct sprite *spri); | |
71 | + | |
72 | +#endif |
@@ -20,38 +20,91 @@ | ||
20 | 20 | * |
21 | 21 | */ |
22 | 22 | #include "src/lib/16_vrs.h" |
23 | +#include "src/lib/typdefst.h" | |
23 | 24 | |
24 | 25 | // Read .vrs file into far memory |
25 | 26 | int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container *vrs_cont){ |
26 | 27 | int fd; |
27 | 28 | dword size; |
29 | +#ifdef __WATCOMC__ | |
30 | + __segment seg; | |
31 | + void __based(seg)* bigbuffer; | |
32 | +#endif | |
33 | +#ifdef __BORLANDC__ | |
34 | + memptr bigbuffer; | |
35 | +#endif | |
28 | 36 | byte huge *buffer; |
37 | + vrl1_vgax_offset_t **vrl_line_offsets; | |
38 | + uint32_t huge *vrl_headers_offsets; | |
39 | + uint32_t huge *vrl_id_iter; | |
40 | + uint32_t vrl_size; | |
41 | + int num_of_vrl, i; | |
42 | + struct vrl1_vgax_header huge *curr_vrl; | |
43 | + int success; | |
44 | + | |
29 | 45 | // Open filename, get size of file, |
30 | 46 | // populate the vrs_container if all tests pass |
31 | - fd = open((filename), O_RDONLY|O_BINARY); | |
32 | - size = filelength(fd); | |
33 | - close(fd); | |
47 | + fd = open(filename, O_RDONLY|O_BINARY); | |
34 | 48 | // Insert sanity cheks later |
35 | - CA_LoadFile(filename, buffer, &gvar->mm, &gvar->mmi); | |
49 | + size = lseek(fd, 0, SEEK_END); | |
50 | + buffer = malloc(size); | |
51 | + lseek(fd, 0, SEEK_SET); | |
52 | + read(fd, buffer, size); | |
53 | + close(fd); | |
54 | + if(!success) | |
55 | + { | |
56 | + fprintf(stderr, "Unablee to load file"); | |
57 | + exit(3); | |
58 | + } | |
36 | 59 | vrs_cont->size = size; |
37 | 60 | vrs_cont->buffer = buffer; |
61 | + | |
62 | + // Calculate vrl offsets | |
63 | + | |
64 | + // Count sprites | |
65 | + vrl_id_iter = (uint32_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]); | |
66 | + while(vrl_id_iter[num_of_vrl]){ | |
67 | + num_of_vrl++; | |
68 | + } | |
69 | + // Allocate memory for vrl line offsets table | |
70 | + vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t)*num_of_vrl); | |
71 | + | |
72 | + vrl_headers_offsets = (uint32_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]); | |
73 | + // Calculate line offsets for each vrl | |
74 | + for(i = 0; i < num_of_vrl; i++){ | |
75 | + curr_vrl = (struct vrl1_vgax_header huge *)(buffer + vrl_headers_offsets[i]); | |
76 | + | |
77 | + // Calc. vrl size as (next_offset - curr_offset) | |
78 | + if (i != num_of_vrl - 1){ | |
79 | + vrl_size = vrl_headers_offsets[i+1] - vrl_headers_offsets[i]; | |
80 | + } | |
81 | + // If it's the last vrl, size is (next_vrs_struct_offset - curr_offset) | |
82 | + else{ | |
83 | + vrl_size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i]; | |
84 | + } | |
85 | + vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(*curr_vrl), vrl_size - sizeof(*curr_vrl)); | |
86 | + } | |
87 | + vrs_cont->vrl_line_offsets = vrl_line_offsets; | |
38 | 88 | return 0; |
39 | 89 | } |
40 | 90 | |
41 | 91 | // Seek and return a specified .vrl blob from .vrs blob in far memory |
42 | -struct vrl_container get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint16_t id){ | |
92 | +struct vrl_container * get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint16_t id){ | |
43 | 93 | uint16_t huge *ids; |
44 | - uint32_t huge *vrl_list; | |
94 | + uint32_t huge *vrl_offs_list; | |
45 | 95 | struct vrl_container huge *vrl_cont; |
46 | 96 | int counter = 0; |
97 | + | |
47 | 98 | // If id is invalid, return null |
48 | 99 | if(id == 0){ |
49 | 100 | // Probably add an error message? |
50 | 101 | return 0; |
51 | 102 | } |
103 | + | |
52 | 104 | // Get id list from .vrs blob (base + offset) |
53 | 105 | ids = (uint16_t huge*)vrs_cont->buffer + |
54 | 106 | (dword)vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]; |
107 | + | |
55 | 108 | // Loop through the id list until we found the right one or hit the end of the list |
56 | 109 | // Counter is keeping track of the offset(in ids/vrl blobs) |
57 | 110 | while(ids[counter] != id && ids[counter]){ |
@@ -62,16 +115,29 @@ struct vrl_container get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint | ||
62 | 115 | // Error message? |
63 | 116 | return 0; |
64 | 117 | } |
65 | - // Get vrl list from .vrs blob (base + offset) | |
66 | - vrl_list = (uint32_t huge *)(vrs_cont->buffer + | |
118 | + | |
119 | + // Get vrl offsets list from .vrs blob (base + offset) | |
120 | + vrl_offs_list = (uint32_t huge *)(vrs_cont->buffer + | |
67 | 121 | vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]); |
122 | + | |
68 | 123 | // Allocate memory for vrl_cont |
69 | - vrl_cont = (struct vrl_container)malloc(sizeof(struct vrl_container)); | |
124 | + vrl_cont = (struct vrl_container *)malloc(sizeof(struct vrl_container)); | |
125 | + | |
70 | 126 | // Get vrl_header from .vrs (base + offset from vrl_list) |
71 | 127 | // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification) |
72 | - vrl_cont->vrl_header = (struct vrl1_vgax_header huge *)(vrs_cont->buffer + vrl_list[counter]); | |
73 | - // Get .vrl size by integer arithmetics (next vrl - current vrl) | |
74 | - // Untested. May be an incorrect way to do so | |
75 | - vrl_cont->size = vrl_list[counter+1] - vrl_list[counter]; | |
128 | + vrl_cont->vrl_header = (struct vrl1_vgax_header huge *)(vrs_cont->buffer + vrl_offs_list[counter]); | |
129 | + | |
130 | + // Get .vrl size by integer arithmetics (next vrl offset - current vrl offset) | |
131 | + if(ids[counter+1]){ | |
132 | + vrl_cont->size = vrl_offs_list[counter+1] - vrl_offs_list[counter]; | |
133 | + } | |
134 | + // If we are retriving the last vrl, size is ids_list offset - current vrl offset, as next vrl offs is 0 | |
135 | + else{ | |
136 | + vrl_cont->size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_offs_list[counter]; | |
137 | + } | |
138 | + | |
139 | + // Retrive line offsets form .vrs | |
140 | + vrl_cont->line_offsets = vrs_cont->vrl_line_offsets[counter]; | |
141 | + | |
76 | 142 | return vrl_cont; |
77 | 143 | } |
@@ -22,52 +22,54 @@ | ||
22 | 22 | #ifndef __16_VRS__ |
23 | 23 | #define __16_VRS__ |
24 | 24 | |
25 | -#include "src/lib/16_head.h" | |
26 | 25 | #include "src/lib/modex16.h" |
27 | -#include "src/lib/16_ca.h" | |
28 | -#include "src/lib/16_mm.h" | |
26 | +#include "src/lib/typdefst.h" | |
29 | 27 | //#include <hw/cpu/cpu.h> |
30 | 28 | //#include <hw/dos/dos.h> |
31 | -//#include <hw/vga/vga.h> | |
32 | - | |
33 | -// Container for .vrs files loaded in memory with useful info | |
34 | -// Includes: | |
35 | -// + size of the .vrs blob in memory | |
36 | -// + pointer to the blob/vrs header | |
29 | +#include <hw/vga/vrl.h> | |
30 | +#include "src/lib/16_ca.h" | |
37 | 31 | |
38 | 32 | struct vrs_container{ |
33 | + // Size of a .vrs lob in memory | |
39 | 34 | dword size; |
40 | 35 | union{ |
41 | 36 | byte huge *buffer; |
42 | 37 | struct vrs_header huge *vrs_hdr; |
43 | 38 | }; |
39 | + // Array of corresponding vrl line offsets | |
40 | + vrl1_vgax_offset_t **vrl_line_offsets; | |
44 | 41 | }; |
45 | 42 | |
46 | 43 | struct vrl_container{ |
44 | + // Size of a .vrl blob in memory | |
47 | 45 | dword size; |
48 | 46 | union{ |
49 | 47 | byte huge *buffer; |
50 | 48 | struct vrl1_vgax_header huge *vrl_header; |
51 | 49 | }; |
50 | + // Pointer to a corresponding vrl line offsets struct | |
51 | + vrl1_vgax_offset_t *line_offsets; | |
52 | 52 | }; |
53 | 53 | |
54 | -// Read .vrs file into memory | |
55 | -// In: | |
56 | -// + char *filename - name of the file to load | |
57 | -// + struct vrs_container *vrs_cont - pointer to the vrs_container | |
58 | -// to load the file into | |
59 | -// Out: | |
60 | -// + int - 0 on succes, 1 on failure | |
54 | +/* Read .vrs file into memory | |
55 | +* In: | |
56 | +* + char *filename - name of the file to load | |
57 | +* + struct vrs_container *vrs_cont - pointer to the vrs_container | |
58 | +* to load the file into | |
59 | +* Out: | |
60 | +* + int - 0 on succes, 1 on failure | |
61 | +*/ | |
61 | 62 | int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container *vrs_cont); |
62 | 63 | |
63 | -// Seek and return a specified .vrl blob from .vrs blob in memory | |
64 | -// In: | |
65 | -// + struct vrs_container *vrs_cont - pointer to the vrs_container | |
66 | -// with a loaded .vrs file | |
67 | -// + uint16_t id - id of the vrl to retrive | |
68 | -// Out: | |
69 | -// struct vrl_container* - a pointer to a vrl_container with a pointer | |
70 | -// to the requested .vrl blob | |
71 | -struct vrl_container* get_vrl_by_id(struct vrs_container *vrs_cont, uint16_t id); | |
64 | +/* Seek and return a specified .vrl blob from .vrs blob in memory | |
65 | +* In: | |
66 | +* + struct vrs_container *vrs_cont - pointer to the vrs_container | |
67 | +* with a loaded .vrs file | |
68 | +* + uint16_t id - id of the vrl to retrive | |
69 | +* Out: | |
70 | +* struct vrl_container* - a pointer to a vrl_container with a pointer | |
71 | +* to the requested .vrl blob | |
72 | +*/ | |
73 | +struct vrl_container * get_vrl_by_id(struct vrs_container *vrs_cont, uint16_t id); | |
72 | 74 | |
73 | 75 | #endif |