diff --git a/examples/core_storage_values.c b/examples/core_storage_values.c new file mode 100644 index 000000000..3190d0a08 --- /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 000000000..6cfd552dd Binary files /dev/null and b/examples/core_storage_values.png differ diff --git a/src/core.c b/src/core.c index 14510a1bc..06260281d 100644 --- a/src/core.c +++ b/src/core.c @@ -708,6 +708,67 @@ void ClearDroppedFiles(void) } #endif +// Storage save integer value (to defined position) +// NOTE: Storage positions is directly related to file memory layout (4 bytes each integer) +void StorageSaveValue(int position, int value) +{ + FILE *storageFile = NULL; + + // Try open existing file to append data + storageFile = fopen("storage.data", "rb+"); + + // If file doesn't exist, create a new storage data file + if (!storageFile) storageFile = fopen("storage.data", "wb"); + + if (!storageFile) TraceLog(WARNING, "Storage data file could not be created"); + else + { + // Get file size + fseek(storageFile, 0, SEEK_END); + int fileSize = ftell(storageFile); // Size in bytes + fseek(storageFile, 0, SEEK_SET); + + if (fileSize < (position*4)) TraceLog(WARNING, "Storage position could not be found"); + else + { + fseek(storageFile, (position*4), SEEK_SET); + fwrite(&value, 1, 4, storageFile); + } + + fclose(storageFile); + } +} + +// Storage load integer value (from defined position) +// NOTE: If requested position could not be found, value 0 is returned +int StorageLoadValue(int position) +{ + int value = 0; + + // Try open existing file to append data + FILE *storageFile = fopen("storage.data", "rb"); + + if (!storageFile) TraceLog(WARNING, "Storage data file could not be found"); + else + { + // Get file size + fseek(storageFile, 0, SEEK_END); + int fileSize = ftell(storageFile); // Size in bytes + rewind(storageFile); + + if (fileSize < (position*4)) TraceLog(WARNING, "Storage position could not be found"); + else + { + fseek(storageFile, (position*4), SEEK_SET); + fread(&value, 1, 4, storageFile); + } + + fclose(storageFile); + } + + return value; +} + // TODO: Gives the ray trace from mouse position Ray GetMouseRay(Vector2 mousePosition, Camera camera) { diff --git a/src/raylib.h b/src/raylib.h index 99a6979cd..f12035370 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -540,6 +540,9 @@ bool IsFileDropped(void); // Check if a file h char **GetDroppedFiles(int *count); // Retrieve dropped files into window void ClearDroppedFiles(void); // Clear dropped files paths buffer +void StorageSaveValue(int position, int value); // Storage save integer value (to defined position) +int StorageLoadValue(int position); // Storage load integer value (from defined position) + //------------------------------------------------------------------------------------ // Input Handling Functions (Module: core) //------------------------------------------------------------------------------------ diff --git a/src/rlgl.c b/src/rlgl.c index 8a0440e04..504381b2a 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1921,40 +1921,31 @@ void rlglUpdateTexture(unsigned int id, int width, int height, int format, void } // Generate mipmap data for selected texture -void rlglGenerateMipmaps(unsigned int textureId) +void rlglGenerateMipmaps(Texture2D texture) { - glBindTexture(GL_TEXTURE_2D, textureId); + glBindTexture(GL_TEXTURE_2D, texture.id); // Check if texture is power-of-two (POT) bool texIsPOT = false; - // NOTE: In OpenGL ES 2.0 we have no way to retrieve texture size from id - -#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) - int width, height; - - glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width); - glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height); - - if (((width > 0) && ((width & (width - 1)) == 0)) && ((height > 0) && ((height & (height - 1)) == 0))) texIsPOT = true; -#endif + if (((texture.width > 0) && ((texture.width & (texture.width - 1)) == 0)) && + ((texture.height > 0) && ((texture.height & (texture.height - 1)) == 0))) texIsPOT = true; if ((texIsPOT) || (npotSupported)) { #if defined(GRAPHICS_API_OPENGL_11) // Compute required mipmaps - // TODO: rlglReadTexturePixels() needs Texture2D type parameter, not unsigned int parameter - void *data; // = rlglReadTexturePixels(textureId, UNCOMPRESSED_R8G8B8A8); // TODO: Detect internal format + void *data = rlglReadTexturePixels(texture); // NOTE: data size is reallocated to fit mipmaps data - int mipmapCount = GenerateMipmaps(data, width, height); + int mipmapCount = GenerateMipmaps(data, texture.width, texture.height); // TODO: Adjust mipmap size depending on texture format! - int size = width*height*4; + int size = texture.width*texture.height*4; int offset = size; - int mipWidth = width/2; - int mipHeight = height/2; + int mipWidth = texture.width/2; + int mipHeight = texture.height/2; // Load the mipmaps for (int level = 1; level < mipmapCount; level++) @@ -1968,17 +1959,17 @@ void rlglGenerateMipmaps(unsigned int textureId) mipHeight /= 2; } - TraceLog(WARNING, "[TEX ID %i] Mipmaps generated manually on CPU side", textureId); + TraceLog(WARNING, "[TEX ID %i] Mipmaps generated manually on CPU side", texture.id); #elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glGenerateMipmap(GL_TEXTURE_2D); // Generate mipmaps automatically - TraceLog(INFO, "[TEX ID %i] Mipmaps generated automatically", textureId); + TraceLog(INFO, "[TEX ID %i] Mipmaps generated automatically", texture.id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Activate Trilinear filtering for mipmaps (must be available) #endif } - else TraceLog(WARNING, "[TEX ID %i] Mipmaps can not be generated", textureId); + else TraceLog(WARNING, "[TEX ID %i] Mipmaps can not be generated", texture.id); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/src/rlgl.h b/src/rlgl.h index a7df043ed..93b56bb2c 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -246,7 +246,7 @@ void rlglInitGraphics(int offsetX, int offsetY, int width, int height); // Init unsigned int rlglLoadTexture(void *data, int width, int height, int textureFormat, int mipmapCount); // Load texture in GPU void rlglUpdateTexture(unsigned int id, int width, int height, int format, void *data); // Update GPU texture with new data -void rlglGenerateMipmaps(unsigned int textureId); // Generate mipmap data for selected texture +void rlglGenerateMipmaps(Texture2D texture); // Generate mipmap data for selected texture // NOTE: There is a set of shader related functions that are available to end user, // to avoid creating function wrappers through core module, they have been directly declared in raylib.h diff --git a/src/textures.c b/src/textures.c index 30ffb9458..f03d2d9ad 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1271,9 +1271,9 @@ void GenTextureMipmaps(Texture2D texture) { TraceLog(WARNING, "Limited NPOT support, no mipmaps available for NPOT textures"); } - else rlglGenerateMipmaps(texture.id); + else rlglGenerateMipmaps(texture); #else - rlglGenerateMipmaps(texture.id); + rlglGenerateMipmaps(texture); #endif }