Last active
October 10, 2024 23:52
-
-
Save gilzoide/2066ddc9d0dacd5677e9f7377aca3062 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <raylib.h> | |
#include <physfs.h> | |
#include "raylib-physfs.h" | |
static unsigned char *LoadPhysfsFileData(const char *fileName, int *dataSize) { | |
PHYSFS_File *file = PHYSFS_openRead(fileName); | |
unsigned char *buffer = NULL; | |
if (file) { | |
PHYSFS_sint64 filesize = PHYSFS_fileLength(file); | |
buffer = (unsigned char *) RL_MALLOC(filesize); | |
if (buffer) { | |
*dataSize = PHYSFS_readBytes(file, buffer, filesize); | |
} | |
PHYSFS_close(file); | |
} | |
return buffer; | |
} | |
static bool SavePhysfsFileData(const char *fileName, void *data, int dataSize) { | |
PHYSFS_File *file = PHYSFS_openWrite(fileName); | |
PHYSFS_sint64 writtenBytes = 0; | |
if (file) { | |
writtenBytes = PHYSFS_writeBytes(file, data, dataSize); | |
PHYSFS_close(file); | |
} | |
return writtenBytes == dataSize; | |
} | |
static char *LoadPhysfsFileText(const char *fileName) { | |
PHYSFS_File *file = PHYSFS_openRead(fileName); | |
char *buffer = NULL; | |
if (file) { | |
PHYSFS_sint64 filesize = PHYSFS_fileLength(file); | |
buffer = (char *) RL_MALLOC(filesize + 1); | |
if (buffer) { | |
PHYSFS_sint64 readBytes = PHYSFS_readBytes(file, buffer, filesize); | |
if (readBytes > 0) { | |
buffer[readBytes] = '\0'; | |
} | |
else { | |
RL_FREE(buffer); | |
buffer = NULL; | |
} | |
} | |
PHYSFS_close(file); | |
} | |
return nullptr; | |
} | |
static bool SavePhysfsFileText(const char *fileName, char *text) { | |
PHYSFS_File *file = PHYSFS_openWrite(fileName); | |
bool is_success = true; | |
if (file) { | |
is_success = false; | |
for (const char *c = text; *c; c++) { | |
PHYSFS_sint64 writtenBytes = PHYSFS_writeBytes(file, c, 1); | |
if (writtenBytes <= 0) { | |
is_success = false; | |
break; | |
} | |
} | |
PHYSFS_close(file); | |
} | |
return is_success; | |
} | |
void SetupRaylibPhysfs() { | |
SetLoadFileDataCallback(LoadPhysfsFileData); | |
SetSaveFileDataCallback(SavePhysfsFileData); | |
SetLoadFileTextCallback(LoadPhysfsFileText); | |
SetSaveFileTextCallback(SavePhysfsFileText); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
/** | |
* Setup Raylib file callbacks using Physfs API | |
* | |
* After calling this, Raylib functions like "LoadTexture" and "LoadModel" | |
* will use Physfs API to read/write files. | |
*/ | |
void SetupRaylibPhysfs(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment