diff --git a/CMakeOptions.txt b/CMakeOptions.txt index 463977333..d76983391 100644 --- a/CMakeOptions.txt +++ b/CMakeOptions.txt @@ -45,7 +45,6 @@ cmake_dependent_option(SUPPORT_GIF_RECORDING "Allow automatic gif recording of c cmake_dependent_option(SUPPORT_BUSY_WAIT_LOOP "Use busy wait loop for timing sync instead of a high-resolution timer" OFF CUSTOMIZE_BUILD OFF) cmake_dependent_option(SUPPORT_EVENTS_WAITING "Wait for events passively (sleeping while no events) instead of polling them actively every frame" OFF CUSTOMIZE_BUILD OFF) cmake_dependent_option(SUPPORT_WINMM_HIGHRES_TIMER "Setting a higher resolution can improve the accuracy of time-out intervals in wait functions" OFF CUSTOMIZE_BUILD OFF) -cmake_dependent_option(SUPPORT_DATA_STORAGE "Support for persistent data storage" ON CUSTOMIZE_BUILD ON) cmake_dependent_option(SUPPORT_COMPRESSION_API "Support for compression API" ON CUSTOMIZE_BUILD ON) # rshapes.c diff --git a/cmake/CompileDefinitions.cmake b/cmake/CompileDefinitions.cmake index 7fbbef99e..6c8d0d715 100644 --- a/cmake/CompileDefinitions.cmake +++ b/cmake/CompileDefinitions.cmake @@ -27,7 +27,6 @@ if (${CUSTOMIZE_BUILD}) define_if("raylib" SUPPORT_BUSY_WAIT_LOOP) define_if("raylib" SUPPORT_EVENTS_WAITING) define_if("raylib" SUPPORT_WINMM_HIGHRES_TIMER) - define_if("raylib" SUPPORT_DATA_STORAGE) define_if("raylib" SUPPORT_COMPRESSION_API) define_if("raylib" SUPPORT_QUADS_DRAW_MODE) define_if("raylib" SUPPORT_IMAGE_EXPORT) diff --git a/examples/core/core_storage_values.c b/examples/core/core_storage_values.c index 41173d26e..03bd70f1e 100644 --- a/examples/core/core_storage_values.c +++ b/examples/core/core_storage_values.c @@ -11,12 +11,20 @@ #include "raylib.h" +#include // Required for: calloc(), free() + +#define STORAGE_DATA_FILE "storage.data" // Storage file + // NOTE: Storage positions must start with 0, directly related to file memory layout typedef enum { STORAGE_POSITION_SCORE = 0, STORAGE_POSITION_HISCORE = 1 } StorageData; +// Persistent storage functions +static bool SaveStorageValue(unsigned int position, int value); +static int LoadStorageValue(unsigned int position); + //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ @@ -87,4 +95,97 @@ int main(void) //-------------------------------------------------------------------------------------- return 0; +} + +// Save integer value to storage file (to defined position) +// NOTE: Storage positions is directly related to file memory layout (4 bytes each integer) +bool SaveStorageValue(unsigned int position, int value) +{ + bool success = false; + unsigned int dataSize = 0; + unsigned int newDataSize = 0; + unsigned char *fileData = LoadFileData(STORAGE_DATA_FILE, &dataSize); + unsigned char *newFileData = NULL; + + if (fileData != NULL) + { + if (dataSize <= (position*sizeof(int))) + { + // Increase data size up to position and store value + newDataSize = (position + 1)*sizeof(int); + newFileData = (unsigned char *)RL_REALLOC(fileData, newDataSize); + + if (newFileData != NULL) + { + // RL_REALLOC succeded + int *dataPtr = (int *)newFileData; + dataPtr[position] = value; + } + else + { + // RL_REALLOC failed + TraceLog(LOG_WARNING, "FILEIO: [%s] Failed to realloc data (%u), position in bytes (%u) bigger than actual file size", STORAGE_DATA_FILE, dataSize, position*sizeof(int)); + + // We store the old size of the file + newFileData = fileData; + newDataSize = dataSize; + } + } + else + { + // Store the old size of the file + newFileData = fileData; + newDataSize = dataSize; + + // Replace value on selected position + int *dataPtr = (int *)newFileData; + dataPtr[position] = value; + } + + success = SaveFileData(STORAGE_DATA_FILE, newFileData, newDataSize); + RL_FREE(newFileData); + + TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", STORAGE_DATA_FILE, value); + } + else + { + TraceLog(LOG_INFO, "FILEIO: [%s] File created successfully", STORAGE_DATA_FILE); + + dataSize = (position + 1)*sizeof(int); + fileData = (unsigned char *)RL_MALLOC(dataSize); + int *dataPtr = (int *)fileData; + dataPtr[position] = value; + + success = SaveFileData(STORAGE_DATA_FILE, fileData, dataSize); + UnloadFileData(fileData); + + TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", STORAGE_DATA_FILE, value); + } + + return success; +} + +// Load integer value from storage file (from defined position) +// NOTE: If requested position could not be found, value 0 is returned +int LoadStorageValue(unsigned int position) +{ + int value = 0; + unsigned int dataSize = 0; + unsigned char *fileData = LoadFileData(STORAGE_DATA_FILE, &dataSize); + + if (fileData != NULL) + { + if (dataSize < (position*4)) TraceLog(LOG_WARNING, "FILEIO: [%s] Failed to find storage position: %i", STORAGE_DATA_FILE, position); + else + { + int *dataPtr = (int *)fileData; + value = dataPtr[position]; + } + + UnloadFileData(fileData); + + TraceLog(LOG_INFO, "FILEIO: [%s] Loaded storage value: %i", STORAGE_DATA_FILE, value); + } + + return value; } \ No newline at end of file diff --git a/src/config.h b/src/config.h index 8a20f1745..132b8cee2 100644 --- a/src/config.h +++ b/src/config.h @@ -63,8 +63,6 @@ #define SUPPORT_GIF_RECORDING 1 // Support CompressData() and DecompressData() functions #define SUPPORT_COMPRESSION_API 1 -// Support saving binary data automatically to a generated storage.data file. This file is managed internally. -#define SUPPORT_DATA_STORAGE 1 // Support automatic generated events, loading and recording of those events when required //#define SUPPORT_EVENTS_AUTOMATION 1 // Support custom frame control, only for advance users diff --git a/src/raylib.h b/src/raylib.h index f9db8a177..3bc08898c 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1081,10 +1081,6 @@ RLAPI unsigned char *DecompressData(const unsigned char *compData, int compDataS RLAPI char *EncodeDataBase64(const unsigned char *data, int dataSize, int *outputSize); // Encode data to Base64 string, memory must be MemFree() RLAPI unsigned char *DecodeDataBase64(const unsigned char *data, int *outputSize); // Decode Base64 string data, memory must be MemFree() -// Persistent storage management -RLAPI bool SaveStorageValue(unsigned int position, int value); // Save integer value to storage file (to defined position), returns true on success -RLAPI int LoadStorageValue(unsigned int position); // Load integer value from storage file (from defined position) - RLAPI void OpenURL(const char *url); // Open URL with default system browser (if available) //------------------------------------------------------------------------------------ diff --git a/src/rcore.c b/src/rcore.c index 55e820b63..1313349bc 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -80,9 +80,6 @@ * provided by stb_image and stb_image_write libraries, so, those libraries must be enabled on textures module * for linkage * -* #define SUPPORT_DATA_STORAGE -* Support saving binary data automatically to a generated storage.data file. This file is managed internally -* * #define SUPPORT_EVENTS_AUTOMATION * Support automatic generated events, loading and recording of those events when required * @@ -332,12 +329,6 @@ #define MAX_CHAR_PRESSED_QUEUE 16 // Maximum number of characters in the char input queue #endif -#if defined(SUPPORT_DATA_STORAGE) - #ifndef STORAGE_DATA_FILE - #define STORAGE_DATA_FILE "storage.data" // Automatic storage filename - #endif -#endif - #ifndef MAX_DECOMPRESSION_SIZE #define MAX_DECOMPRESSION_SIZE 64 // Maximum size allocated for decompression in MB #endif @@ -3398,110 +3389,6 @@ unsigned char *DecodeDataBase64(const unsigned char *data, int *outputSize) return decodedData; } -// Save integer value to storage file (to defined position) -// NOTE: Storage positions is directly related to file memory layout (4 bytes each integer) -bool SaveStorageValue(unsigned int position, int value) -{ - bool success = false; - -#if defined(SUPPORT_DATA_STORAGE) - char path[512] = { 0 }; - strcpy(path, TextFormat("%s/%s", CORE.Storage.basePath, STORAGE_DATA_FILE)); - - unsigned int dataSize = 0; - unsigned int newDataSize = 0; - unsigned char *fileData = LoadFileData(path, &dataSize); - unsigned char *newFileData = NULL; - - if (fileData != NULL) - { - if (dataSize <= (position*sizeof(int))) - { - // Increase data size up to position and store value - newDataSize = (position + 1)*sizeof(int); - newFileData = (unsigned char *)RL_REALLOC(fileData, newDataSize); - - if (newFileData != NULL) - { - // RL_REALLOC succeded - int *dataPtr = (int *)newFileData; - dataPtr[position] = value; - } - else - { - // RL_REALLOC failed - TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to realloc data (%u), position in bytes (%u) bigger than actual file size", path, dataSize, position*sizeof(int)); - - // We store the old size of the file - newFileData = fileData; - newDataSize = dataSize; - } - } - else - { - // Store the old size of the file - newFileData = fileData; - newDataSize = dataSize; - - // Replace value on selected position - int *dataPtr = (int *)newFileData; - dataPtr[position] = value; - } - - success = SaveFileData(path, newFileData, newDataSize); - RL_FREE(newFileData); - - TRACELOG(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", path, value); - } - else - { - TRACELOG(LOG_INFO, "FILEIO: [%s] File created successfully", path); - - dataSize = (position + 1)*sizeof(int); - fileData = (unsigned char *)RL_MALLOC(dataSize); - int *dataPtr = (int *)fileData; - dataPtr[position] = value; - - success = SaveFileData(path, fileData, dataSize); - UnloadFileData(fileData); - - TRACELOG(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", path, value); - } -#endif - - return success; -} - -// Load integer value from storage file (from defined position) -// NOTE: If requested position could not be found, value 0 is returned -int LoadStorageValue(unsigned int position) -{ - int value = 0; - -#if defined(SUPPORT_DATA_STORAGE) - char path[512] = { 0 }; - strcpy(path, TextFormat("%s/%s", CORE.Storage.basePath, STORAGE_DATA_FILE)); - - unsigned int dataSize = 0; - unsigned char *fileData = LoadFileData(path, &dataSize); - - if (fileData != NULL) - { - if (dataSize < (position*4)) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to find storage position: %i", path, position); - else - { - int *dataPtr = (int *)fileData; - value = dataPtr[position]; - } - - UnloadFileData(fileData); - - TRACELOG(LOG_INFO, "FILEIO: [%s] Loaded storage value: %i", path, value); - } -#endif - return value; -} - // Open URL with default system browser (if available) // NOTE: This function is only safe to use if you control the URL given. // A user could craft a malicious string performing another action.