You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.0 KiB

  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 { STORAGE_SCORE = 0, STORAGE_HISCORE } StorageData;
  14. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [core] example - storage save/load values");
  21. int score = 0;
  22. int hiscore = 0;
  23. int framesCounter = 0;
  24. SetTargetFPS(60);
  25. //--------------------------------------------------------------------------------------
  26. // Main game loop
  27. while (!WindowShouldClose()) // Detect window close button or ESC key
  28. {
  29. // Update
  30. //----------------------------------------------------------------------------------
  31. if (IsKeyPressed(KEY_R))
  32. {
  33. score = GetRandomValue(1000, 2000);
  34. hiscore = GetRandomValue(2000, 4000);
  35. }
  36. if (IsKeyPressed(KEY_ENTER))
  37. {
  38. StorageSaveValue(STORAGE_SCORE, score);
  39. StorageSaveValue(STORAGE_HISCORE, hiscore);
  40. }
  41. else if (IsKeyPressed(KEY_SPACE))
  42. {
  43. // NOTE: If requested position could not be found, value 0 is returned
  44. score = StorageLoadValue(STORAGE_SCORE);
  45. hiscore = StorageLoadValue(STORAGE_HISCORE);
  46. }
  47. framesCounter++;
  48. //----------------------------------------------------------------------------------
  49. // Draw
  50. //----------------------------------------------------------------------------------
  51. BeginDrawing();
  52. ClearBackground(RAYWHITE);
  53. DrawText(FormatText("SCORE: %i", score), 280, 130, 40, MAROON);
  54. DrawText(FormatText("HI-SCORE: %i", hiscore), 210, 200, 50, BLACK);
  55. DrawText(FormatText("frames: %i", framesCounter), 10, 10, 20, LIME);
  56. DrawText("Press R to generate random numbers", 220, 40, 20, LIGHTGRAY);
  57. DrawText("Press ENTER to SAVE values", 250, 310, 20, LIGHTGRAY);
  58. DrawText("Press SPACE to LOAD values", 252, 350, 20, LIGHTGRAY);
  59. EndDrawing();
  60. //----------------------------------------------------------------------------------
  61. }
  62. // De-Initialization
  63. //--------------------------------------------------------------------------------------
  64. CloseWindow(); // Close window and OpenGL context
  65. //--------------------------------------------------------------------------------------
  66. return 0;
  67. }