feat: add engine SaveStorageData and LoadStorageData and started saving stats of player
This commit is contained in:
@@ -97,6 +97,9 @@ void RenderGame(App *app) {
|
||||
(Rectangle){0, 0, g->mouse.cursorTexture.width,
|
||||
g->mouse.cursorTexture.height},
|
||||
(Vector2){g->mouse.screenPosition.x, g->mouse.screenPosition.y}, WHITE);
|
||||
|
||||
Player *player = GetPlayer();
|
||||
DrawText(TextFormat("Gold: %d", player->gold), 0.0f, 0.0f, 12.0f, YELLOW);
|
||||
EndTextureMode();
|
||||
|
||||
BeginDrawing();
|
||||
|
||||
+2
-2
@@ -75,9 +75,9 @@ void CreatePlayer(void)
|
||||
.animation = playerAnimation,
|
||||
.physics = playerPhysics,
|
||||
.rotation = rotation,
|
||||
.soundFlapping = LoadSound(playerSoundAssets[FLAPPING].path)
|
||||
.soundFlapping = LoadSound(playerSoundAssets[FLAPPING].path),
|
||||
.gold = LoadStorageValue(STORAGE_DATA_FILE, STORAGE_POSITION_GOLD)
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
void UpdatePlayer(void)
|
||||
|
||||
@@ -73,6 +73,9 @@ void UpdateProjectiles(Projectiles *projectiles, Enemies *enemies) {
|
||||
for (int j = 0; j < enemies->count; j++){
|
||||
if (CheckCollisionCircleRec(projectiles->physics[i].position, projectiles->radius[i], enemies->sprite[j].destRec)){
|
||||
PlaySound(projectiles->soundImpact[i]);
|
||||
Player *player = GetPlayer();
|
||||
player->gold += 1;
|
||||
SaveStorageValue(STORAGE_DATA_FILE, STORAGE_POSITION_GOLD, player->gold);
|
||||
projectiles->active[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#include "engine.h"
|
||||
|
||||
bool SaveStorageValue(const char* fileName, unsigned int position, int value){
|
||||
bool success = false;
|
||||
int dataSize = 0;
|
||||
unsigned int newDataSize = 0;
|
||||
unsigned char *fileData = LoadFileData(fileName, &dataSize);
|
||||
unsigned char *newFileData = NULL;
|
||||
|
||||
if (fileData != NULL)
|
||||
{
|
||||
if (dataSize <= (position*sizeof(int)))
|
||||
{
|
||||
newDataSize = (position + 1)*sizeof(int);
|
||||
newFileData = (unsigned char *)RL_REALLOC(fileData, newDataSize);
|
||||
if (newFileData != NULL)
|
||||
{
|
||||
int *dataPtr = (int *)newFileData;
|
||||
dataPtr[position]=value;
|
||||
}
|
||||
else
|
||||
{
|
||||
TraceLog(LOG_WARNING, "FLIEIO: [%s] Failed to realloc data (%u), position in bytes (%u) bigger than actual file size", fileName, dataSize, position*sizeof(int));
|
||||
newFileData = fileData;
|
||||
newDataSize = dataSize;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
newFileData = fileData;
|
||||
newDataSize = dataSize;
|
||||
int *dataPtr = (int *)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(int);
|
||||
fileData = (unsigned char *)RL_MALLOC(dataSize);
|
||||
int *dataPtr = (int *)fileData;
|
||||
dataPtr[position] = value;
|
||||
success = SaveFileData(fileName, fileData, dataSize);
|
||||
UnloadFileData(fileData);
|
||||
TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", fileName, value);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
int LoadStorageValue(const char * fileName, unsigned int position)
|
||||
{
|
||||
int value = 0;
|
||||
int dataSize = 0;
|
||||
unsigned char *fileData = LoadFileData(fileName, &dataSize);
|
||||
if (fileData != NULL)
|
||||
{
|
||||
if (dataSize < ((int)(position*4))) TraceLog(LOG_WARNING, "FILEIO: [%s] Failed to find storage position: %i", fileName, position);
|
||||
else
|
||||
{
|
||||
int *dataPtr = (int *)fileData;
|
||||
value = dataPtr[position];
|
||||
}
|
||||
UnloadFileData(fileData);
|
||||
TraceLog(LOG_INFO, "FILEIO: [%s] Loaded storage value: %i", fileName, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
Reference in New Issue
Block a user