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
22 #include "src/lib/16_vrs.h"
\r
24 // Read .vrs file into far memory
\r
25 int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container *vrs_cont){
\r
29 vrl1_vgax_offset_t **vrl_line_offsets;
\r
30 uint32_t far *vrl_headers_offsets;
\r
31 uint16_t far *vrl_id_iter;
\r
33 int num_of_vrl=0, i;
\r
34 struct vrl1_vgax_header far *curr_vrl;
\r
37 // Open filename, get size of file,
\r
38 // populate the vrs_container if all tests pass
\r
39 fd = open(filename, O_RDONLY|O_BINARY);
\r
40 // Insert sanity cheks later
\r
41 size = lseek(fd, 0, SEEK_END);
\r
42 buffer = malloc(size);
\r
43 lseek(fd, 0, SEEK_SET);
\r
44 read(fd, buffer, size);
\r
48 fprintf(stderr, "Unable to load file");
\r
51 vrs_cont->data_size = size - sizeof(struct vrs_header);
\r
52 vrs_cont->buffer = buffer;
\r
54 // Calculate vrl offsets
\r
57 vrl_id_iter = (uint16_t far *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);
\r
58 while(vrl_id_iter[num_of_vrl]){
\r
61 // Allocate memory for vrl line offsets table
\r
62 vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t *)*num_of_vrl);
\r
64 vrl_headers_offsets = (uint32_t far *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
\r
65 // Calculate line offsets for each vrl
\r
66 for(i = 0; i < num_of_vrl; i++){
\r
67 curr_vrl = (struct vrl1_vgax_header far *)(buffer + vrl_headers_offsets[i]);
\r
69 // Calc. vrl size as (next_offset - curr_offset)
\r
70 if (i != num_of_vrl - 1){
\r
71 vrl_size = vrl_headers_offsets[i+1] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);
\r
73 // If it's the last vrl, size is (next_vrs_struct_offset - curr_offset)
\r
75 vrl_size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);
\r
77 vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(struct vrl1_vgax_header), vrl_size);
\r
79 vrs_cont->vrl_line_offsets = vrl_line_offsets;
\r
83 // Seek and return a specified .vrl blob from .vrs blob in far memory
\r
84 int get_vrl_by_id(struct vrs_container far *vrs_cont, uint16_t id, struct vrl_container *vrl_cont){
\r
86 uint32_t far *vrl_offs_list;
\r
89 // If id is invalid, return -1
\r
91 // Probably add an error message?
\r
95 // Get id list from .vrs blob (base + offset)
\r
96 ids = (uint16_t far*)(vrs_cont->buffer +
\r
97 vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);
\r
99 // Loop through the id list until we found the right one or hit the end of the list
\r
100 // Counter is keeping track of the offset(in ids/vrl blobs)
\r
101 while(ids[counter] != id && ids[counter]){
\r
104 // Return -2 if we couldn't find the requested id
\r
110 // Get vrl offsets list from .vrs blob (base + offset)
\r
111 vrl_offs_list = (uint32_t far *)(vrs_cont->buffer +
\r
112 vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
\r
114 // Get vrl_header from .vrs (base + offset from vrl_list)
\r
115 // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)
\r
116 vrl_cont->vrl_header = (struct vrl1_vgax_header far *)(vrs_cont->buffer + vrl_offs_list[counter]);
\r
118 // Get .vrl size by integer arithmetics (next vrl offset - current vrl offset)
\r
119 if(ids[counter+1]){
\r
120 vrl_cont->data_size = vrl_offs_list[counter+1] - vrl_offs_list[counter] - sizeof(struct vrl1_vgax_header);
\r
122 // If we are retriving the last vrl, size is ids_list offset - current vrl offset, as next vrl offs is 0
\r
124 vrl_cont->data_size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_offs_list[counter] - sizeof(struct vrl1_vgax_header);
\r
127 // Retrive line offsets form .vrs
\r
128 vrl_cont->line_offsets = vrs_cont->vrl_line_offsets[counter];
\r