Mercurial > hg > dmlib
annotate src/dmres.h @ 2293:587b26cecf5b
Add read/write RAW palette function prototypes to public header.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 03 Jul 2019 09:01:47 +0300 |
parents | e69de47d2419 |
children | 43e39d9ec42f |
rev | line source |
---|---|
0 | 1 /* |
2 * DMLib | |
3 * -- Resource management | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
863 | 5 * (C) Copyright 2011-2015 Tecnic Software productions (TNSP) |
0 | 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 { | |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
39 DMF_PERSIST = 0x0001, // Persist loaded RAW resource |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
40 DMF_COMPRESSED = 0x0002, // Resource is compressed in PACK file, otherwise raw |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
41 DMF_TEMPORARY = 0x0004, |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
42 DMF_PACK_MASK = 0x00ff, // Mask for flags that may be specified in PACK |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
43 |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
44 DMF_UNALLOCATED = 0x1000, // The raw data is not allocated, so do not free it |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
45 DMF_LOADED_RAW = 0x2000, // Raw data has been loaded |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
46 DMF_LOADED_RES = 0x4000, // Resource has been loaded |
0 | 47 }; |
48 | |
49 | |
50 /* Typedefs and structures | |
51 */ | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
52 struct DMResourceLib; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
53 struct DMResourceDataOps; |
0 | 54 struct DMResourceOps; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
55 struct DMResource; |
0 | 56 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
57 typedef struct DMResource |
0 | 58 { |
59 // Timestamps (in seconds from time()) | |
60 int mtime, // When resource was loaded | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
61 atime; // Last accessed (dmResourceRef()/unref) |
0 | 62 int refcount; // Reference count |
63 | |
64 int flags; // Resource flags (DMF_*) | |
65 char *filename; | |
66 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
67 // Raw data (or mem data) |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
68 size_t rawSize; // Size of data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
69 off_t rawOffset; // Current offset in data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
70 Uint8 *rawData; // Pointer to data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
71 |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
72 // Decoded resource data |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
73 void *resData; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
74 size_t resSize; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
75 struct DMResourceDataOps *rops; |
0 | 76 |
77 int error; // Error code | |
78 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
79 #ifdef DM_USE_STDIO |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
80 FILE * fh; // File handle for stdio |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
81 #endif |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
82 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
83 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
|
84 struct DMResourceLib *lib; // Pointer to the resource library |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
85 struct DMResource *next, *prev; |
0 | 86 } DMResource; |
87 | |
88 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
89 typedef struct DMResourceDataOps |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
90 { |
105
d5d27f262227
Beging merging of dmsimple2-fork into the main "engine".
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
91 BOOL (*probe)(DMResource *res, const char *fext); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
92 int (*load)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
93 void (*free)(DMResource *res); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
94 } DMResourceDataOps; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
95 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
96 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
97 typedef struct DMResourceLib |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
98 { |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
99 int flags; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
100 char *resPath; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
101 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
102 DMResource *resources, *preload; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
103 DMMutex *mutex; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
104 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
105 #ifdef DM_USE_PACKFS |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
106 char *packFilename; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
107 DMPackFile *packFile; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
108 #endif |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
109 } DMResourceLib; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
110 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
111 |
0 | 112 typedef struct DMResourceOps |
113 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
114 char *name; |
732 | 115 |
116 int (*freset)(DMResource *); | |
0 | 117 int (*ferror)(DMResource *); |
1106
f8e9f6b2a41a
Hmm .. back out the Sint64 changes for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
118 int (*fseek)(DMResource *, const off_t, const int); |
0 | 119 off_t (*fsize)(DMResource *); |
1106
f8e9f6b2a41a
Hmm .. back out the Sint64 changes for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
120 off_t (*ftell)(DMResource *); |
0 | 121 BOOL (*feof)(DMResource *); |
122 int (*fgetc)(DMResource *); | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
123 int (*fputc)(int, DMResource *); |
0 | 124 size_t (*fread)(void *, const size_t, const size_t, DMResource *); |
1086 | 125 size_t (*fwrite)(const void *, const size_t, const size_t, DMResource *); |
0 | 126 |
127 int (*fopen)(DMResource *); | |
128 void (*fclose)(DMResource *); | |
129 int (*preload)(DMResource *); | |
130 } DMResourceOps; | |
131 | |
132 | |
133 /* Functions | |
134 */ | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
135 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
|
136 int dmResourcesClose(DMResourceLib *lib); |
0 | 137 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
138 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
|
139 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total); |
0 | 140 |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
141 DMResource * dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size, const int flags); |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
142 void dmResourceFree(DMResource *node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
143 void dmResourceInsert(DMResourceLib *lib, DMResource * node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
144 void dmResourceDelete(DMResourceLib *lib, DMResource * node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
145 DMResource * dmResourceFind(DMResourceLib *lib, const char *filename); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
146 int dmResourceRef(DMResource *); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
625
diff
changeset
|
147 int dmResourceUnref(DMResource *); |
0 | 148 |
149 | |
150 // Opening and closing resources | |
1607 | 151 int dmf_open(DMResourceLib *lib, const char *, DMResource **phandle); |
152 void dmf_close(DMResource *fh); | |
914 | 153 |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1106
diff
changeset
|
154 int dmf_open_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
|
155 #ifdef DM_USE_STDIO |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1106
diff
changeset
|
156 int dmf_open_stdio(const char *filename, const char *mode, DMResource **phandle); |
1607 | 157 int dmf_open_stdio_stream(FILE *fh, DMResource **phandle); |
0 | 158 #endif |
159 | |
160 | |
161 // Basic resource access functions | |
1607 | 162 int dmfreset(DMResource *fh); |
163 int dmferror(DMResource *fh); | |
164 int dmfseek(DMResource *fh, const off_t offset, const int whence); | |
165 off_t dmfsize(DMResource *fh); | |
166 off_t dmftell(DMResource *fh); | |
167 BOOL dmfeof(DMResource *fh); | |
168 int dmfgetc(DMResource *fh); | |
169 int dmfputc(int val, DMResource *fh); | |
170 size_t dmfread(void *ptr, const size_t size, const size_t nmemb, DMResource *fh); | |
171 size_t dmfwrite(const void *ptr, const size_t size, const size_t nmemb, DMResource *fh); | |
172 char * dmfgets(char *str, const int size, DMResource *fh); | |
1608
7f9fe2a9a87e
Implement dmfputs() in DMResource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
1607
diff
changeset
|
173 int dmfputs(const char *str, DMResource *fh); |
1885
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
174 int dmvfprintf(DMResource *fh, const char *fmt, va_list ap); |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
175 int dmfprintf(DMResource *fh, const char *fmt, ...); |
0 | 176 |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
177 // Helper functions for endianess based reading etc |
1607 | 178 BOOL dmf_read_str(DMResource *fh, void *ptr, const size_t len); |
179 BOOL dmf_read_byte(DMResource *fh, Uint8 *val); | |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
180 |
1088
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
181 #define DM_DEFINE_FFUNC(xname, xtype, z) \ |
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
182 BOOL dmf_read_ ## xname (DMResource *fh, xtype *v); |
872
b01d04e44b6f
Assume we always have 64bit type.
Matti Hamalainen <ccr@tnsp.org>
parents:
863
diff
changeset
|
183 |
1088
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
184 #include "dmfiletmpl.h" |
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
185 |
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
186 #undef DM_DEFINE_FFUNC |
0 | 187 |
1881
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
188 BOOL dmf_write_str(DMResource *fh, const void *data, const size_t len); |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
189 BOOL dmf_write_byte(DMResource *fh, const Uint8 val); |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
190 |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
191 #define DM_DEFINE_FFUNC(xname, xtype, z) \ |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
192 BOOL dmf_write_ ## xname (DMResource *fh, const xtype v); |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
193 |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
194 #include "dmfiletmpl.h" |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
195 |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
196 #undef DM_DEFINE_FFUNC |
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
197 |
0 | 198 |
199 #ifdef __cplusplus | |
200 } | |
201 #endif | |
202 #endif // DMRES_H |