summaryrefslogtreecommitdiff
path: root/libs/raylib/src/utils.c
diff options
context:
space:
mode:
authorLuca Sas <sas.luca.alex@gmail.com>2020-10-01 19:17:58 +0100
committerLuca Sas <sas.luca.alex@gmail.com>2020-10-01 19:17:58 +0100
commit45ee5878397822c41b9109d0e3bc2d635c8c8634 (patch)
treee94f7a4c3d7c94760ff5b96886a2632f0d3d5f6b /libs/raylib/src/utils.c
parent224c1c2087195c48d4a116ba9db0faa209e50332 (diff)
downloadgamejam-slgj-2024-45ee5878397822c41b9109d0e3bc2d635c8c8634.tar.gz
gamejam-slgj-2024-45ee5878397822c41b9109d0e3bc2d635c8c8634.tar.bz2
gamejam-slgj-2024-45ee5878397822c41b9109d0e3bc2d635c8c8634.zip
Updated to raylib 3.0
Diffstat (limited to 'libs/raylib/src/utils.c')
-rw-r--r--libs/raylib/src/utils.c168
1 files changed, 154 insertions, 14 deletions
diff --git a/libs/raylib/src/utils.c b/libs/raylib/src/utils.c
index 55b85cb..5ba8b00 100644
--- a/libs/raylib/src/utils.c
+++ b/libs/raylib/src/utils.c
@@ -46,8 +46,8 @@
#endif
#include <stdlib.h> // Required for: exit()
-#include <stdio.h> // Required for: printf(), sprintf()
-#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
+#include <stdio.h> // Required for: vprintf()
+#include <stdarg.h> // Required for: va_list, va_start(), va_end()
#include <string.h> // Required for: strcpy(), strcat()
#define MAX_TRACELOG_BUFFER_SIZE 128 // Max length of one trace-log message
@@ -64,7 +64,8 @@ static int logTypeExit = LOG_ERROR; // Log type that exits
static TraceLogCallback logCallback = NULL; // Log callback function pointer
#if defined(PLATFORM_ANDROID)
-static AAssetManager *assetManager = NULL; // Android assets manager pointer
+static AAssetManager *assetManager = NULL; // Android assets manager pointer
+static const char *internalDataPath = NULL; // Android internal data path
#endif
#if defined(PLATFORM_UWP)
@@ -78,7 +79,6 @@ static UWPMessage *UWPInMessages[MAX_UWP_MESSAGES]; // Messages in from UWP
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
#if defined(PLATFORM_ANDROID)
-// This should be in <stdio.h>, but Travis does not find it...
FILE *funopen(const void *cookie, int (*readfn)(void *, char *, int), int (*writefn)(void *, const char *, int),
fpos_t (*seekfn)(void *, fpos_t, int), int (*closefn)(void *));
@@ -164,23 +164,163 @@ void TraceLog(int logType, const char *text, ...)
#endif // SUPPORT_TRACELOG
}
+// Load data from file into a buffer
+unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
+{
+ unsigned char *data = NULL;
+ *bytesRead = 0;
+
+ if (fileName != NULL)
+ {
+ FILE *file = fopen(fileName, "rb");
+
+ if (file != NULL)
+ {
+ // WARNING: On binary streams SEEK_END could not be found,
+ // using fseek() and ftell() could not work in some (rare) cases
+ fseek(file, 0, SEEK_END);
+ int size = ftell(file);
+ fseek(file, 0, SEEK_SET);
+
+ if (size > 0)
+ {
+ data = (unsigned char *)RL_MALLOC(sizeof(unsigned char)*size);
+
+ // NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
+ unsigned int count = fread(data, sizeof(unsigned char), size, file);
+ *bytesRead = count;
+
+ if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName);
+ else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
+ }
+ else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
+
+ fclose(file);
+ }
+ else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
+ }
+ else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
+
+ return data;
+}
+
+// Save data to file from buffer
+void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
+{
+ if (fileName != NULL)
+ {
+ FILE *file = fopen(fileName, "wb");
+
+ if (file != NULL)
+ {
+ unsigned int count = fwrite(data, sizeof(unsigned char), bytesToWrite, file);
+
+ if (count == 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write file", fileName);
+ else if (count != bytesToWrite) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName);
+ else TRACELOG(LOG_INFO, "FILEIO: [%s] File saved successfully", fileName);
+
+ fclose(file);
+ }
+ else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
+ }
+ else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
+}
+
+// Load text data from file, returns a '\0' terminated string
+// NOTE: text chars array should be freed manually
+char *LoadFileText(const char *fileName)
+{
+ char *text = NULL;
+
+ if (fileName != NULL)
+ {
+ FILE *textFile = fopen(fileName, "rt");
+
+ if (textFile != NULL)
+ {
+ // WARNING: When reading a file as 'text' file,
+ // text mode causes carriage return-linefeed translation...
+ // ...but using fseek() should return correct byte-offset
+ fseek(textFile, 0, SEEK_END);
+ int size = ftell(textFile);
+ fseek(textFile, 0, SEEK_SET);
+
+ if (size > 0)
+ {
+ text = (char *)RL_MALLOC(sizeof(char)*(size + 1));
+ int count = fread(text, sizeof(char), size, textFile);
+
+ // WARNING: \r\n is converted to \n on reading, so,
+ // read bytes count gets reduced by the number of lines
+ if (count < size) text = RL_REALLOC(text, count + 1);
+
+ // Zero-terminate the string
+ text[count] = '\0';
+
+ TRACELOG(LOG_INFO, "FILEIO: [%s] Text file loaded successfully", fileName);
+ }
+ else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read text file", fileName);
+
+ fclose(textFile);
+ }
+ else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
+ }
+ else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
+
+ return text;
+}
+
+// Save text data to file (write), string must be '\0' terminated
+void SaveFileText(const char *fileName, char *text)
+{
+ if (fileName != NULL)
+ {
+ FILE *file = fopen(fileName, "wt");
+
+ if (file != NULL)
+ {
+ int count = fprintf(file, "%s", text);
+
+ if (count == 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write text file", fileName);
+ else TRACELOG(LOG_INFO, "FILEIO: [%s] Text file saved successfully", fileName);
+
+ fclose(file);
+ }
+ else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
+ }
+ else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
+}
+
#if defined(PLATFORM_ANDROID)
// Initialize asset manager from android app
-void InitAssetManager(AAssetManager *manager)
+void InitAssetManager(AAssetManager *manager, const char *dataPath)
{
assetManager = manager;
+ internalDataPath = dataPath;
}
// Replacement for fopen
+// Ref: https://developer.android.com/ndk/reference/group/asset
FILE *android_fopen(const char *fileName, const char *mode)
{
- if (mode[0] == 'w') return NULL;
-
- AAsset *asset = AAssetManager_open(assetManager, fileName, 0);
-
- if (!asset) return NULL;
+ if (mode[0] == 'w') // TODO: Test!
+ {
+ // TODO: fopen() is mapped to android_fopen() that only grants read access
+ // to assets directory through AAssetManager but we want to also be able to
+ // write data when required using the standard stdio FILE access functions
+ // Ref: https://stackoverflow.com/questions/11294487/android-writing-saving-files-from-native-code-only
+ #undef fopen
+ return fopen(TextFormat("%s/%s", internalDataPath, fileName), mode);
+ #define fopen(name, mode) android_fopen(name, mode)
+ }
+ else
+ {
+ // NOTE: AAsset provides access to read-only asset
+ AAsset *asset = AAssetManager_open(assetManager, fileName, AASSET_MODE_UNKNOWN);
- return funopen(asset, android_read, android_write, android_seek, android_close);
+ if (asset != NULL) return funopen(asset, android_read, android_write, android_seek, android_close);
+ else return NULL;
+ }
}
#endif // PLATFORM_ANDROID
@@ -195,7 +335,7 @@ static int android_read(void *cookie, char *buf, int size)
static int android_write(void *cookie, const char *buf, int size)
{
- TraceLog(LOG_ERROR, "Can't provide write access to the APK");
+ TRACELOG(LOG_WARNING, "ANDROID: Failed to provide write access to APK");
return EACCES;
}
@@ -252,7 +392,7 @@ void UWPSendMessage(UWPMessage *msg)
UWPInMessageId++;
UWPInMessages[UWPInMessageId] = msg;
}
- else TraceLog(LOG_WARNING, "[UWP Messaging] Not enough array space to register new UWP inbound Message.");
+ else TRACELOG(LOG_WARNING, "UWP: Not enough array space to register new inbound message");
}
void SendMessageToUWP(UWPMessage *msg)
@@ -262,7 +402,7 @@ void SendMessageToUWP(UWPMessage *msg)
UWPOutMessageId++;
UWPOutMessages[UWPOutMessageId] = msg;
}
- else TraceLog(LOG_WARNING, "[UWP Messaging] Not enough array space to register new UWP outward Message.");
+ else TRACELOG(LOG_WARNING, "UWP: Not enough array space to register new outward message");
}
bool HasMessageFromUWP(void)