]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_vrs.c
wwwwwwwwwwwwwwwwwwwwwwwwwwww vrstest.c works wwwwww yakui-lover wwwww look! wwww
[16.git] / src / lib / 16_vrs.c
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 #include "src/lib/16_vrs.h"
23 #include "src/lib/typdefst.h"
24
25 // Read .vrs file into far memory
26 int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container *vrs_cont){
27         int fd;
28         dword size;
29 #ifdef __WATCOMC__
30         __segment seg;
31         void __based(seg)* bigbuffer;
32 #endif
33 #ifdef __BORLANDC__
34         memptr bigbuffer;
35 #endif
36         byte huge *buffer;
37         vrl1_vgax_offset_t **vrl_line_offsets;
38         uint32_t huge *vrl_headers_offsets;
39         uint16_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
45         // Open filename, get size of file,
46         // populate the vrs_container if all tests pass
47         fd = open(filename, O_RDONLY|O_BINARY);
48         // Insert sanity cheks later
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         }
59         vrs_cont->data_size = size - sizeof(struct vrs_header);
60         vrs_cont->buffer = buffer;
61
62         // Calculate vrl offsets
63         
64         // Count sprites
65         vrl_id_iter = (uint16_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] - sizeof(struct vrl1_vgax_header);
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] - sizeof(struct vrl1_vgax_header);
84                 }
85                 vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(struct vrl1_vgax_header), vrl_size);
86         }
87         vrs_cont->vrl_line_offsets = vrl_line_offsets;
88         return 0;
89 }
90
91 // Seek and return a specified .vrl blob from .vrs blob in far memory
92 int get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint16_t id, struct vrl_container *vrl_cont){
93         uint16_t huge *ids;
94         uint32_t huge *vrl_offs_list;
95         int counter = 0;
96
97         // If id is invalid, return -1
98         if(id == 0){
99                 // Probably add an error message?
100                 return -1;
101         }
102
103         // Get id list from .vrs blob (base + offset)
104         ids = (uint16_t huge*)(vrs_cont->buffer + 
105                 vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);
106
107         // Loop through the id list until we found the right one or hit the end of the list
108         // Counter is keeping track of the offset(in ids/vrl blobs)
109         while(ids[counter] != id && ids[counter]){
110                 counter++;
111         }
112         // Return -2 if we couldn't find the requested id
113         if(!ids[counter]){
114                 // Error message?
115                 return -2;
116         }
117
118         // Get vrl offsets list from .vrs blob (base + offset)
119         vrl_offs_list = (uint32_t huge *)(vrs_cont->buffer +
120                                         vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
121
122         // Get vrl_header from .vrs (base + offset from vrl_list)
123         // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)
124         vrl_cont->vrl_header = (struct vrl1_vgax_header huge *)(vrs_cont->buffer + vrl_offs_list[counter]);
125
126         // Get .vrl size by integer arithmetics (next vrl offset - current vrl offset)
127         if(ids[counter+1]){
128                 vrl_cont->data_size = vrl_offs_list[counter+1] - vrl_offs_list[counter] - sizeof(struct vrl1_vgax_header);
129         }
130         // If we are retriving the last vrl, size is ids_list offset - current vrl offset, as next vrl offs is 0
131         else{
132                 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);
133         }
134
135         // Retrive line offsets form .vrs
136         vrl_cont->line_offsets = vrs_cont->vrl_line_offsets[counter];
137
138         return 0;
139 }