diff --git a/src/raylib.h b/src/raylib.h index 7919db775..4ae78c619 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -950,11 +950,11 @@ typedef enum { // Callbacks to hook some internal functions // WARNING: These callbacks are intended for advanced users -typedef void (*TraceLogCallback)(int logLevel, const char *text, va_list args); // Logging: Redirect trace log messages -typedef unsigned char *(*LoadFileDataCallback)(const char *fileName, int *dataSize); // FileIO: Load binary data -typedef bool (*SaveFileDataCallback)(const char *fileName, void *data, int dataSize); // FileIO: Save binary data -typedef char *(*LoadFileTextCallback)(const char *fileName); // FileIO: Load text data -typedef bool (*SaveFileTextCallback)(const char *fileName, char *text); // FileIO: Save text data +typedef void (*TraceLogCallback)(void* userData, int logLevel, const char *text, va_list args); // Logging: Redirect trace log messages +typedef unsigned char *(*LoadFileDataCallback)(void* userData, const char *fileName, int *dataSize); // FileIO: Load binary data +typedef bool (*SaveFileDataCallback)(void* userData, const char *fileName, void *data, int dataSize); // FileIO: Save binary data +typedef char *(*LoadFileTextCallback)(void* userData, const char *fileName); // FileIO: Load text data +typedef bool (*SaveFileTextCallback)(void* userData, const char *fileName, char *text); // FileIO: Save text data //------------------------------------------------------------------------------------ // Global Variables Definition @@ -1110,11 +1110,11 @@ RLAPI void MemFree(void *ptr); // Internal me // Set custom callbacks // WARNING: Callbacks setup is intended for advanced users -RLAPI void SetTraceLogCallback(TraceLogCallback callback); // Set custom trace log -RLAPI void SetLoadFileDataCallback(LoadFileDataCallback callback); // Set custom file binary data loader -RLAPI void SetSaveFileDataCallback(SaveFileDataCallback callback); // Set custom file binary data saver -RLAPI void SetLoadFileTextCallback(LoadFileTextCallback callback); // Set custom file text data loader -RLAPI void SetSaveFileTextCallback(SaveFileTextCallback callback); // Set custom file text data saver +RLAPI void SetTraceLogCallback(void* userData, TraceLogCallback callback); // Set custom trace log +RLAPI void SetLoadFileDataCallback(void* userData, LoadFileDataCallback callback); // Set custom file binary data loader +RLAPI void SetSaveFileDataCallback(void* userData, SaveFileDataCallback callback); // Set custom file binary data saver +RLAPI void SetLoadFileTextCallback(void* userData, LoadFileTextCallback callback); // Set custom file text data loader +RLAPI void SetSaveFileTextCallback(void* userData, SaveFileTextCallback callback); // Set custom file text data saver // Files management functions RLAPI unsigned char *LoadFileData(const char *fileName, int *dataSize); // Load file data as byte array (read) diff --git a/src/utils.c b/src/utils.c index 5c189845b..8a92f116e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -67,14 +67,20 @@ static SaveFileDataCallback saveFileData = NULL; // SaveFileText callback fun static LoadFileTextCallback loadFileText = NULL; // LoadFileText callback function pointer static SaveFileTextCallback saveFileText = NULL; // SaveFileText callback function pointer +static void* traceLogUser = NULL; // TraceLog callback user-data pointer +static void* loadFileDataUser = NULL; // LoadFileData callback user-data pointer +static void* saveFileDataUser = NULL; // SaveFileText callback user-data pointer +static void* loadFileTextUser = NULL; // LoadFileText callback user-data pointer +static void* saveFileTextUser = NULL; // SaveFileText callback user-data pointer + //---------------------------------------------------------------------------------- // Functions to set internal callbacks //---------------------------------------------------------------------------------- -void SetTraceLogCallback(TraceLogCallback callback) { traceLog = callback; } // Set custom trace log -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 +void SetTraceLogCallback(void* userData, TraceLogCallback callback) { traceLogUser = userData; traceLog = callback; } // Set custom trace log +void SetLoadFileDataCallback(void* userData, LoadFileDataCallback callback) { loadFileDataUser = userData; loadFileData = callback; } // Set custom file data loader +void SetSaveFileDataCallback(void* userData, SaveFileDataCallback callback) { saveFileDataUser = userData; saveFileData = callback; } // Set custom file data saver +void SetLoadFileTextCallback(void* userData, LoadFileTextCallback callback) { loadFileTextUser = userData; loadFileText = callback; } // Set custom file text loader +void SetSaveFileTextCallback(void* userData, SaveFileTextCallback callback) { saveFileTextUser = userData; saveFileText = callback; } // Set custom file text saver #if defined(PLATFORM_ANDROID) static AAssetManager *assetManager = NULL; // Android assets manager pointer @@ -113,7 +119,7 @@ void TraceLog(int logType, const char *text, ...) if (traceLog) { - traceLog(logType, text, args); + traceLog(traceLogUser, logType, text, args); va_end(args); return; } @@ -188,7 +194,7 @@ unsigned char *LoadFileData(const char *fileName, int *dataSize) { if (loadFileData) { - data = loadFileData(fileName, dataSize); + data = loadFileData(loadFileDataUser, fileName, dataSize); return data; } #if defined(SUPPORT_STANDARD_FILEIO) @@ -259,7 +265,7 @@ bool SaveFileData(const char *fileName, void *data, int dataSize) { if (saveFileData) { - return saveFileData(fileName, data, dataSize); + return saveFileData(saveFileDataUser, fileName, data, dataSize); } #if defined(SUPPORT_STANDARD_FILEIO) FILE *file = fopen(fileName, "wb"); @@ -350,7 +356,7 @@ char *LoadFileText(const char *fileName) { if (loadFileText) { - text = loadFileText(fileName); + text = loadFileText(loadFileTextUser, fileName); return text; } #if defined(SUPPORT_STANDARD_FILEIO) @@ -413,7 +419,7 @@ bool SaveFileText(const char *fileName, char *text) { if (saveFileText) { - return saveFileText(fileName, text); + return saveFileText(saveFileTextUser, fileName, text); } #if defined(SUPPORT_STANDARD_FILEIO) FILE *file = fopen(fileName, "wt");