2014-09-16 22:51:31 +02:00
|
|
|
/**********************************************************************************************
|
2014-01-23 12:36:18 +01:00
|
|
|
*
|
2017-02-16 00:50:02 +01:00
|
|
|
* raylib.utils - Some common utility functions
|
2014-01-23 12:36:18 +01:00
|
|
|
*
|
2017-02-16 00:50:02 +01:00
|
|
|
* CONFIGURATION:
|
2014-09-03 16:51:28 +02:00
|
|
|
*
|
2017-03-19 12:52:58 +01:00
|
|
|
* #define SUPPORT_TRACELOG
|
|
|
|
|
* Show TraceLog() output messages
|
2017-07-02 12:35:13 +02:00
|
|
|
* NOTE: By default LOG_DEBUG traces not shown
|
2017-02-16 00:50:02 +01:00
|
|
|
*
|
2016-11-16 18:46:13 +01:00
|
|
|
*
|
2017-02-16 00:50:02 +01:00
|
|
|
* LICENSE: zlib/libpng
|
2016-11-16 18:46:13 +01:00
|
|
|
*
|
2023-01-01 16:07:51 +01:00
|
|
|
* Copyright (c) 2014-2023 Ramon Santamaria (@raysan5)
|
2014-09-03 16:51:28 +02:00
|
|
|
*
|
|
|
|
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
2014-01-23 12:36:18 +01:00
|
|
|
* will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
|
*
|
2014-09-03 16:51:28 +02:00
|
|
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
2014-01-23 12:36:18 +01:00
|
|
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
|
|
|
|
*
|
2014-09-03 16:51:28 +02:00
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
|
|
|
|
* wrote the original software. If you use this software in a product, an acknowledgment
|
2014-01-23 12:36:18 +01:00
|
|
|
* in the product documentation would be appreciated but is not required.
|
|
|
|
|
*
|
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
|
|
|
|
* as being the original software.
|
|
|
|
|
*
|
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
*
|
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
|
2019-01-15 12:32:41 +01:00
|
|
|
#include "raylib.h" // WARNING: Required for: LogType enum
|
2019-03-12 16:00:26 +01:00
|
|
|
|
|
|
|
|
// Check if config flags have been externally provided on compilation line
|
|
|
|
|
#if !defined(EXTERNAL_CONFIG_FLAGS)
|
2019-04-28 16:45:23 +02:00
|
|
|
#include "config.h" // Defines module configuration flags
|
2019-03-12 16:00:26 +01:00
|
|
|
#endif
|
|
|
|
|
|
2014-01-23 12:36:18 +01:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
2014-09-16 22:51:31 +02:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
2019-01-15 12:32:41 +01:00
|
|
|
#include <errno.h> // Required for: Android error types
|
|
|
|
|
#include <android/log.h> // Required for: Android log system: __android_log_vprint()
|
|
|
|
|
#include <android/asset_manager.h> // Required for: Android assets manager: AAsset, AAssetManager_open(), ...
|
2014-09-16 22:51:31 +02:00
|
|
|
#endif
|
|
|
|
|
|
2021-03-04 20:36:04 +01:00
|
|
|
#include <stdlib.h> // Required for: exit()
|
|
|
|
|
#include <stdio.h> // Required for: FILE, fopen(), fseek(), ftell(), fread(), fwrite(), fprintf(), vprintf(), fclose()
|
2020-02-09 16:11:11 +01:00
|
|
|
#include <stdarg.h> // Required for: va_list, va_start(), va_end()
|
2019-04-23 14:55:35 +02:00
|
|
|
#include <string.h> // Required for: strcpy(), strcat()
|
2014-01-23 12:36:18 +01:00
|
|
|
|
2020-05-01 17:31:44 +02:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
// Defines and Macros
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
#ifndef MAX_TRACELOG_MSG_LENGTH
|
|
|
|
|
#define MAX_TRACELOG_MSG_LENGTH 128 // Max length of one trace-log message
|
|
|
|
|
#endif
|
2020-01-08 18:21:08 +01:00
|
|
|
|
2014-04-09 20:25:26 +02:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
// Global Variables Definition
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2021-03-04 12:06:28 +01:00
|
|
|
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
|
|
|
|
|
|
2021-03-08 18:48:27 +01:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
// Functions to set internal callbacks
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2021-03-04 12:06:28 +01:00
|
|
|
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
|
|
|
|
|
|
2017-12-24 16:47:33 +01:00
|
|
|
|
2014-09-16 22:51:31 +02:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
2021-11-10 13:26:50 +01:00
|
|
|
static AAssetManager *assetManager = NULL; // Android assets manager pointer
|
|
|
|
|
static const char *internalDataPath = NULL; // Android internal data path
|
2020-01-08 18:21:08 +01:00
|
|
|
#endif
|
|
|
|
|
|
2014-09-16 22:51:31 +02:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
// Module specific Functions Declaration
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
2019-01-21 10:32:14 +01:00
|
|
|
FILE *funopen(const void *cookie, int (*readfn)(void *, char *, int), int (*writefn)(void *, const char *, int),
|
2018-12-29 17:41:41 +01:00
|
|
|
fpos_t (*seekfn)(void *, fpos_t, int), int (*closefn)(void *));
|
|
|
|
|
|
2014-09-16 22:51:31 +02:00
|
|
|
static int android_read(void *cookie, char *buf, int size);
|
|
|
|
|
static int android_write(void *cookie, const char *buf, int size);
|
|
|
|
|
static fpos_t android_seek(void *cookie, fpos_t offset, int whence);
|
|
|
|
|
static int android_close(void *cookie);
|
|
|
|
|
#endif
|
2014-04-09 20:25:26 +02:00
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
// Module Functions Definition - Utilities
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2017-03-29 00:35:42 +02:00
|
|
|
|
2019-01-21 10:32:14 +01:00
|
|
|
// Set the current threshold (minimum) log level
|
2021-03-04 12:06:28 +01:00
|
|
|
void SetTraceLogLevel(int logType) { logTypeLevel = logType; }
|
2018-07-26 21:57:45 +02:00
|
|
|
|
2017-07-02 12:35:13 +02:00
|
|
|
// Show trace log messages (LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_DEBUG)
|
2019-01-21 10:05:40 +01:00
|
|
|
void TraceLog(int logType, const char *text, ...)
|
2014-04-09 20:25:26 +02:00
|
|
|
{
|
2017-03-29 00:35:42 +02:00
|
|
|
#if defined(SUPPORT_TRACELOG)
|
2019-01-21 10:32:14 +01:00
|
|
|
// Message has level below current threshold, don't emit
|
|
|
|
|
if (logType < logTypeLevel) return;
|
2019-01-20 22:23:07 +01:00
|
|
|
|
2018-07-26 21:57:45 +02:00
|
|
|
va_list args;
|
|
|
|
|
va_start(args, text);
|
|
|
|
|
|
2021-03-04 12:06:28 +01:00
|
|
|
if (traceLog)
|
2018-07-26 21:57:45 +02:00
|
|
|
{
|
2021-03-04 12:06:28 +01:00
|
|
|
traceLog(logType, text, args);
|
2018-07-26 21:57:45 +02:00
|
|
|
va_end(args);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-09-16 22:51:31 +02:00
|
|
|
|
2017-01-15 01:10:23 +01:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
2021-06-26 13:45:39 +02:00
|
|
|
switch (logType)
|
2014-09-16 22:51:31 +02:00
|
|
|
{
|
2019-01-20 22:23:07 +01:00
|
|
|
case LOG_TRACE: __android_log_vprint(ANDROID_LOG_VERBOSE, "raylib", text, args); break;
|
|
|
|
|
case LOG_DEBUG: __android_log_vprint(ANDROID_LOG_DEBUG, "raylib", text, args); break;
|
2019-01-29 12:45:10 +01:00
|
|
|
case LOG_INFO: __android_log_vprint(ANDROID_LOG_INFO, "raylib", text, args); break;
|
2019-01-20 22:23:07 +01:00
|
|
|
case LOG_WARNING: __android_log_vprint(ANDROID_LOG_WARN, "raylib", text, args); break;
|
|
|
|
|
case LOG_ERROR: __android_log_vprint(ANDROID_LOG_ERROR, "raylib", text, args); break;
|
|
|
|
|
case LOG_FATAL: __android_log_vprint(ANDROID_LOG_FATAL, "raylib", text, args); break;
|
2014-09-16 22:51:31 +02:00
|
|
|
default: break;
|
|
|
|
|
}
|
2017-01-15 01:10:23 +01:00
|
|
|
#else
|
2020-05-01 17:31:44 +02:00
|
|
|
char buffer[MAX_TRACELOG_MSG_LENGTH] = { 0 };
|
2019-01-20 22:23:07 +01:00
|
|
|
|
UWP Support Overhaul (#819)
* Working build
* Fix build again, stop deleting files
* Hotfix crash, needs investigating
* Remove VS2015.UWP, I cannot update the project
* Lots of UWP work, added keyboard and mouse press support. Still need to finish scroll wheel, mouse position and cursor hiding, plus other stuff that I haven't seen yet.
* Implemented a ton more things, added BaseApp.h to provide common code to UWP apps.
* Remove constant window dimensions
* Enable and Disable cursor support.
* Actually use mouse delta
* Gamepad Support
* Cleaning and small tweaks
* Restore original example.
* Update comment
* Use 'Messages' to handle the cursor functions so code is more portable.
* Comment
* Comment unused message fields and use vector for mouse pos instead.
* Move messages to utils.h and use messages for everything. No more plat-specific code in raylib.h
* Working build
* Fix build again, stop deleting files
* Hotfix crash, needs investigating
* Remove VS2015.UWP, I cannot update the project
* Lots of UWP work, added keyboard and mouse press support. Still need to finish scroll wheel, mouse position and cursor hiding, plus other stuff that I haven't seen yet.
* Implemented a ton more things, added BaseApp.h to provide common code to UWP apps.
* Remove constant window dimensions
* Enable and Disable cursor support.
* Actually use mouse delta
* Gamepad Support
* Cleaning and small tweaks
* Restore original example.
* Update comment
* Use 'Messages' to handle the cursor functions so code is more portable.
* Comment
* Comment unused message fields and use vector for mouse pos instead.
* Move messages to utils.h and use messages for everything. No more plat-specific code in raylib.h
* Tested some desktop stuff and added projection matrix updates for window resizing.
* Fixed big bad mouse bug
* Fix alt buttons and add hack to combat flickery key presses (far from perfect)
* Remove debug code
* Final commit
* Well, so I thought
* Wow, i am bad
* Remove packages folder
* Remove useless include
* Apply requested changes and fix linux build
* Try to stop packages folder
* Have we fixed the formatting properly?
* Third time's the charm?
* Where did this come from?
* Re-fix
* Autoformat is gonna kill
* Fixed XBOX ONE Support
* Fix tabs
2019-04-27 19:33:51 +01:00
|
|
|
switch (logType)
|
2017-12-24 16:47:33 +01:00
|
|
|
{
|
2019-01-20 22:23:07 +01:00
|
|
|
case LOG_TRACE: strcpy(buffer, "TRACE: "); break;
|
|
|
|
|
case LOG_DEBUG: strcpy(buffer, "DEBUG: "); break;
|
2019-01-29 12:45:10 +01:00
|
|
|
case LOG_INFO: strcpy(buffer, "INFO: "); break;
|
2019-01-29 12:33:08 +01:00
|
|
|
case LOG_WARNING: strcpy(buffer, "WARNING: "); break;
|
2019-01-20 22:23:07 +01:00
|
|
|
case LOG_ERROR: strcpy(buffer, "ERROR: "); break;
|
|
|
|
|
case LOG_FATAL: strcpy(buffer, "FATAL: "); break;
|
2017-12-24 16:47:33 +01:00
|
|
|
default: break;
|
|
|
|
|
}
|
2019-01-20 22:23:07 +01:00
|
|
|
|
|
|
|
|
strcat(buffer, text);
|
|
|
|
|
strcat(buffer, "\n");
|
|
|
|
|
vprintf(buffer, args);
|
2022-05-06 20:15:13 +02:00
|
|
|
fflush(stdout);
|
2017-01-15 01:10:23 +01:00
|
|
|
#endif
|
2014-09-16 22:51:31 +02:00
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
2021-05-30 18:02:06 +02:00
|
|
|
if (logType == LOG_FATAL) exit(EXIT_FAILURE); // If fatal logging, exit program
|
2018-04-12 19:31:53 +02:00
|
|
|
|
2017-03-29 00:35:42 +02:00
|
|
|
#endif // SUPPORT_TRACELOG
|
2017-01-15 01:10:23 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-19 19:43:25 +01:00
|
|
|
// Internal memory allocator
|
2021-03-04 12:06:28 +01:00
|
|
|
// NOTE: Initializes to zero by default
|
2022-09-07 00:39:38 +02:00
|
|
|
void *MemAlloc(unsigned int size)
|
2020-12-19 19:43:25 +01:00
|
|
|
{
|
2021-03-08 18:48:27 +01:00
|
|
|
void *ptr = RL_CALLOC(size, 1);
|
2021-03-04 12:06:28 +01:00
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Internal memory reallocator
|
2022-09-07 00:39:38 +02:00
|
|
|
void *MemRealloc(void *ptr, unsigned int size)
|
2021-03-04 12:06:28 +01:00
|
|
|
{
|
2021-03-08 18:48:27 +01:00
|
|
|
void *ret = RL_REALLOC(ptr, size);
|
2021-03-04 12:06:28 +01:00
|
|
|
return ret;
|
2020-12-19 19:43:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Internal memory free
|
|
|
|
|
void MemFree(void *ptr)
|
|
|
|
|
{
|
2021-03-08 18:48:27 +01:00
|
|
|
RL_FREE(ptr);
|
2020-12-19 19:43:25 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-26 20:29:33 +01:00
|
|
|
// Load data from file into a buffer
|
2020-03-30 13:51:36 +02:00
|
|
|
unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
|
2020-02-26 20:29:33 +01:00
|
|
|
{
|
|
|
|
|
unsigned char *data = NULL;
|
|
|
|
|
*bytesRead = 0;
|
|
|
|
|
|
2020-03-04 00:21:46 +01:00
|
|
|
if (fileName != NULL)
|
2020-02-26 20:29:33 +01:00
|
|
|
{
|
2021-04-01 20:24:33 +02:00
|
|
|
if (loadFileData)
|
2021-03-04 12:06:28 +01:00
|
|
|
{
|
|
|
|
|
data = loadFileData(fileName, bytesRead);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
2021-03-04 20:36:04 +01:00
|
|
|
#if defined(SUPPORT_STANDARD_FILEIO)
|
2020-03-04 00:21:46 +01:00
|
|
|
FILE *file = fopen(fileName, "rb");
|
2020-02-26 20:29:33 +01:00
|
|
|
|
2020-03-04 00:21:46 +01:00
|
|
|
if (file != NULL)
|
2020-02-26 20:29:33 +01:00
|
|
|
{
|
2020-03-04 00:21:46 +01:00
|
|
|
// WARNING: On binary streams SEEK_END could not be found,
|
|
|
|
|
// using fseek() and ftell() could not work in some (rare) cases
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
|
int size = ftell(file);
|
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
|
{
|
2020-05-23 19:23:40 +02:00
|
|
|
data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char));
|
2020-03-04 00:21:46 +01:00
|
|
|
|
|
|
|
|
// NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
|
2020-05-06 19:12:09 +02:00
|
|
|
unsigned int count = (unsigned int)fread(data, sizeof(unsigned char), size, file);
|
2020-03-04 00:21:46 +01:00
|
|
|
*bytesRead = count;
|
|
|
|
|
|
2020-03-27 17:15:26 +01:00
|
|
|
if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName);
|
|
|
|
|
else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
|
2020-03-04 00:21:46 +01:00
|
|
|
}
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
2020-03-04 00:21:46 +01:00
|
|
|
|
|
|
|
|
fclose(file);
|
2020-02-26 20:29:33 +01:00
|
|
|
}
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
|
2021-03-04 20:36:04 +01:00
|
|
|
#else
|
|
|
|
|
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
|
|
|
|
|
#endif
|
2020-02-26 20:29:33 +01:00
|
|
|
}
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
|
2020-02-26 20:29:33 +01:00
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 20:47:58 +01:00
|
|
|
// Unload file data allocated by LoadFileData()
|
|
|
|
|
void UnloadFileData(unsigned char *data)
|
|
|
|
|
{
|
|
|
|
|
RL_FREE(data);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-26 20:29:33 +01:00
|
|
|
// Save data to file from buffer
|
2020-11-22 00:10:16 +01:00
|
|
|
bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
|
2020-02-26 20:29:33 +01:00
|
|
|
{
|
2020-11-22 00:10:16 +01:00
|
|
|
bool success = false;
|
2020-12-23 15:03:26 +01:00
|
|
|
|
2020-03-04 00:21:46 +01:00
|
|
|
if (fileName != NULL)
|
|
|
|
|
{
|
2021-04-01 20:24:33 +02:00
|
|
|
if (saveFileData)
|
2021-03-04 12:06:28 +01:00
|
|
|
{
|
2021-04-03 02:56:42 -04:00
|
|
|
return saveFileData(fileName, data, bytesToWrite);
|
2021-03-04 12:06:28 +01:00
|
|
|
}
|
2021-03-04 20:36:04 +01:00
|
|
|
#if defined(SUPPORT_STANDARD_FILEIO)
|
2020-03-04 00:21:46 +01:00
|
|
|
FILE *file = fopen(fileName, "wb");
|
|
|
|
|
|
|
|
|
|
if (file != NULL)
|
|
|
|
|
{
|
2020-05-06 19:12:09 +02:00
|
|
|
unsigned int count = (unsigned int)fwrite(data, sizeof(unsigned char), bytesToWrite, file);
|
2020-03-04 00:21:46 +01:00
|
|
|
|
2020-03-27 17:15:26 +01:00
|
|
|
if (count == 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write file", fileName);
|
|
|
|
|
else if (count != bytesToWrite) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName);
|
|
|
|
|
else TRACELOG(LOG_INFO, "FILEIO: [%s] File saved successfully", fileName);
|
2020-03-04 00:21:46 +01:00
|
|
|
|
2020-11-22 00:10:16 +01:00
|
|
|
int result = fclose(file);
|
|
|
|
|
if (result == 0) success = true;
|
2020-03-04 00:21:46 +01:00
|
|
|
}
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
|
2021-03-04 20:36:04 +01:00
|
|
|
#else
|
|
|
|
|
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
|
2021-04-01 20:24:33 +02:00
|
|
|
#endif
|
2020-03-04 00:21:46 +01:00
|
|
|
}
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
|
2020-12-23 15:03:26 +01:00
|
|
|
|
2020-11-22 00:10:16 +01:00
|
|
|
return success;
|
2020-03-04 00:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-06 00:14:28 +02:00
|
|
|
// Export data to code (.h), returns true on success
|
2022-11-04 15:39:04 -04:00
|
|
|
bool ExportDataAsCode(const unsigned char *data, unsigned int size, const char *fileName)
|
2022-05-06 00:14:28 +02:00
|
|
|
{
|
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
|
|
#ifndef TEXT_BYTES_PER_LINE
|
|
|
|
|
#define TEXT_BYTES_PER_LINE 20
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// NOTE: Text data buffer size is estimated considering raw data size in bytes
|
|
|
|
|
// and requiring 6 char bytes for every byte: "0x00, "
|
|
|
|
|
char *txtData = (char *)RL_CALLOC(size*6 + 2000, sizeof(char));
|
|
|
|
|
|
|
|
|
|
int byteCount = 0;
|
|
|
|
|
byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n");
|
|
|
|
|
byteCount += sprintf(txtData + byteCount, "// //\n");
|
|
|
|
|
byteCount += sprintf(txtData + byteCount, "// DataAsCode exporter v1.0 - Raw data exported as an array of bytes //\n");
|
|
|
|
|
byteCount += sprintf(txtData + byteCount, "// //\n");
|
|
|
|
|
byteCount += sprintf(txtData + byteCount, "// more info and bugs-report: github.com/raysan5/raylib //\n");
|
|
|
|
|
byteCount += sprintf(txtData + byteCount, "// feedback and support: ray[at]raylib.com //\n");
|
|
|
|
|
byteCount += sprintf(txtData + byteCount, "// //\n");
|
2023-01-01 16:07:51 +01:00
|
|
|
byteCount += sprintf(txtData + byteCount, "// Copyright (c) 2022-2023 Ramon Santamaria (@raysan5) //\n");
|
2022-05-06 00:14:28 +02:00
|
|
|
byteCount += sprintf(txtData + byteCount, "// //\n");
|
|
|
|
|
byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n\n");
|
|
|
|
|
|
|
|
|
|
// Get file name from path and convert variable name to uppercase
|
|
|
|
|
char varFileName[256] = { 0 };
|
|
|
|
|
strcpy(varFileName, GetFileNameWithoutExt(fileName));
|
|
|
|
|
for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; }
|
|
|
|
|
|
|
|
|
|
byteCount += sprintf(txtData + byteCount, "static unsigned char %s_DATA[%i] = { ", varFileName, size);
|
2022-06-07 10:04:24 +02:00
|
|
|
for (unsigned int i = 0; i < size - 1; i++) byteCount += sprintf(txtData + byteCount, ((i%TEXT_BYTES_PER_LINE == 0)? "0x%x,\n" : "0x%x, "), data[i]);
|
2022-05-06 00:14:28 +02:00
|
|
|
byteCount += sprintf(txtData + byteCount, "0x%x };\n", data[size - 1]);
|
|
|
|
|
|
|
|
|
|
// NOTE: Text data size exported is determined by '\0' (NULL) character
|
|
|
|
|
success = SaveFileText(fileName, txtData);
|
|
|
|
|
|
|
|
|
|
RL_FREE(txtData);
|
|
|
|
|
|
|
|
|
|
if (success != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Data as code exported successfully", fileName);
|
|
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export data as code", fileName);
|
|
|
|
|
|
2022-08-02 00:36:31 +02:00
|
|
|
return success;
|
2022-05-06 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-04 00:21:46 +01:00
|
|
|
// Load text data from file, returns a '\0' terminated string
|
|
|
|
|
// NOTE: text chars array should be freed manually
|
|
|
|
|
char *LoadFileText(const char *fileName)
|
|
|
|
|
{
|
|
|
|
|
char *text = NULL;
|
2020-02-26 20:29:33 +01:00
|
|
|
|
2020-03-04 00:21:46 +01:00
|
|
|
if (fileName != NULL)
|
2020-02-26 20:29:33 +01:00
|
|
|
{
|
2021-04-01 20:24:33 +02:00
|
|
|
if (loadFileText)
|
2021-03-04 20:36:04 +01:00
|
|
|
{
|
|
|
|
|
text = loadFileText(fileName);
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
#if defined(SUPPORT_STANDARD_FILEIO)
|
2021-03-04 20:22:58 +01:00
|
|
|
FILE *file = fopen(fileName, "rt");
|
2020-02-26 20:29:33 +01:00
|
|
|
|
2021-03-04 20:22:58 +01:00
|
|
|
if (file != NULL)
|
2020-03-04 00:21:46 +01:00
|
|
|
{
|
|
|
|
|
// WARNING: When reading a file as 'text' file,
|
|
|
|
|
// text mode causes carriage return-linefeed translation...
|
|
|
|
|
// ...but using fseek() should return correct byte-offset
|
2021-03-04 20:22:58 +01:00
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
|
unsigned int size = (unsigned int)ftell(file);
|
|
|
|
|
fseek(file, 0, SEEK_SET);
|
2020-03-04 00:21:46 +01:00
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
|
{
|
2020-05-23 19:23:40 +02:00
|
|
|
text = (char *)RL_MALLOC((size + 1)*sizeof(char));
|
2021-03-04 20:22:58 +01:00
|
|
|
unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
|
2020-03-04 00:21:46 +01:00
|
|
|
|
|
|
|
|
// WARNING: \r\n is converted to \n on reading, so,
|
|
|
|
|
// read bytes count gets reduced by the number of lines
|
|
|
|
|
if (count < size) text = RL_REALLOC(text, count + 1);
|
|
|
|
|
|
|
|
|
|
// Zero-terminate the string
|
|
|
|
|
text[count] = '\0';
|
2020-03-25 19:41:51 +01:00
|
|
|
|
2020-03-27 17:15:26 +01:00
|
|
|
TRACELOG(LOG_INFO, "FILEIO: [%s] Text file loaded successfully", fileName);
|
2020-03-04 00:21:46 +01:00
|
|
|
}
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read text file", fileName);
|
2020-03-04 00:21:46 +01:00
|
|
|
|
2021-03-04 20:22:58 +01:00
|
|
|
fclose(file);
|
2020-03-04 00:21:46 +01:00
|
|
|
}
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
|
2021-03-04 20:36:04 +01:00
|
|
|
#else
|
|
|
|
|
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
|
|
|
|
|
#endif
|
2020-03-04 00:21:46 +01:00
|
|
|
}
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
|
2020-02-26 20:29:33 +01:00
|
|
|
|
2020-03-04 00:21:46 +01:00
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 20:47:58 +01:00
|
|
|
// Unload file text data allocated by LoadFileText()
|
2021-05-22 16:54:04 +02:00
|
|
|
void UnloadFileText(char *text)
|
2020-12-14 20:47:58 +01:00
|
|
|
{
|
|
|
|
|
RL_FREE(text);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 00:21:46 +01:00
|
|
|
// Save text data to file (write), string must be '\0' terminated
|
2020-11-22 00:10:16 +01:00
|
|
|
bool SaveFileText(const char *fileName, char *text)
|
2020-03-04 00:21:46 +01:00
|
|
|
{
|
2020-11-22 00:10:16 +01:00
|
|
|
bool success = false;
|
2020-12-23 15:03:26 +01:00
|
|
|
|
2020-03-04 00:21:46 +01:00
|
|
|
if (fileName != NULL)
|
|
|
|
|
{
|
2021-04-01 20:24:33 +02:00
|
|
|
if (saveFileText)
|
2021-03-04 20:36:04 +01:00
|
|
|
{
|
2021-04-03 02:56:42 -04:00
|
|
|
return saveFileText(fileName, text);
|
2021-03-04 20:36:04 +01:00
|
|
|
}
|
|
|
|
|
#if defined(SUPPORT_STANDARD_FILEIO)
|
2020-03-04 00:21:46 +01:00
|
|
|
FILE *file = fopen(fileName, "wt");
|
|
|
|
|
|
|
|
|
|
if (file != NULL)
|
|
|
|
|
{
|
|
|
|
|
int count = fprintf(file, "%s", text);
|
|
|
|
|
|
2020-11-01 19:42:50 +01:00
|
|
|
if (count < 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write text file", fileName);
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_INFO, "FILEIO: [%s] Text file saved successfully", fileName);
|
2020-03-04 00:21:46 +01:00
|
|
|
|
2020-11-22 00:10:16 +01:00
|
|
|
int result = fclose(file);
|
|
|
|
|
if (result == 0) success = true;
|
2020-03-04 00:21:46 +01:00
|
|
|
}
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
|
2021-03-04 20:36:04 +01:00
|
|
|
#else
|
|
|
|
|
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
|
|
|
|
|
#endif
|
2020-02-26 20:29:33 +01:00
|
|
|
}
|
2020-03-27 17:15:26 +01:00
|
|
|
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
|
2020-12-23 15:03:26 +01:00
|
|
|
|
2020-11-22 00:10:16 +01:00
|
|
|
return success;
|
2020-02-26 20:29:33 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-15 01:10:23 +01:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
2014-09-16 22:51:31 +02:00
|
|
|
// Initialize asset manager from android app
|
2020-03-05 18:12:41 +01:00
|
|
|
void InitAssetManager(AAssetManager *manager, const char *dataPath)
|
2014-09-16 22:51:31 +02:00
|
|
|
{
|
|
|
|
|
assetManager = manager;
|
2020-03-05 18:12:41 +01:00
|
|
|
internalDataPath = dataPath;
|
2014-04-09 20:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-16 21:19:28 +02:00
|
|
|
// Replacement for fopen()
|
2020-03-05 18:12:41 +01:00
|
|
|
// Ref: https://developer.android.com/ndk/reference/group/asset
|
2015-02-02 00:53:49 +01:00
|
|
|
FILE *android_fopen(const char *fileName, const char *mode)
|
2014-04-09 20:25:26 +02:00
|
|
|
{
|
2021-04-16 21:19:28 +02:00
|
|
|
if (mode[0] == 'w')
|
2020-03-05 18:12:41 +01:00
|
|
|
{
|
2021-10-13 23:45:57 +02:00
|
|
|
// fopen() is mapped to android_fopen() that only grants read access to
|
|
|
|
|
// assets directory through AAssetManager but we want to also be able to
|
2020-03-05 18:12:41 +01:00
|
|
|
// write data when required using the standard stdio FILE access functions
|
|
|
|
|
// Ref: https://stackoverflow.com/questions/11294487/android-writing-saving-files-from-native-code-only
|
|
|
|
|
#undef fopen
|
|
|
|
|
return fopen(TextFormat("%s/%s", internalDataPath, fileName), mode);
|
|
|
|
|
#define fopen(name, mode) android_fopen(name, mode)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// NOTE: AAsset provides access to read-only asset
|
|
|
|
|
AAsset *asset = AAssetManager_open(assetManager, fileName, AASSET_MODE_UNKNOWN);
|
2014-09-16 22:51:31 +02:00
|
|
|
|
2020-11-03 23:47:33 +01:00
|
|
|
if (asset != NULL)
|
2020-04-05 17:51:27 +02:00
|
|
|
{
|
2021-06-10 17:49:55 +02:00
|
|
|
// Get pointer to file in the assets
|
2020-04-05 17:51:27 +02:00
|
|
|
return funopen(asset, android_read, android_write, android_seek, android_close);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
#undef fopen
|
|
|
|
|
// Just do a regular open if file is not found in the assets
|
|
|
|
|
return fopen(TextFormat("%s/%s", internalDataPath, fileName), mode);
|
|
|
|
|
#define fopen(name, mode) android_fopen(name, mode)
|
|
|
|
|
}
|
2020-03-05 18:12:41 +01:00
|
|
|
}
|
2014-04-09 20:25:26 +02:00
|
|
|
}
|
2019-04-28 16:45:23 +02:00
|
|
|
#endif // PLATFORM_ANDROID
|
2014-04-09 20:25:26 +02:00
|
|
|
|
2014-09-16 22:51:31 +02:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
// Module specific Functions Definition
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
2015-02-02 00:53:49 +01:00
|
|
|
static int android_read(void *cookie, char *buf, int size)
|
2014-09-16 22:51:31 +02:00
|
|
|
{
|
|
|
|
|
return AAsset_read((AAsset *)cookie, buf, size);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-02 00:53:49 +01:00
|
|
|
static int android_write(void *cookie, const char *buf, int size)
|
2014-09-16 22:51:31 +02:00
|
|
|
{
|
2020-03-27 17:15:26 +01:00
|
|
|
TRACELOG(LOG_WARNING, "ANDROID: Failed to provide write access to APK");
|
2014-09-16 22:51:31 +02:00
|
|
|
|
|
|
|
|
return EACCES;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-02 00:53:49 +01:00
|
|
|
static fpos_t android_seek(void *cookie, fpos_t offset, int whence)
|
2014-09-16 22:51:31 +02:00
|
|
|
{
|
|
|
|
|
return AAsset_seek((AAsset *)cookie, offset, whence);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-02 00:53:49 +01:00
|
|
|
static int android_close(void *cookie)
|
2014-09-16 22:51:31 +02:00
|
|
|
{
|
|
|
|
|
AAsset_close((AAsset *)cookie);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-28 16:45:23 +02:00
|
|
|
#endif // PLATFORM_ANDROID
|