// 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, float value, const char* _fileName) { bool success = false; unsigned int dataSize = 0; unsigned int newDataSize = 0; unsigned char *fileData = LoadFileData(_fileName, &dataSize); unsigned char *newFileData = NULL; if (fileData != NULL) { if (dataSize <= (position*sizeof(float))) { // Increase data size up to position and store value newDataSize = (position + 1)*sizeof(float); newFileData = (unsigned char *)RL_REALLOC(fileData, newDataSize); if (newFileData != NULL) { // RL_REALLOC succeded float *dataPtr = (float *)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", *_fileName, 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 float *dataPtr = (float *)newFileData; dataPtr[position] = value; } success = SaveFileData(_fileName, newFileData, newDataSize); RL_FREE(newFileData); TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", _fileName, value); } else { TraceLog(LOG_INFO, "FILEIO: [%s] File created successfully", _fileName); dataSize = (position + 1)*sizeof(float); fileData = (unsigned char *)RL_MALLOC(dataSize); float *dataPtr = (float *)fileData; dataPtr[position] = value; success = SaveFileData(_fileName, fileData, dataSize); UnloadFileData(fileData); TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", _fileName, value); } return success; } // Load float value from storage file (from defined position) // NOTE: If requested position could not be found, value 0 is returned float LoadStorageValue(unsigned int position, const char* _fileName) { float value = 0; unsigned int dataSize = 0; unsigned char *fileData = LoadFileData(_fileName, &dataSize); if (fileData != NULL) { if (dataSize < (position*sizeof(float))) TraceLog(LOG_WARNING, "FILEIO: [%s] Failed to find storage position: %i", _fileName, position); else { float *dataPtr = (float *)fileData; value = dataPtr[position]; } UnloadFileData(fileData); TraceLog(LOG_INFO, "FILEIO: [%s] Loaded storage value: %f", _fileName, value); } return value; }