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 | |
12 | #include "raylib.h" |
13 | |
14 | // NOTE: Storage positions must start with 0, directly related to file memory layout |
15 | typedef enum { |
16 | STORAGE_POSITION_SCORE = 0, |
17 | STORAGE_POSITION_HISCORE = 1 |
18 | } StorageData; |
19 | |
20 | int main(void) |
21 | { |
22 | // Initialization |
23 | //-------------------------------------------------------------------------------------- |
24 | const int screenWidth = 800; |
25 | const int screenHeight = 450; |
26 | |
27 | InitWindow(screenWidth, screenHeight, "raylib [core] example - storage save/load values" ); |
28 | |
29 | int score = 0; |
30 | int hiscore = 0; |
31 | int framesCounter = 0; |
32 | |
33 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
34 | //-------------------------------------------------------------------------------------- |
35 | |
36 | // Main game loop |
37 | while (!WindowShouldClose()) // Detect window close button or ESC key |
38 | { |
39 | // Update |
40 | //---------------------------------------------------------------------------------- |
41 | if (IsKeyPressed(KEY_R)) |
42 | { |
43 | score = GetRandomValue(1000, 2000); |
44 | hiscore = GetRandomValue(2000, 4000); |
45 | } |
46 | |
47 | if (IsKeyPressed(KEY_ENTER)) |
48 | { |
49 | SaveStorageValue(STORAGE_POSITION_SCORE, score); |
50 | SaveStorageValue(STORAGE_POSITION_HISCORE, hiscore); |
51 | } |
52 | else if (IsKeyPressed(KEY_SPACE)) |
53 | { |
54 | // NOTE: If requested position could not be found, value 0 is returned |
55 | score = LoadStorageValue(STORAGE_POSITION_SCORE); |
56 | hiscore = LoadStorageValue(STORAGE_POSITION_HISCORE); |
57 | } |
58 | |
59 | framesCounter++; |
60 | //---------------------------------------------------------------------------------- |
61 | |
62 | // Draw |
63 | //---------------------------------------------------------------------------------- |
64 | BeginDrawing(); |
65 | |
66 | ClearBackground(RAYWHITE); |
67 | |
68 | DrawText(FormatText("SCORE: %i" , score), 280, 130, 40, MAROON); |
69 | DrawText(FormatText("HI-SCORE: %i" , hiscore), 210, 200, 50, BLACK); |
70 | |
71 | DrawText(FormatText("frames: %i" , framesCounter), 10, 10, 20, LIME); |
72 | |
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 | |
77 | EndDrawing(); |
78 | //---------------------------------------------------------------------------------- |
79 | } |
80 | |
81 | // De-Initialization |
82 | //-------------------------------------------------------------------------------------- |
83 | CloseWindow(); // Close window and OpenGL context |
84 | //-------------------------------------------------------------------------------------- |
85 | |
86 | return 0; |
87 | } |