wwww
修訂 | 848c772e64209441c696de3d8101f7b3b81f1354 (tree) |
---|---|
時間 | 2016-07-22 04:01:14 |
作者 | yakui-lover <yakui-lover@yand...> |
Commiter | yakui-lover |
Documented 16_vrs files
Also, wrapped header file in #ifdef
and added a check for invalid vrl id get_vrl_by_id
@@ -1,20 +1,21 @@ | ||
1 | 1 | #include <stdio.h> |
2 | 2 | #include <fcntl.h> |
3 | 3 | #include <ctype.h> |
4 | -#include <errno.h> | |
5 | -#include <assert.h> | |
6 | 4 | #include <stdint.h> |
7 | 5 | #include <stdlib.h> |
8 | -#include <string.h> | |
9 | 6 | #include <unistd.h> |
10 | 7 | |
11 | -#include "stdlib.h" | |
12 | 8 | #include "16_vrs.h" |
13 | 9 | |
10 | +// Read .vrs file into memory | |
14 | 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 | |
15 | 14 | int fd; |
16 | 15 | unsigned long size; |
17 | 16 | unsigned char *buffer; |
17 | + // Open filename, get size of file, | |
18 | + // populate the vrs_container if all tests pass | |
18 | 19 | fd = open(filename, O_RDONLY|O_BINARY); |
19 | 20 | // Insert sanity cheks later |
20 | 21 | size = lseek(fd, 0, SEEK_END); |
@@ -23,25 +24,45 @@ int read_vrs(char *filename, struct vrs_container *vrs_cont){ | ||
23 | 24 | read(fd, buffer, size); |
24 | 25 | vrs_cont->size = size; |
25 | 26 | vrs_cont->buffer = buffer; |
26 | - | |
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; | |
27 | 31 | return 0; |
28 | 32 | } |
29 | 33 | |
34 | +// Seek and return a specified .vrl blob from .vrs blob in memory | |
30 | 35 | struct vrl_container* get_vrl_by_id(struct vrs_container *vrs_cont, uint16_t id){ |
31 | 36 | uint16_t *ids; |
32 | 37 | uint32_t *vrl_list; |
33 | - int counter = 0; | |
34 | 38 | struct vrl_container *vrl_cont; |
39 | + int counter = 0; | |
40 | + // If id is invalid, return null | |
35 | 41 | if(id == 0){ |
42 | + // Probably add an error message? | |
36 | 43 | return 0; |
37 | 44 | } |
45 | + // Get id list from .vrs blob (base + offset) | |
38 | 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) | |
39 | 49 | while(ids[counter] != id && ids[counter]){ |
40 | 50 | counter++; |
41 | 51 | } |
52 | + // Return null if we couldn't find the requested id | |
53 | + if(!ids[counter]){ | |
54 | + // Error message? | |
55 | + return 0; | |
56 | + } | |
57 | + // Get vrl list from .vrs blob (base + offset) | |
42 | 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 | |
43 | 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) | |
44 | 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 | |
45 | 66 | vrl_cont->size = vrl_list[counter+1] - vrl_list[counter]; |
46 | 67 | return vrl_cont; |
47 | 68 | } |
@@ -1,14 +1,32 @@ | ||
1 | +#ifndef __16_VRS__ | |
2 | +#define __16_VRS__ | |
3 | + | |
4 | +#include <stdint.h> | |
1 | 5 | #include "hw/vga/vrl.h" |
2 | 6 | #include "hw/vga/vrs.h" |
3 | 7 | |
8 | +// Container for .vrs files loaded in memory with useful info | |
9 | +// Includes: | |
10 | +// + size of the .vrs blob in memory | |
11 | +// + pointer to the blob/vrs header | |
12 | +// + id of the curent (shown) animation | |
13 | +// + id of the first sprite of the curret animation (supplementary) | |
14 | +// + id of the current (shown) sprite | |
15 | + | |
4 | 16 | struct vrs_container{ |
5 | 17 | unsigned long size; |
6 | 18 | union{ |
7 | 19 | unsigned char *buffer; |
8 | 20 | struct vrs_header *vrs_hdr; |
9 | 21 | }; |
22 | + uint16_t anchor_sprite_id; | |
23 | + uint16_t current_sprite_id; | |
10 | 24 | }; |
11 | 25 | |
26 | +// Container for .vrl files loaded in memory with useful info | |
27 | +// Includes: | |
28 | +// + size of the .vrl blob in memory | |
29 | +// + pointer to the blob/vrl header | |
12 | 30 | struct vrl_container{ |
13 | 31 | unsigned long size; |
14 | 32 | union{ |
@@ -17,5 +35,23 @@ struct vrl_container{ | ||
17 | 35 | }; |
18 | 36 | }; |
19 | 37 | |
38 | +// Read .vrs file into memory | |
39 | +// In: | |
40 | +// + char *filename - name of the file to load | |
41 | +// + struct vrs_container *vrs_cont - pointer to the vrs_container | |
42 | +// to load the file into | |
43 | +// Out: | |
44 | +// + int - 0 on succes, 1 on failure | |
20 | 45 | int read_vrs(char *filename, struct vrs_container *vrs_cont); |
46 | + | |
47 | +// Seek and return a specified .vrl blob from .vrs blob in memory | |
48 | +// In: | |
49 | +// + struct vrs_container *vrs_cont - pointer to the vrs_container | |
50 | +// with a loaded .vrs file | |
51 | +// + uint16_t id - id of the vrl to retrive | |
52 | +// Out: | |
53 | +// struct vrl_container* - a pointer to a vrl_container with a pointer | |
54 | +// to the requested .vrl blob | |
21 | 55 | struct vrl_container* get_vrl_by_id(struct vrs_container *vrs_cont, uint16_t id); |
56 | + | |
57 | +#endif |