]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_vrs.c
Merge remote-tracking branch 'upstream/master'
[16.git] / src / lib / 16_vrs.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
3  *\r
4  * This file is part of Project 16.\r
5  *\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
10  *\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
15  *\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
20  *\r
21  */\r
22 #include "src/lib/16_vrs.h"\r
23 #include "src/lib/typdefst.h"\r
24 \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
27         int fd;\r
28         dword size;\r
29 #ifdef __WATCOMC__\r
30         __segment seg;\r
31         void __based(seg)* bigbuffer;\r
32 #endif\r
33 #ifdef __BORLANDC__\r
34         memptr bigbuffer;\r
35 #endif\r
36         byte huge *buffer;\r
37         vrl1_vgax_offset_t **vrl_line_offsets;\r
38         uint32_t huge *vrl_headers_offsets;\r
39         uint16_t huge *vrl_id_iter;\r
40         uint32_t vrl_size;\r
41         int num_of_vrl, i;\r
42         struct vrl1_vgax_header huge *curr_vrl;\r
43         int success;\r
44 \r
45         // Open filename, get size of file,\r
46         // populate the vrs_container if all tests pass\r
47         fd = open(filename, O_RDONLY|O_BINARY);\r
48         // Insert sanity cheks later\r
49         size = lseek(fd, 0, SEEK_END);\r
50         buffer = malloc(size);\r
51         lseek(fd, 0, SEEK_SET);\r
52         read(fd, buffer, size);\r
53         close(fd);\r
54         if(!success)\r
55         {\r
56                 fprintf(stderr, "Unablee to load file");\r
57                 exit(3);\r
58         }\r
59         vrs_cont->data_size = size - sizeof(struct vrs_header);\r
60         vrs_cont->buffer = buffer;\r
61 \r
62         // Calculate vrl offsets\r
63         \r
64         // Count sprites\r
65         vrl_id_iter = (uint16_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);\r
66         while(vrl_id_iter[num_of_vrl]){\r
67                 num_of_vrl++;\r
68         }\r
69         // Allocate memory for vrl line offsets table\r
70         vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t *)*num_of_vrl);\r
71 \r
72         vrl_headers_offsets = (uint32_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);\r
73         // Calculate line offsets for each vrl\r
74         for(i = 0; i < num_of_vrl; i++){\r
75                 curr_vrl = (struct vrl1_vgax_header huge *)(buffer + vrl_headers_offsets[i]);\r
76 \r
77                 // Calc. vrl size as (next_offset - curr_offset)\r
78                 if (i != num_of_vrl - 1){\r
79                         vrl_size = vrl_headers_offsets[i+1] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);\r
80                 }\r
81                 // If it's the last vrl, size is (next_vrs_struct_offset - curr_offset)\r
82                 else{\r
83                         vrl_size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);\r
84                 }\r
85                 vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(struct vrl1_vgax_header), vrl_size);\r
86         }\r
87         vrs_cont->vrl_line_offsets = vrl_line_offsets;\r
88         return 0;\r
89 }\r
90 \r
91 // Seek and return a specified .vrl blob from .vrs blob in far memory\r
92 int get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint16_t id, struct vrl_container *vrl_cont){\r
93         uint16_t huge *ids;\r
94         uint32_t huge *vrl_offs_list;\r
95         int counter = 0;\r
96 \r
97         // If id is invalid, return -1\r
98         if(id == 0){\r
99                 // Probably add an error message?\r
100                 return -1;\r
101         }\r
102 \r
103         // Get id list from .vrs blob (base + offset)\r
104         ids = (uint16_t huge*)(vrs_cont->buffer + \r
105                 vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);\r
106 \r
107         // Loop through the id list until we found the right one or hit the end of the list\r
108         // Counter is keeping track of the offset(in ids/vrl blobs)\r
109         while(ids[counter] != id && ids[counter]){\r
110                 counter++;\r
111         }\r
112         // Return -2 if we couldn't find the requested id\r
113         if(!ids[counter]){\r
114                 // Error message?\r
115                 return -2;\r
116         }\r
117 \r
118         // Get vrl offsets list from .vrs blob (base + offset)\r
119         vrl_offs_list = (uint32_t huge *)(vrs_cont->buffer +\r
120                                         vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);\r
121 \r
122         // Get vrl_header from .vrs (base + offset from vrl_list)\r
123         // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)\r
124         vrl_cont->vrl_header = (struct vrl1_vgax_header huge *)(vrs_cont->buffer + vrl_offs_list[counter]);\r
125 \r
126         // Get .vrl size by integer arithmetics (next vrl offset - current vrl offset)\r
127         if(ids[counter+1]){\r
128                 vrl_cont->data_size = vrl_offs_list[counter+1] - vrl_offs_list[counter] - sizeof(struct vrl1_vgax_header);\r
129         }\r
130         // If we are retriving the last vrl, size is ids_list offset - current vrl offset, as next vrl offs is 0\r
131         else{\r
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);\r
133         }\r
134 \r
135         // Retrive line offsets form .vrs\r
136         vrl_cont->line_offsets = vrs_cont->vrl_line_offsets[counter];\r
137 \r
138         return 0;\r
139 }\r