core_storage_values.c 6.7 KB

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