From 70d405b41bcbbd73b9f752f4dc3910100abd1a36 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 4 Jan 2016 15:12:34 +0100 Subject: [PATCH] Added functionality: Storage values Two new functions added to save/load values as binary data: - StorageSaveValue() - StorageLoadValue() --- examples/core_storage_values.c | 85 +++++++++++++++++++++++++++++++ examples/core_storage_values.png | Bin 0 -> 16147 bytes src/core.c | 61 ++++++++++++++++++++++ src/raylib.h | 3 ++ 4 files changed, 149 insertions(+) create mode 100644 examples/core_storage_values.c create mode 100644 examples/core_storage_values.png diff --git a/examples/core_storage_values.c b/examples/core_storage_values.c new file mode 100644 index 00000000..3190d0a0 --- /dev/null +++ b/examples/core_storage_values.c @@ -0,0 +1,85 @@ +/******************************************************************************************* +* +* raylib [core] example - Storage save/load values +* +* This example has been created using raylib 1.4 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2014 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +// NOTE: Storage positions must start with 0, directly related to file memory layout +typedef enum { STORAGE_SCORE = 0, STORAGE_HISCORE } StorageData; + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - storage save/load values"); + + int score = 0; + int hiscore = 0; + + int framesCounter = 0; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (IsKeyPressed(KEY_R)) + { + score = GetRandomValue(1000, 2000); + hiscore = GetRandomValue(2000, 4000); + } + + if (IsKeyPressed(KEY_ENTER)) + { + StorageSaveValue(STORAGE_SCORE, score); + StorageSaveValue(STORAGE_HISCORE, hiscore); + } + else if (IsKeyPressed(KEY_SPACE)) + { + // NOTE: If requested position could not be found, value 0 is returned + score = StorageLoadValue(STORAGE_SCORE); + hiscore = StorageLoadValue(STORAGE_HISCORE); + } + + framesCounter++; + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText(FormatText("SCORE: %i", score), 280, 130, 40, MAROON); + DrawText(FormatText("HI-SCORE: %i", hiscore), 210, 200, 50, BLACK); + + DrawText(FormatText("frames: %i", framesCounter), 10, 10, 20, LIME); + + DrawText("Press R to generate random numbers", 220, 40, 20, LIGHTGRAY); + DrawText("Press ENTER to SAVE values", 250, 310, 20, LIGHTGRAY); + DrawText("Press SPACE to LOAD values", 252, 350, 20, LIGHTGRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/examples/core_storage_values.png b/examples/core_storage_values.png new file mode 100644 index 0000000000000000000000000000000000000000..6cfd552dd63f24c854877db10f4e77cc6e6b4367 GIT binary patch literal 16147 zcmeHOdt8$D8b=@{4WleCq##jNryJ#^(om8_omr%{m8CM9lg!FFG?Vg*s5^K~vs^YD zYI;i3CQU7uhFh(Rlcedoonu~xnz{FT?*@UuOd$}B%Lq;2iovd;s}P9o9q0AywuE}^dOqCO6~W$2 zKtp*rB`*wwM!t}WK}HEth6-Oa!eXoGTAx`IC9O^E8L)l zKf78y?J7M?)&5h~!lJt{IzgTrIuW*2k8%bIy>si}o2|cOVUi?Y#R)GewD~zuYAsb# z7ldo;S1eo5XRG7Zopbx-tfm_p{DEjryE0cv!V#=tE3=9n$utW%MZt?$AG#!F{+ZoX zT2W0zlibP}4|A-<`ldhQ>=k;)N=52T0dF~~=x(xWz%4o6m%>LjarW|Dvbt|4pim=v7ksz{bcL319l!8XE93KAwwh}$0^cOrFX*YtitOo5$R{9$T;HkP- z={|E5B0{QqAX4NjGXXNH*#OEjH{}(EDK-Ix9JvoikZvU4FD}9oUaGa!YNSi3@-38#19x6C%kBh4;8)fUB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