10 // Read .vrs file into memory
11 int read_vrs(char *filename, struct vrs_container *vrs_cont){
12 // Initialise a local copy of becessary variables
13 // so vrs_cont won't be dirty on error
16 unsigned char *buffer;
17 // Open filename, get size of file,
18 // populate the vrs_container if all tests pass
19 fd = open(filename, O_RDONLY|O_BINARY);
20 // Insert sanity cheks later
21 size = lseek(fd, 0, SEEK_END);
22 buffer = malloc(size);
23 lseek(fd, 0, SEEK_SET);
24 read(fd, buffer, size);
25 vrs_cont->size = size;
26 vrs_cont->buffer = buffer;
27 // 0 is an invalid value for ids under vrs specifications,
28 // so it is safe to flush ids to this value
29 vrs_cont->anchor_sprite_id = 0;
30 vrs_cont->current_sprite_id = 0;
34 // Seek and return a specified .vrl blob from .vrs blob in memory
35 struct vrl_container* get_vrl_by_id(struct vrs_container *vrs_cont, uint16_t id){
38 struct vrl_container *vrl_cont;
40 // If id is invalid, return null
42 // Probably add an error message?
45 // Get id list from .vrs blob (base + offset)
46 ids = (uint16_t*)vrs_cont->buffer + (unsigned long)vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST];
47 // Loop through the id list until we found the right one or hit the end of the list
48 // Counter is keeping track of the offset(in ids/vrl blobs)
49 while(ids[counter] != id && ids[counter]){
52 // Return null if we couldn't find the requested id
57 // Get vrl list from .vrs blob (base + offset)
58 vrl_list = (uint32_t *)(vrs_cont->buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
59 // Allocate memory for vrl_cont
60 vrl_cont = (struct vrl_container*)malloc(sizeof(struct vrl_container));
61 // Get vrl_header from .vrs (base + offset from vrl_list)
62 // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification)
63 vrl_cont->vrl_header = (struct vrl1_vgax_header*)(vrs_cont->buffer + vrl_list[counter]);
64 // Get .vrl size by integer arithmetics (next vrl - current vrl)
65 // Untested. May be an incorrect way to do so
66 vrl_cont->size = vrl_list[counter+1] - vrl_list[counter];