]> git.bts.cx Git - benzene.git/blob - src_platform/sdl/bz/resources/resource.c
Sprites
[benzene.git] / src_platform / sdl / bz / resources / resource.c
1 #include <bz/resources/resource.h>
2
3 #include <bz/types/identifier_internal.h>
4 #include <stb_sprintf.h>
5 #include <physfs.h>
6 #include <stdlib.h>
7
8 struct BZResource { PHYSFS_File unused; };
9
10 // This is FOR DEBUG ONLY!
11 char *gBZDataDebugDirectory = NULL;
12
13 void bzFileSystemInit(const char *argv0, const char *dataPath) {
14 PHYSFS_init(argv0);
15
16 if (gBZDataDebugDirectory != NULL) {
17 bzLog("!!! A debug data directory was found !!!");
18 bzLog("!!! DO NOT SHIP IF THIS IS DISPLAYED !!!");
19 int fsr3 = PHYSFS_mount(gBZDataDebugDirectory, NULL, 0);
20 bzAssert(fsr3 != 0);
21 bzLog("Trying to load: %s", gBZDataDebugDirectory);
22 } else {
23 char tmp[1024];
24 stbsp_snprintf(tmp, 1024, "%s%s", dataPath, "assets.pak");
25 int fsr2 = PHYSFS_mount(tmp, NULL, 0);
26 bzAssert(fsr2 != 0);
27 bzLog("Trying to load: %s", tmp);
28 }
29 }
30
31 void bzFileSystemTeardown() {
32 PHYSFS_deinit();
33 }
34
35 BZResourceID bzResourcesOpenResource(const char *type, const char *identifierFmt, ...) {
36 bzMakeIdentifier(identifier, identifierFmt);
37 bzLog("Trying to load: %s", identifier);
38
39 BZResourceID resource = (BZResourceID)PHYSFS_openRead(identifier);
40 bzAssertMessage(resource != NULL, "Error opening resource '%s': %s", identifier, PHYSFS_getLastError());
41
42 return resource;
43 }
44
45 extern void bzResourcesCloseResource(BZResourceID resource) {
46 PHYSFS_File *handle = (PHYSFS_File *)resource;
47 PHYSFS_close(handle);
48 }
49
50 size_t bzResourcesFileLength(BZResourceID resource) {
51 PHYSFS_File *handle = (PHYSFS_File *)resource;
52 return PHYSFS_fileLength(handle);
53 }
54
55 size_t bzResourcesTell(BZResourceID resource) {
56 PHYSFS_File *handle = (PHYSFS_File *)resource;
57 return PHYSFS_tell(handle);
58 }
59
60 size_t bzResourcesSeek(BZResourceID resource, size_t position) {
61 PHYSFS_File *handle = (PHYSFS_File *)resource;
62 return PHYSFS_seek(handle, position);
63 }
64
65 size_t bzResourcesReadBytes(BZResourceID resource, void *outputBuffer, size_t numBytes) {
66 PHYSFS_File *handle = (PHYSFS_File *)resource;
67 PHYSFS_sint64 r = PHYSFS_readBytes(handle, outputBuffer, numBytes);
68 if (r == -1) {
69 // FIXME, handle error?
70 return 0;
71 } else {
72 return (size_t)r;
73 }
74 }