mirror of
https://github.com/raysan5/raylib.git
synced 2025-12-25 10:22:33 -05:00
Added functionality: Storage values
Two new functions added to save/load values as binary data: - StorageSaveValue() - StorageLoadValue()
This commit is contained in:
61
src/core.c
61
src/core.c
@ -708,6 +708,67 @@ void ClearDroppedFiles(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
// Storage save integer value (to defined position)
|
||||
// NOTE: Storage positions is directly related to file memory layout (4 bytes each integer)
|
||||
void StorageSaveValue(int position, int value)
|
||||
{
|
||||
FILE *storageFile = NULL;
|
||||
|
||||
// Try open existing file to append data
|
||||
storageFile = fopen("storage.data", "rb+");
|
||||
|
||||
// If file doesn't exist, create a new storage data file
|
||||
if (!storageFile) storageFile = fopen("storage.data", "wb");
|
||||
|
||||
if (!storageFile) TraceLog(WARNING, "Storage data file could not be created");
|
||||
else
|
||||
{
|
||||
// Get file size
|
||||
fseek(storageFile, 0, SEEK_END);
|
||||
int fileSize = ftell(storageFile); // Size in bytes
|
||||
fseek(storageFile, 0, SEEK_SET);
|
||||
|
||||
if (fileSize < (position*4)) TraceLog(WARNING, "Storage position could not be found");
|
||||
else
|
||||
{
|
||||
fseek(storageFile, (position*4), SEEK_SET);
|
||||
fwrite(&value, 1, 4, storageFile);
|
||||
}
|
||||
|
||||
fclose(storageFile);
|
||||
}
|
||||
}
|
||||
|
||||
// Storage load integer value (from defined position)
|
||||
// NOTE: If requested position could not be found, value 0 is returned
|
||||
int StorageLoadValue(int position)
|
||||
{
|
||||
int value = 0;
|
||||
|
||||
// Try open existing file to append data
|
||||
FILE *storageFile = fopen("storage.data", "rb");
|
||||
|
||||
if (!storageFile) TraceLog(WARNING, "Storage data file could not be found");
|
||||
else
|
||||
{
|
||||
// Get file size
|
||||
fseek(storageFile, 0, SEEK_END);
|
||||
int fileSize = ftell(storageFile); // Size in bytes
|
||||
rewind(storageFile);
|
||||
|
||||
if (fileSize < (position*4)) TraceLog(WARNING, "Storage position could not be found");
|
||||
else
|
||||
{
|
||||
fseek(storageFile, (position*4), SEEK_SET);
|
||||
fread(&value, 1, 4, storageFile);
|
||||
}
|
||||
|
||||
fclose(storageFile);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
// TODO: Gives the ray trace from mouse position
|
||||
Ray GetMouseRay(Vector2 mousePosition, Camera camera)
|
||||
{
|
||||
|
||||
@ -540,6 +540,9 @@ bool IsFileDropped(void); // Check if a file h
|
||||
char **GetDroppedFiles(int *count); // Retrieve dropped files into window
|
||||
void ClearDroppedFiles(void); // Clear dropped files paths buffer
|
||||
|
||||
void StorageSaveValue(int position, int value); // Storage save integer value (to defined position)
|
||||
int StorageLoadValue(int position); // Storage load integer value (from defined position)
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Input Handling Functions (Module: core)
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user