FEATURE: Several callbacks added -WIP- #1523 #1329

NOTE: This feature is still under consideration and not complete.
This commit is contained in:
Ray 2021-03-04 12:06:28 +01:00
parent af3926af4b
commit c4a7c702b4
2 changed files with 70 additions and 25 deletions

View file

@ -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)