summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorRay <[email protected]>2021-03-04 12:06:28 +0100
committerRay <[email protected]>2021-03-04 12:06:28 +0100
commitc4a7c702b4d8c1d6cc18b4bfa3f90ae9e53a878d (patch)
tree874dd44398b0ffe06abfff6ab8952eadb7010e15 /src
parentaf3926af4b81fdd72c2b40617675f8de15fa17cc (diff)
downloadraylib-c4a7c702b4d8c1d6cc18b4bfa3f90ae9e53a878d.tar.gz
raylib-c4a7c702b4d8c1d6cc18b4bfa3f90ae9e53a878d.zip
FEATURE: Several callbacks added -WIP- #1523 #1329
NOTE: This feature is still under consideration and not complete.
Diffstat (limited to 'src')
-rw-r--r--src/raylib.h20
-rw-r--r--src/utils.c75
2 files changed, 70 insertions, 25 deletions
diff --git a/src/raylib.h b/src/raylib.h
index 470ffa70..198fbac2 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -876,10 +876,13 @@ typedef enum {
// Callbacks to be implemented by users
typedef void (*TraceLogCallback)(int logType, const char *text, va_list args);
typedef void *(*MemAllocCallback)(int size);
-typedef void *(*MemReallocCallback)(int size);
+typedef void *(*MemReallocCallback)(void *ptr, int size);
typedef void (*MemFreeCallback)(void *ptr);
-typedef unsigned char* (*LoadFileDataCallback)(const char* fileName, unsigned int* bytesRead); // Load file data as byte array (read)
-typedef char* (*LoadFileTextCallback)(const char* fileName); // Load text data from file (read), returns a '\0' terminated string
+typedef unsigned char* (*LoadFileDataCallback)(const char* fileName, unsigned int* bytesRead);
+typedef void (*SaveFileDataCallback)(const char *fileName, void *data, unsigned int bytesToWrite);
+typedef char *(*LoadFileTextCallback)(const char* fileName);
+typedef void (*SaveFileTextCallback)(const char *fileName, char *text);
+
#if defined(__cplusplus)
extern "C" { // Prevents name mangling of functions
@@ -979,15 +982,18 @@ RLAPI void SetConfigFlags(unsigned int flags); // Setup init
RLAPI void TraceLog(int logType, const char *text, ...); // Show trace log messages (LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR)
RLAPI void SetTraceLogLevel(int logType); // Set the current threshold (minimum) log level
RLAPI void *MemAlloc(int size); // Internal memory allocator
+RLAPI void *MemRealloc(void *ptr, int size); // Internal memory reallocator
RLAPI void MemFree(void *ptr); // Internal memory free
-// Set system callbacks
-RLAPI void SetTraceLogCallback(TraceLogCallback callback); // Set a trace log callback to enable custom logging
+// Set custom system callbacks
+RLAPI void SetTraceLogCallback(TraceLogCallback callback); // Set custom trace log
RLAPI void SetMemAllocCallback(MemAllocCallback callback); // Set custom memory allocator
RLAPI void SetMemReallocCallback(MemReallocCallback callback); // Set custom memory reallocator
RLAPI void SetMemFreeCallback(MemFreeCallback callback); // Set custom memory free
-RLAPI void SetLoadFileDataCallback(LoadFileDataCallback callback); // override default file access functions
-RLAPI void SetLoadFileTextCallback(LoadFileDataCallback callback); // override default file access functions
+RLAPI void SetLoadFileDataCallback(LoadFileDataCallback callback); // Set custom file data loader
+RLAPI void SetSaveFileDataCallback(SaveFileDataCallback callback); // Set custom file data saver
+RLAPI void SetLoadFileTextCallback(LoadFileTextCallback callback); // Set custom file text loader
+RLAPI void SetSaveFileTextCallback(SaveFileTextCallback callback); // Set custom file text saver
// Files management functions
RLAPI unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read)
diff --git a/src/utils.c b/src/utils.c
index 42830115..7c9fa41d 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -65,9 +65,29 @@
//----------------------------------------------------------------------------------
// Log types messages
-static int logTypeLevel = LOG_INFO; // Minimum log type level
-static int logTypeExit = LOG_ERROR; // Log type that exits
-static TraceLogCallback logCallback = NULL; // Log callback function pointer
+static int logTypeLevel = LOG_INFO; // Minimum log type level
+
+static TraceLogCallback traceLog = NULL; // TraceLog callback function pointer
+static MemAllocCallback memAlloc = NULL; // MemAlloc callback function pointer
+static MemReallocCallback memRealloc = NULL; // MemRealloc callback funtion pointer
+static MemFreeCallback memFree = NULL; // MemFree callback funtion pointer
+static LoadFileDataCallback loadFileData = NULL; // LoadFileData callback funtion pointer
+static SaveFileDataCallback saveFileData = NULL; // SaveFileText callback funtion pointer
+static LoadFileTextCallback loadFileText = NULL; // LoadFileText callback funtion pointer
+static SaveFileTextCallback saveFileText = NULL; // SaveFileText callback funtion pointer
+
+//void *MemAllocDefault(unsigned int size) { return RL_MALLOC(size); }
+//void MemFreeDefault(void *ptr) { RL_FREE(ptr); }
+
+void SetTraceLogCallback(TraceLogCallback callback) { traceLog = callback; } // Set custom trace log
+void SetMemAllocCallback(MemAllocCallback callback) { memAlloc = callback; } // Set custom memory allocator
+void SetMemReallocCallback(MemReallocCallback callback) { memRealloc = callback; } // Set custom memory reallocator
+void SetMemFreeCallback(MemFreeCallback callback) { memFree = callback; } // Set custom memory free
+void SetLoadFileDataCallback(LoadFileDataCallback callback) { loadFileData = callback; } // Set custom file data loader
+void SetSaveFileDataCallback(SaveFileDataCallback callback) { saveFileData = callback; } // Set custom file data saver
+void SetLoadFileTextCallback(LoadFileTextCallback callback) { loadFileText = callback; } // Set custom file text loader
+void SetSaveFileTextCallback(SaveFileTextCallback callback) { saveFileText = callback; } // Set custom file text saver
+
#if defined(PLATFORM_ANDROID)
static AAssetManager *assetManager = NULL; // Android assets manager pointer
@@ -92,16 +112,7 @@ static int android_close(void *cookie);
//----------------------------------------------------------------------------------
// Set the current threshold (minimum) log level
-void SetTraceLogLevel(int logType)
-{
- logTypeLevel = logType;
-}
-
-// Set a trace log callback to enable custom logging
-void SetTraceLogCallback(TraceLogCallback callback)
-{
- logCallback = callback;
-}
+void SetTraceLogLevel(int logType) { logTypeLevel = logType; }
// Show trace log messages (LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_DEBUG)
void TraceLog(int logType, const char *text, ...)
@@ -113,9 +124,9 @@ void TraceLog(int logType, const char *text, ...)
va_list args;
va_start(args, text);
- if (logCallback)
+ if (traceLog)
{
- logCallback(logType, text, args);
+ traceLog(logType, text, args);
va_end(args);
return;
}
@@ -152,21 +163,37 @@ void TraceLog(int logType, const char *text, ...)
va_end(args);
- if (logType >= logTypeExit) exit(1); // If exit message, exit program
+ if (logType == LOG_ERROR) exit(1); // If error, exit program
#endif // SUPPORT_TRACELOG
}
// Internal memory allocator
+// NOTE: Initializes to zero by default
void *MemAlloc(int size)
{
- return RL_MALLOC(size);
+ // WARNING: This implementation allows changing memAlloc at any
+ // point during program execution, it could be a security risk
+ void *ptr = NULL;
+ if (memAlloc) ptr = memAlloc(size);
+ else ptr = RL_CALLOC(size, 1);
+ return ptr;
+}
+
+// Internal memory reallocator
+void *MemRealloc(void *ptr, int size)
+{
+ void *ret = NULL;
+ if (memRealloc) ret = memRealloc(ptr, size);
+ else ret = RL_REALLOC(ptr, size);
+ return ret;
}
// Internal memory free
void MemFree(void *ptr)
{
- RL_FREE(ptr);
+ if (memFree) memFree(ptr);
+ else RL_FREE(ptr);
}
// Load data from file into a buffer
@@ -177,6 +204,12 @@ unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
if (fileName != NULL)
{
+ if (loadFileData)
+ {
+ data = loadFileData(fileName, bytesRead);
+ return data;
+ }
+
FILE *file = fopen(fileName, "rb");
if (file != NULL)
@@ -222,6 +255,12 @@ bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
if (fileName != NULL)
{
+ if (saveFileData)
+ {
+ saveFileData(fileName, data, bytesToWrite);
+ return success;
+ }
+
FILE *file = fopen(fileName, "wb");
if (file != NULL)