Update C sources

This commit is contained in:
Milan Nikolic 2021-05-25 14:35:17 +02:00
parent 9d258bad65
commit c1525b67af
No known key found for this signature in database
GPG key ID: 9229D0EAA3AA4E75
68 changed files with 34056 additions and 25957 deletions

View file

@ -11,7 +11,7 @@
*
* LICENSE: zlib/libpng
*
* Copyright (c) 2014-2020 Ramon Santamaria (@raysan5)
* Copyright (c) 2014-2021 Ramon Santamaria (@raysan5)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
@ -46,7 +46,7 @@
#endif
#include <stdlib.h> // Required for: exit()
#include <stdio.h> // Required for: vprintf()
#include <stdio.h> // Required for: FILE, fopen(), fseek(), ftell(), fread(), fwrite(), fprintf(), vprintf(), fclose()
#include <stdarg.h> // Required for: va_list, va_start(), va_end()
#include <string.h> // Required for: strcpy(), strcat()
@ -63,11 +63,23 @@
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
static int logTypeLevel = LOG_INFO; // Minimum log type level
static TraceLogCallback traceLog = NULL; // TraceLog callback function 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
//----------------------------------------------------------------------------------
// 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
// 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
#if defined(PLATFORM_ANDROID)
static AAssetManager *assetManager = NULL; // Android assets manager pointer
@ -92,22 +104,7 @@ static int android_close(void *cookie);
//----------------------------------------------------------------------------------
// Set the current threshold (minimum) log level
void SetTraceLogLevel(int logType)
{
logTypeLevel = logType;
}
// Set the exit threshold (minimum) log level
void SetTraceLogExit(int logType)
{
logTypeExit = 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, ...)
@ -119,9 +116,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;
}
@ -158,11 +155,32 @@ 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)
{
void *ptr = RL_CALLOC(size, 1);
return ptr;
}
// Internal memory reallocator
void *MemRealloc(void *ptr, int size)
{
void *ret = RL_REALLOC(ptr, size);
return ret;
}
// Internal memory free
void MemFree(void *ptr)
{
RL_FREE(ptr);
}
// Load data from file into a buffer
unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
{
@ -171,6 +189,12 @@ unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
if (fileName != NULL)
{
if (loadFileData)
{
data = loadFileData(fileName, bytesRead);
return data;
}
#if defined(SUPPORT_STANDARD_FILEIO)
FILE *file = fopen(fileName, "rb");
if (file != NULL)
@ -197,17 +221,33 @@ unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
fclose(file);
}
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
#else
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
#endif
}
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)
// Unload file data allocated by LoadFileData()
void UnloadFileData(unsigned char *data)
{
RL_FREE(data);
}
// Save data to file from buffer
bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
{
bool success = false;
if (fileName != NULL)
{
if (saveFileData)
{
return saveFileData(fileName, data, bytesToWrite);
}
#if defined(SUPPORT_STANDARD_FILEIO)
FILE *file = fopen(fileName, "wb");
if (file != NULL)
@ -218,11 +258,17 @@ void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
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);
int result = fclose(file);
if (result == 0) success = true;
}
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
#else
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
#endif
}
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
return success;
}
// Load text data from file, returns a '\0' terminated string
@ -233,21 +279,27 @@ char *LoadFileText(const char *fileName)
if (fileName != NULL)
{
FILE *textFile = fopen(fileName, "rt");
if (loadFileText)
{
text = loadFileText(fileName);
return text;
}
#if defined(SUPPORT_STANDARD_FILEIO)
FILE *file = fopen(fileName, "rt");
if (textFile != NULL)
if (file != 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);
unsigned int size = (unsigned int)ftell(textFile);
fseek(textFile, 0, SEEK_SET);
fseek(file, 0, SEEK_END);
unsigned int size = (unsigned int)ftell(file);
fseek(file, 0, SEEK_SET);
if (size > 0)
{
text = (char *)RL_MALLOC((size + 1)*sizeof(char));
unsigned int count = (unsigned int)fread(text, sizeof(char), size, textFile);
unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
// WARNING: \r\n is converted to \n on reading, so,
// read bytes count gets reduced by the number of lines
@ -260,34 +312,56 @@ char *LoadFileText(const char *fileName)
}
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read text file", fileName);
fclose(textFile);
fclose(file);
}
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
#else
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
#endif
}
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)
// Unload file text data allocated by LoadFileText()
void UnloadFileText(char *text)
{
RL_FREE(text);
}
// Save text data to file (write), string must be '\0' terminated
bool SaveFileText(const char *fileName, char *text)
{
bool success = false;
if (fileName != NULL)
{
if (saveFileText)
{
return saveFileText(fileName, text);
}
#if defined(SUPPORT_STANDARD_FILEIO)
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);
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);
int result = fclose(file);
if (result == 0) success = true;
}
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
#else
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
#endif
}
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
return success;
}
#if defined(PLATFORM_ANDROID)
@ -298,11 +372,11 @@ void InitAssetManager(AAssetManager *manager, const char *dataPath)
internalDataPath = dataPath;
}
// Replacement for fopen
// 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') // TODO: Test!
if (mode[0] == 'w')
{
// 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
@ -317,7 +391,7 @@ FILE *android_fopen(const char *fileName, const char *mode)
// NOTE: AAsset provides access to read-only asset
AAsset *asset = AAssetManager_open(assetManager, fileName, AASSET_MODE_UNKNOWN);
if (asset != NULL)
if (asset != NULL)
{
// Return pointer to file in the assets
return funopen(asset, android_read, android_write, android_seek, android_close);