]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_vrs.c
ok wwwww zscroll and scroll compile disabled due to me being too tired to whack at...
[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 \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
26         int fd;\r
27         dword size;\r
28         byte huge *buffer;\r
29         vrl1_vgax_offset_t **vrl_line_offsets;\r
30         uint32_t huge *vrl_headers_offsets;\r
31         uint16_t huge *vrl_id_iter;\r
32         uint32_t vrl_size;\r
33         int num_of_vrl=0, i;\r
34         struct vrl1_vgax_header huge *curr_vrl;\r
35         int success=1;\r
36 \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
45         close(fd);\r
46         if(!success)\r
47         {\r
48                 fprintf(stderr, "Unable to load file");\r
49                 exit(3);\r
50         }\r
51         vrs_cont->data_size = size - sizeof(struct vrs_header);\r
52         vrs_cont->buffer = buffer;\r
53 \r
54         // Calculate vrl offsets\r
55 \r
56         // Count sprites\r
57         vrl_id_iter = (uint16_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);\r
58         while(vrl_id_iter[num_of_vrl]){\r
59                 num_of_vrl++;\r
60         }\r
61         // Allocate memory for vrl line offsets table\r
62         vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t *)*num_of_vrl);\r
63 \r
64         vrl_headers_offsets = (uint32_t huge *)(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 huge *)(buffer + vrl_headers_offsets[i]);\r
68 \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
72                 }\r
73                 // If it's the last vrl, size is (next_vrs_struct_offset - curr_offset)\r
74                 else{\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
76                 }\r
77                 vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(struct vrl1_vgax_header), vrl_size);\r
78         }\r
79         vrs_cont->vrl_line_offsets = vrl_line_offsets;\r
80         return 0;\r
81 }\r
82 \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 /*huge*/ *vrs_cont, uint16_t id, struct vrl_container *vrl_cont){\r
85         uint16_t huge *ids;\r
86         uint32_t huge *vrl_offs_list;\r
87         int counter = 0;\r
88 \r
89         // If id is invalid, return -1\r
90         if(id == 0){\r
91                 // Probably add an error message?\r
92                 return -1;\r
93         }\r
94 \r
95         // Get id list from .vrs blob (base + offset)\r
96         ids = (uint16_t huge*)(vrs_cont->buffer +\r
97                 vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);\r
98 \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
102                 counter++;\r
103         }\r
104         // Return -2 if we couldn't find the requested id\r
105         if(!ids[counter]){\r
106                 // Error message?\r
107                 return -2;\r
108         }\r
109 \r
110         // Get vrl offsets list from .vrs blob (base + offset)\r
111         vrl_offs_list = (uint32_t huge *)(vrs_cont->buffer +\r
112                                         vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);\r
113 \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 huge *)(vrs_cont->buffer + vrl_offs_list[counter]);\r
117 \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
121         }\r
122         // If we are retriving the last vrl, size is ids_list offset - current vrl offset, as next vrl offs is 0\r
123         else{\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
125         }\r
126 \r
127         // Retrive line offsets form .vrs\r
128         vrl_cont->line_offsets = vrs_cont->vrl_line_offsets[counter];\r
129 \r
130         return 0;\r
131 }\r