core_storage_values.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Storage save/load values
  4. *
  5. * Example originally created with raylib 1.4, last time updated with raylib 4.2
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include <stdlib.h> // Required for: calloc(), free()
  15. #define STORAGE_DATA_FILE "storage.data" // Storage file
  16. // NOTE: Storage positions must start with 0, directly related to file memory layout
  17. typedef enum {
  18. STORAGE_POSITION_SCORE = 0,
  19. STORAGE_POSITION_HISCORE = 1
  20. } StorageData;
  21. // Persistent storage functions
  22. static bool SaveStorageValue(unsigned int position, int value);
  23. static int LoadStorageValue(unsigned int position);
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. InitWindow(screenWidth, screenHeight, "raylib [core] example - storage save/load values");
  34. int score = 0;
  35. int hiscore = 0;
  36. int framesCounter = 0;
  37. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  38. //--------------------------------------------------------------------------------------
  39. // Main game loop
  40. while (!WindowShouldClose()) // Detect window close button or ESC key
  41. {
  42. // Update
  43. //----------------------------------------------------------------------------------
  44. if (IsKeyPressed(KEY_R))
  45. {
  46. score = GetRandomValue(1000, 2000);
  47. hiscore = GetRandomValue(2000, 4000);
  48. }
  49. if (IsKeyPressed(KEY_ENTER))
  50. {
  51. SaveStorageValue(STORAGE_POSITION_SCORE, score);
  52. SaveStorageValue(STORAGE_POSITION_HISCORE, hiscore);
  53. }
  54. else if (IsKeyPressed(KEY_SPACE))
  55. {
  56. // NOTE: If requested position could not be found, value 0 is returned
  57. score = LoadStorageValue(STORAGE_POSITION_SCORE);
  58. hiscore = LoadStorageValue(STORAGE_POSITION_HISCORE);
  59. }
  60. framesCounter++;
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. DrawText(TextFormat("SCORE: %i", score), 280, 130, 40, MAROON);
  67. DrawText(TextFormat("HI-SCORE: %i", hiscore), 210, 200, 50, BLACK);
  68. DrawText(TextFormat("frames: %i", framesCounter), 10, 10, 20, LIME);
  69. DrawText("Press R to generate random numbers", 220, 40, 20, LIGHTGRAY);
  70. DrawText("Press ENTER to SAVE values", 250, 310, 20, LIGHTGRAY);
  71. DrawText("Press SPACE to LOAD values", 252, 350, 20, LIGHTGRAY);
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. CloseWindow(); // Close window and OpenGL context
  78. //--------------------------------------------------------------------------------------
  79. return 0;
  80. }
  81. // Save integer value to storage file (to defined position)
  82. // NOTE: Storage positions is directly related to file memory layout (4 bytes each integer)
  83. bool SaveStorageValue(unsigned int position, int value)
  84. {
  85. bool success = false;
  86. int dataSize = 0;
  87. unsigned int newDataSize = 0;
  88. unsigned char *fileData = LoadFileData(STORAGE_DATA_FILE, &dataSize);
  89. unsigned char *newFileData = NULL;
  90. if (fileData != NULL)
  91. {
  92. if (dataSize <= (position*sizeof(int)))
  93. {
  94. // Increase data size up to position and store value
  95. newDataSize = (position + 1)*sizeof(int);
  96. newFileData = (unsigned char *)RL_REALLOC(fileData, newDataSize);
  97. if (newFileData != NULL)
  98. {
  99. // RL_REALLOC succeded
  100. int *dataPtr = (int *)newFileData;
  101. dataPtr[position] = value;
  102. }
  103. else
  104. {
  105. // RL_REALLOC failed
  106. 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));
  107. // We store the old size of the file
  108. newFileData = fileData;
  109. newDataSize = dataSize;
  110. }
  111. }
  112. else
  113. {
  114. // Store the old size of the file
  115. newFileData = fileData;
  116. newDataSize = dataSize;
  117. // Replace value on selected position
  118. int *dataPtr = (int *)newFileData;
  119. dataPtr[position] = value;
  120. }
  121. success = SaveFileData(STORAGE_DATA_FILE, newFileData, newDataSize);
  122. RL_FREE(newFileData);
  123. TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", STORAGE_DATA_FILE, value);
  124. }
  125. else
  126. {
  127. TraceLog(LOG_INFO, "FILEIO: [%s] File created successfully", STORAGE_DATA_FILE);
  128. dataSize = (position + 1)*sizeof(int);
  129. fileData = (unsigned char *)RL_MALLOC(dataSize);
  130. int *dataPtr = (int *)fileData;
  131. dataPtr[position] = value;
  132. success = SaveFileData(STORAGE_DATA_FILE, fileData, dataSize);
  133. UnloadFileData(fileData);
  134. TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", STORAGE_DATA_FILE, value);
  135. }
  136. return success;
  137. }
  138. // Load integer value from storage file (from defined position)
  139. // NOTE: If requested position could not be found, value 0 is returned
  140. int LoadStorageValue(unsigned int position)
  141. {
  142. int value = 0;
  143. int dataSize = 0;
  144. unsigned char *fileData = LoadFileData(STORAGE_DATA_FILE, &dataSize);
  145. if (fileData != NULL)
  146. {
  147. if (dataSize < ((int)(position*4))) TraceLog(LOG_WARNING, "FILEIO: [%s] Failed to find storage position: %i", STORAGE_DATA_FILE, position);
  148. else
  149. {
  150. int *dataPtr = (int *)fileData;
  151. value = dataPtr[position];
  152. }
  153. UnloadFileData(fileData);
  154. TraceLog(LOG_INFO, "FILEIO: [%s] Loaded storage value: %i", STORAGE_DATA_FILE, value);
  155. }
  156. return value;
  157. }