-#include <stdio.h>
-#include <fcntl.h>
-#include <ctype.h>
-#include <errno.h>
-#include <assert.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "stdlib.h"
-#include "16_vrs.h"
-
-int read_vrs(char *filename, struct vrs_container *vrs_cont){
- int fd;
- unsigned long size;
- unsigned char *buffer;
- fd = open(filename, O_RDONLY|O_BINARY);
- // Insert sanity cheks later
- size = lseek(fd, 0, SEEK_END);
- buffer = malloc(size);
- lseek(fd, 0, SEEK_SET);
- read(fd, buffer, size);
- vrs_cont->size = size;
- vrs_cont->buffer = buffer;
-
- return 0;
-}
-
-struct vrl_container* get_vrl_by_id(struct vrs_container *vrs_cont, uint16_t id){
- uint16_t *ids;
- uint32_t *vrl_list;
- int counter = 0;
- struct vrl_container *vrl_cont;
- if(id == 0){
- return 0;
- }
- ids = (uint16_t*)vrs_cont->buffer + (unsigned long)vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST];
- while(ids[counter] != id && ids[counter]){
- counter++;
- }
- vrl_list = (uint32_t *)(vrs_cont->buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
- vrl_cont = (struct vrl_container*)malloc(sizeof(struct vrl_container));
- vrl_cont->vrl_header = (struct vrl1_vgax_header*)(vrs_cont->buffer + vrl_list[counter]);
- vrl_cont->size = vrl_list[counter+1] - vrl_list[counter];
- return vrl_cont;
-}
+/* Project 16 Source Code~\r
+ * Copyright (C) 2012-2017 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
+ *\r
+ * This file is part of Project 16.\r
+ *\r
+ * Project 16 is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Project 16 is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program. If not, see <http://www.gnu.org/licenses/>, or\r
+ * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
+ * Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ */\r
+#include "src/lib/16_vrs.h"\r
+\r
+void VRS_ReadVRS(char *filename, entity_t *enti, global_game_variables_t *gvar){ VRS_OpenVRS(filename, enti, 1, gvar); }\r
+void VRS_LoadVRS(char *filename, entity_t *enti, global_game_variables_t *gvar){ VRS_OpenVRS(filename, enti, 0, gvar); }\r
+void VRS_OpenVRS(char *filename, entity_t *enti, boolean rlsw, global_game_variables_t *gvar)\r
+{\r
+ vrl1_vgax_offset_t **vrl_line_offsets;\r
+ uint16_t far *vrl_id_iter;\r
+ uint32_t far *vrl_headers_offsets;\r
+ struct vrl1_vgax_header far *curr_vrl;\r
+ uint32_t vrl_size;\r
+ int num_of_vrl,i;\r
+ int size=0;\r
+\r
+ switch(rlsw)\r
+ {\r
+ case 1:\r
+ CA_ReadFile(filename, &gvar->ca.spribuff, gvar);\r
+ break;\r
+ case 0:\r
+ CA_LoadFile(filename, &gvar->ca.spribuff, gvar);\r
+ break;\r
+ }\r
+\r
+ // Insert sanity cheks later\r
+ enti->spri.spritesheet.buffer = gvar->ca.spribuff;\r
+ enti->spri.spritesheet.data_size = size - sizeof(struct vrl1_vgax_header);\r
+ num_of_vrl = 0;\r
+ vrl_id_iter = (uint16_t far *)(enti->spri.spritesheet.buffer + enti->spri.spritesheet.vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);\r
+ while(vrl_id_iter[num_of_vrl]){\r
+ num_of_vrl++;\r
+ }\r
+\r
+ // Allocate memory for vrl line offsets table\r
+ vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t *)*num_of_vrl);//TODO: USE MM_ CA_ AND PM_\r
+\r
+ vrl_headers_offsets = (uint32_t far *)(enti->spri.spritesheet.buffer + enti->spri.spritesheet.vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);\r
+ // Calculate line offsets for each vrl\r
+ for(i = 0; i < num_of_vrl; i++){\r
+ curr_vrl = (struct vrl1_vgax_header far *)(enti->spri.spritesheet.buffer + vrl_headers_offsets[i]);\r
+\r
+ // Calc. vrl size as (next_offset - curr_offset)\r
+ if (i != num_of_vrl - 1){\r
+ vrl_size = vrl_headers_offsets[i+1] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);\r
+ }\r
+ // If it's the last vrl, size is (next_vrs_struct_offset - curr_offset)\r
+ else{\r
+ vrl_size = enti->spri.spritesheet.vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);\r
+ }\r
+ vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(struct vrl1_vgax_header), vrl_size);\r
+ }\r
+ enti->spri.spritesheet.vrl_line_offsets = vrl_line_offsets;\r
+}\r
+\r
+// Seek and return a specified .vrl blob from .vrs blob in far memory\r
+int get_vrl_by_id(struct vrs_container far *vrs_cont, uint16_t id, struct vrl_container *vrl_cont){\r
+ uint16_t far *ids;\r
+ uint32_t far *vrl_offs_list;\r
+ int counter = 0;\r
+\r
+ // If id is invalid, return -1\r
+ if(id == 0){\r
+ // Probably add an error message?\r
+ return -1;\r
+ }\r
+\r
+ // Get id list from .vrs blob (base + offset)\r
+ ids = (uint16_t far*)(vrs_cont->buffer +\r
+ vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);\r
+\r
+ // Loop through the id list until we found the right one or hit the end of the list\r
+ // Counter is keeping track of the offset(in ids/vrl blobs)\r
+ while(ids[counter] != id && ids[counter]){\r
+ counter++;\r
+ }\r
+ // Return -2 if we couldn't find the requested id\r
+ if(!ids[counter]){\r
+ // Error message?\r
+ return -2;\r
+ }\r
+\r
+ // Get vrl offsets list from .vrs blob (base + offset)\r
+ vrl_offs_list = (uint32_t far *)(vrs_cont->buffer +\r
+ vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);\r
+\r
+ // Get vrl_header from .vrs (base + offset from vrl_list)\r
+ // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)\r
+ vrl_cont->vrl_header = (struct vrl1_vgax_header far *)(vrs_cont->buffer + vrl_offs_list[counter]);\r
+\r
+ // Get .vrl size by integer arithmetics (next vrl offset - current vrl offset)\r
+ if(ids[counter+1]){\r
+ vrl_cont->data_size = vrl_offs_list[counter+1] - vrl_offs_list[counter] - sizeof(struct vrl1_vgax_header);\r
+ }\r
+ // If we are retriving the last vrl, size is ids_list offset - current vrl offset, as next vrl offs is 0\r
+ else{\r
+ 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
+ }\r
+\r
+ // Retrive line offsets form .vrs\r
+ vrl_cont->line_offsets = vrs_cont->vrl_line_offsets[counter];\r
+\r
+ return 0;\r
+}\r