summaryrefslogtreecommitdiffhomepage
path: root/src/utils.c
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/utils.c
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/utils.c')
-rw-r--r--src/utils.c75
1 files changed, 57 insertions, 18 deletions
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)