core_storage_values.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Storage save/load values
  4. *
  5. * This example has been created using raylib 1.4 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. // NOTE: Storage positions must start with 0, directly related to file memory layout
  13. typedef enum {
  14. STORAGE_POSITION_SCORE = 0,
  15. STORAGE_POSITION_HISCORE = 1
  16. } StorageData;
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [core] example - storage save/load values");
  24. int score = 0;
  25. int hiscore = 0;
  26. int framesCounter = 0;
  27. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  28. //--------------------------------------------------------------------------------------
  29. // Main game loop
  30. while (!WindowShouldClose()) // Detect window close button or ESC key
  31. {
  32. // Update
  33. //----------------------------------------------------------------------------------
  34. if (IsKeyPressed(KEY_R))
  35. {
  36. score = GetRandomValue(1000, 2000);
  37. hiscore = GetRandomValue(2000, 4000);
  38. }
  39. if (IsKeyPressed(KEY_ENTER))
  40. {
  41. SaveStorageValue(STORAGE_POSITION_SCORE, score);
  42. SaveStorageValue(STORAGE_POSITION_HISCORE, hiscore);
  43. }
  44. else if (IsKeyPressed(KEY_SPACE))
  45. {
  46. // NOTE: If requested position could not be found, value 0 is returned
  47. score = LoadStorageValue(STORAGE_POSITION_SCORE);
  48. hiscore = LoadStorageValue(STORAGE_POSITION_HISCORE);
  49. }
  50. framesCounter++;
  51. //----------------------------------------------------------------------------------
  52. // Draw
  53. //----------------------------------------------------------------------------------
  54. BeginDrawing();
  55. ClearBackground(RAYWHITE);
  56. DrawText(TextFormat("SCORE: %i", score), 280, 130, 40, MAROON);
  57. DrawText(TextFormat("HI-SCORE: %i", hiscore), 210, 200, 50, BLACK);
  58. DrawText(TextFormat("frames: %i", framesCounter), 10, 10, 20, LIME);
  59. DrawText("Press R to generate random numbers", 220, 40, 20, LIGHTGRAY);
  60. DrawText("Press ENTER to SAVE values", 250, 310, 20, LIGHTGRAY);
  61. DrawText("Press SPACE to LOAD values", 252, 350, 20, LIGHTGRAY);
  62. EndDrawing();
  63. //----------------------------------------------------------------------------------
  64. }
  65. // De-Initialization
  66. //--------------------------------------------------------------------------------------
  67. CloseWindow(); // Close window and OpenGL context
  68. //--------------------------------------------------------------------------------------
  69. return 0;
  70. }