diff --git a/include/app.h b/include/app.h index 0660337..b9f0429 100644 --- a/include/app.h +++ b/include/app.h @@ -8,6 +8,7 @@ // DEFINES// #define TEXT_COLOR_DARK (Color){182, 137, 98, 255} #define TEXT_COLOR_LIGHT (Color){243, 229, 194, 255} +#define STORAGE_DATA_FILE "save1.data" // ENUMS// typedef enum { @@ -38,6 +39,12 @@ typedef enum { GAMEPLAY_THEME, AMBIENT, SOUNDTRACK_COUNT } SoundtrackID; typedef enum View { LOGO = 0, MENU = 1, GAMEPLAY = 2 } View; +typedef enum { + STORAGE_POSITION_GOLD = 0, + STORAGE_POSITION_POSX = 1, + STORAGE_POSITION_POSY = 2 +} StorageData; + // STRUCTS// typedef struct { int frameCounter; @@ -101,6 +108,7 @@ typedef struct { Physics physics; float rotation; Sound soundFlapping; + int gold; } Player; typedef enum { BOAT, ENEMIES_TEX_COUNT } enemiesTextureID; diff --git a/include/engine.h b/include/engine.h index 6aa987d..98f5f04 100644 --- a/include/engine.h +++ b/include/engine.h @@ -237,5 +237,8 @@ float Vector2ToRotation(Vector2 vec); Vector2 GetPositionInsideRectangle(Rectangle rec); Vector2 GetPositionOutsideRectangle(Rectangle rec, int min, int max); +bool SaveStorageValue(const char* fileName, unsigned int position, int value); +int LoadStorageValue(const char* fileName, unsigned int position); + #endif //ENGINE_H diff --git a/src/appGame.c b/src/appGame.c index 04c3f37..8b2d934 100644 --- a/src/appGame.c +++ b/src/appGame.c @@ -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(); diff --git a/src/appGamePlayer.c b/src/appGamePlayer.c index 8257739..96ca894 100644 --- a/src/appGamePlayer.c +++ b/src/appGamePlayer.c @@ -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) diff --git a/src/appGameProjectiles.c b/src/appGameProjectiles.c index 469dddd..ecccde2 100644 --- a/src/appGameProjectiles.c +++ b/src/appGameProjectiles.c @@ -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; } } diff --git a/src/engineStorage.c b/src/engineStorage.c new file mode 100644 index 0000000..1a602a9 --- /dev/null +++ b/src/engineStorage.c @@ -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; +}