]> 4ch.mooo.com Git - 16.git/blobdiff - src/lib/16_vrs.c
Merge branch 'master' of https://github.com/sparky4/16
[16.git] / src / lib / 16_vrs.c
index b3cd9eb74e7c870c49904926d2bb572e2601eec8..5dc525e15d39c237e6783525de42f6538eed733b 100755 (executable)
@@ -36,7 +36,7 @@ int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container
        byte huge *buffer;
        vrl1_vgax_offset_t **vrl_line_offsets;
        uint32_t huge *vrl_headers_offsets;
-       uint32_t huge *vrl_id_iter;
+       uint16_t huge *vrl_id_iter;
        uint32_t vrl_size;
        int num_of_vrl, i;
        struct vrl1_vgax_header huge *curr_vrl;
@@ -56,18 +56,18 @@ int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container
                fprintf(stderr, "Unablee to load file");
                exit(3);
        }
-       vrs_cont->size = size;
+       vrs_cont->data_size = size - sizeof(struct vrs_header);
        vrs_cont->buffer = buffer;
 
        // Calculate vrl offsets
        
        // Count sprites
-       vrl_id_iter = (uint32_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);
+       vrl_id_iter = (uint16_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);
        while(vrl_id_iter[num_of_vrl]){
                num_of_vrl++;
        }
        // Allocate memory for vrl line offsets table
-       vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t)*num_of_vrl);
+       vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t *)*num_of_vrl);
 
        vrl_headers_offsets = (uint32_t huge *)(buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
        // Calculate line offsets for each vrl
@@ -76,68 +76,64 @@ int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container
 
                // Calc. vrl size as (next_offset - curr_offset)
                if (i != num_of_vrl - 1){
-                       vrl_size = vrl_headers_offsets[i+1] - vrl_headers_offsets[i];
+                       vrl_size = vrl_headers_offsets[i+1] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);
                }
                // If it's the last vrl, size is (next_vrs_struct_offset - curr_offset)
                else{
-                       vrl_size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i];
+                       vrl_size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);
                }
-               vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(*curr_vrl), vrl_size - sizeof(*curr_vrl));
+               vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(struct vrl1_vgax_header), vrl_size);
        }
        vrs_cont->vrl_line_offsets = vrl_line_offsets;
        return 0;
 }
 
 // Seek and return a specified .vrl blob from .vrs blob in far memory
-struct vrl_container * get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint16_t id){
+int get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint16_t id, struct vrl_container *vrl_cont){
        uint16_t huge *ids;
        uint32_t huge *vrl_offs_list;
-       struct vrl_container huge *vrl_cont;
        int counter = 0;
 
-       // If id is invalid, return null
+       // If id is invalid, return -1
        if(id == 0){
                // Probably add an error message?
-               return 0;
+               return -1;
        }
 
        // Get id list from .vrs blob (base + offset)
-       ids = (uint16_t huge*)vrs_cont->buffer +
-               (dword)vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST];
+       ids = (uint16_t huge*)(vrs_cont->buffer + 
+               vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);
 
        // Loop through the id list until we found the right one or hit the end of the list
        // Counter is keeping track of the offset(in ids/vrl blobs)
        while(ids[counter] != id && ids[counter]){
                counter++;
        }
-       // Return null if we couldn't find the requested id
+       // Return -2 if we couldn't find the requested id
        if(!ids[counter]){
                // Error message?
-               return 0;
+               return -2;
        }
 
        // Get vrl offsets list from .vrs blob (base + offset)
        vrl_offs_list = (uint32_t huge *)(vrs_cont->buffer +
                                        vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
 
-       // Allocate memory for vrl_cont
-       vrl_cont = (struct vrl_container *)malloc(sizeof(struct vrl_container));
-
        // Get vrl_header from .vrs (base + offset from vrl_list)
        // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)
        vrl_cont->vrl_header = (struct vrl1_vgax_header huge *)(vrs_cont->buffer + vrl_offs_list[counter]);
 
        // Get .vrl size by integer arithmetics (next vrl offset - current vrl offset)
        if(ids[counter+1]){
-               vrl_cont->size = vrl_offs_list[counter+1] - vrl_offs_list[counter];
+               vrl_cont->data_size = vrl_offs_list[counter+1] - vrl_offs_list[counter] - sizeof(struct vrl1_vgax_header);
        }
        // If we are retriving the last vrl, size is ids_list offset - current vrl offset, as next vrl offs is 0
        else{
-               vrl_cont->size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_offs_list[counter];
+               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);
        }
 
        // Retrive line offsets form .vrs
        vrl_cont->line_offsets = vrs_cont->vrl_line_offsets[counter];
 
-       return vrl_cont;
+       return 0;
 }