Mercurial > hg > dmlib
annotate dmres.h @ 730:3d813c81f33c
More work on resources API.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 21 Apr 2013 19:29:00 +0300 |
parents | 867fc7f205a8 |
children | 0e164aaa7409 |
rev | line source |
---|---|
0 | 1 /* |
2 * DMLib | |
3 * -- Resource management | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
5 * (C) Copyright 2011-2012 Tecnic Software productions (TNSP) | |
6 */ | |
7 #ifndef DMRES_H | |
8 #define DMRES_H | |
9 | |
10 #include "dmlib.h" | |
11 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
12 #ifdef DM_USE_PACKFS |
0 | 13 #include "dmpack.h" |
14 #endif | |
15 | |
16 #ifdef __cplusplus | |
17 extern "C" { | |
18 #endif | |
19 | |
20 | |
21 /* Constants | |
22 */ | |
23 enum | |
24 { | |
25 DMPRUNE_ATIME = 0x0001, | |
26 DMPRUNE_MTIME = 0x0002, | |
27 }; | |
28 | |
29 enum | |
30 { | |
31 DRF_USE_PACK = 0x0001, | |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
32 DRF_USE_STDIO = 0x0002, |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
33 DRF_PRELOAD_RAW = 0x0004, |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
34 DRF_PRELOAD_RES = 0x0008, |
0 | 35 }; |
36 | |
37 enum | |
38 { | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
39 DMF_PERSIST = 0x0001, // Persist loaded RAW resource |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
40 DMF_TEMPORARY = 0x0002, |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
41 DMF_LOADED_RAW = 0x1000, // Raw data has been loaded |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
42 DMF_LOADED_RES = 0x2000, // Resource has been loaded |
0 | 43 }; |
44 | |
45 | |
46 /* Typedefs and structures | |
47 */ | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
48 struct DMResourceLib; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
49 struct DMResourceDataOps; |
0 | 50 struct DMResourceOps; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
51 struct DMResource; |
0 | 52 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
53 typedef struct DMResource |
0 | 54 { |
55 // Timestamps (in seconds from time()) | |
56 int mtime, // When resource was loaded | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
57 atime; // Last accessed (dmResourceRef()/unref) |
0 | 58 int refcount; // Reference count |
59 | |
60 int flags; // Resource flags (DMF_*) | |
61 char *filename; | |
62 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
63 // Raw data (or mem data) |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
64 size_t rawSize; // Size of data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
65 off_t rawOffset; // Current offset in data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
66 Uint8 *rawData; // Pointer to data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
67 |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
68 // Decoded resource data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
69 void *resData; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
70 size_t resSize; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
71 struct DMResourceDataOps *rops; |
0 | 72 |
73 int error; // Error code | |
74 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
75 #ifdef DM_USE_STDIO |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
76 FILE * fh; // File handle for stdio |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
77 #endif |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
78 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
79 struct DMResourceOps *fops; // Pointer to file handling functions struct |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
80 struct DMResourceLib *lib; // Pointer to the resource library |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
81 struct DMResource *next, *prev; |
0 | 82 } DMResource; |
83 | |
84 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
85 typedef struct DMResourceDataOps |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
86 { |
105
d5d27f262227
Beging merging of dmsimple2-fork into the main "engine".
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
87 BOOL (*probe)(DMResource *res, const char *fext); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
88 int (*load)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
89 void (*free)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
90 } DMResourceDataOps; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
91 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
92 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
93 typedef struct DMResourceLib |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
94 { |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
95 int flags; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
96 char *resPath; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
97 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
98 DMResource *resources, *preload; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
99 DMMutex *mutex; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
100 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
101 #ifdef DM_USE_PACKFS |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
102 char *packFilename; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
103 DMPackFile *packFile; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
104 #endif |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
105 } DMResourceLib; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
106 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
107 |
0 | 108 typedef struct DMResourceOps |
109 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
110 char *name; |
0 | 111 int (*ferror)(DMResource *); |
112 int (*fseek)(DMResource *, const off_t, const int); | |
113 off_t (*fsize)(DMResource *); | |
114 off_t (*ftell)(DMResource *); | |
115 BOOL (*feof)(DMResource *); | |
116 int (*fgetc)(DMResource *); | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
117 int (*fputc)(int, DMResource *); |
0 | 118 size_t (*fread)(void *, const size_t, const size_t, DMResource *); |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
119 size_t (*fwrite)(void *, const size_t, const size_t, DMResource *); |
0 | 120 |
121 int (*fopen)(DMResource *); | |
122 void (*fclose)(DMResource *); | |
123 int (*preload)(DMResource *); | |
124 } DMResourceOps; | |
125 | |
126 | |
127 /* Functions | |
128 */ | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
129 int dmResourcesInit(DMResourceLib **lib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *)); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
130 int dmResourcesClose(DMResourceLib *lib); |
0 | 131 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
132 void dmResourcesPrune(DMResourceLib *lib, const int agems, int const flags); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
133 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total); |
0 | 134 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
135 DMResource * dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
136 void dmResourceFree(DMResource *node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
137 void dmResourceInsert(DMResourceLib *lib, DMResource * node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
138 void dmResourceDelete(DMResourceLib *lib, DMResource * node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
139 DMResource * dmResourceFind(DMResourceLib *lib, const char *filename); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
140 int dmResourceRef(DMResource *); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
141 int dmResourceUnref(DMResource *); |
0 | 142 |
143 | |
144 // Opening and closing resources | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
145 int dmf_open(DMResourceLib *lib, const char *, DMResource **handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
146 int dmf_create_memio(DMResourceLib *lib, const char *, Uint8 *buf, size_t len, DMResource **phandle); |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
147 #ifdef DM_USE_STDIO |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
148 int dmf_create_stdio(const char *filename, const char *mode, DMResource **phandle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
149 int dmf_create_stdio_stream(FILE *, DMResource **phandle); |
0 | 150 #endif |
151 void dmf_close(DMResource *); | |
152 | |
153 | |
154 // Basic resource access functions | |
155 int dmferror(DMResource *); | |
156 int dmfseek(DMResource *, const off_t, const int); | |
157 off_t dmfsize(DMResource *); | |
158 off_t dmftell(DMResource *); | |
159 BOOL dmfeof(DMResource *); | |
160 int dmfgetc(DMResource *); | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
161 int dmfputc(int, DMResource *); |
0 | 162 size_t dmfread(void *, const size_t, const size_t, DMResource *); |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
163 size_t dmfwrite(void *, const size_t, const size_t, DMResource *); |
235 | 164 char * dmfgets(char *s, int size, DMResource * f); |
0 | 165 |
166 | |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
167 // Helper functions for endianess based reading etc |
553
b60220fd1669
s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
168 int dmf_read_str(DMResource *, void *, size_t); |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
169 BOOL dmf_read_byte(DMResource *, Uint8 *); |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
170 |
0 | 171 BOOL dmf_read_be16(DMResource *, Uint16 *); |
172 BOOL dmf_read_be32(DMResource *, Uint32 *); | |
173 BOOL dmf_read_le16(DMResource *, Uint16 *); | |
174 BOOL dmf_read_le32(DMResource *, Uint32 *); | |
175 | |
176 #ifdef DM_HAVE_64BIT | |
177 BOOL dmf_read_be64(DMResource *, Uint64 *); | |
178 BOOL dmf_read_le64(DMResource *, Uint64 *); | |
179 #endif | |
180 | |
181 | |
182 #ifdef __cplusplus | |
183 } | |
184 #endif | |
185 | |
186 #endif // DMRES_H |