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
23 #include "src/lib/typdefst.h"
\r
25 // Read .vrs file into far memory
\r
26 int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container *vrs_cont){
\r
30 vrl1_vgax_offset_t **vrl_line_offsets;
\r
31 uint32_t huge *vrl_headers_offsets;
\r
32 uint16_t huge *vrl_id_iter;
\r
34 int num_of_vrl=0, i;
\r
35 struct vrl1_vgax_header huge *curr_vrl;
\r
38 // Open filename, get size of file,
\r
39 // populate the vrs_container if all tests pass
\r
40 fd = open(filename, O_RDONLY|O_BINARY);
\r
41 // Insert sanity cheks later
\r
42 size = lseek(fd, 0, SEEK_END);
\r
43 buffer = malloc(size);
\r
44 lseek(fd, 0, SEEK_SET);
\r
45 read(fd, buffer, size);
\r
49 fprintf(stderr, "Unablee to load file");
\r
52 vrs_cont->data_size = size - sizeof(struct vrs_header);
\r
53 vrs_cont->buffer = buffer;
\r
55 // Calculate vrl offsets
\r
58 vrl_id_iter = (uint16_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);
\r
59 while(vrl_id_iter[num_of_vrl]){
\r
62 // Allocate memory for vrl line offsets table
\r
63 vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t *)*num_of_vrl);
\r
65 vrl_headers_offsets = (uint32_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
\r
66 // Calculate line offsets for each vrl
\r
67 for(i = 0; i < num_of_vrl; i++){
\r
68 curr_vrl = (struct vrl1_vgax_header huge *)(buffer + vrl_headers_offsets[i]);
\r
70 // Calc. vrl size as (next_offset - curr_offset)
\r
71 if (i != num_of_vrl - 1){
\r
72 vrl_size = vrl_headers_offsets[i+1] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);
\r
74 // If it's the last vrl, size is (next_vrs_struct_offset - curr_offset)
\r
76 vrl_size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);
\r
78 vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(struct vrl1_vgax_header), vrl_size);
\r
80 vrs_cont->vrl_line_offsets = vrl_line_offsets;
\r
84 // Seek and return a specified .vrl blob from .vrs blob in far memory
\r
85 int get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint16_t id, struct vrl_container *vrl_cont){
\r
87 uint32_t huge *vrl_offs_list;
\r
90 // If id is invalid, return -1
\r
92 // Probably add an error message?
\r
96 // Get id list from .vrs blob (base + offset)
\r
97 ids = (uint16_t huge*)(vrs_cont->buffer +
\r
98 vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);
\r
100 // Loop through the id list until we found the right one or hit the end of the list
\r
101 // Counter is keeping track of the offset(in ids/vrl blobs)
\r
102 while(ids[counter] != id && ids[counter]){
\r
105 // Return -2 if we couldn't find the requested id
\r
111 // Get vrl offsets list from .vrs blob (base + offset)
\r
112 vrl_offs_list = (uint32_t huge *)(vrs_cont->buffer +
\r
113 vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
\r
115 // Get vrl_header from .vrs (base + offset from vrl_list)
\r
116 // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)
\r
117 vrl_cont->vrl_header = (struct vrl1_vgax_header huge *)(vrs_cont->buffer + vrl_offs_list[counter]);
\r
119 // Get .vrl size by integer arithmetics (next vrl offset - current vrl offset)
\r
120 if(ids[counter+1]){
\r
121 vrl_cont->data_size = vrl_offs_list[counter+1] - vrl_offs_list[counter] - sizeof(struct vrl1_vgax_header);
\r
123 // If we are retriving the last vrl, size is ids_list offset - current vrl offset, as next vrl offs is 0
\r
125 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
128 // Retrive line offsets form .vrs
\r
129 vrl_cont->line_offsets = vrs_cont->vrl_line_offsets[counter];
\r