core_storage_values.c 7.1 KB

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