From 8e229ada1dcaf5c0c5a1c8172def3e06eeea8a91 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 7 Jul 2022 12:15:55 +0200 Subject: ADDED: example: `textures_gif_player` --- examples/Makefile | 2 +- examples/README.md | 2 +- examples/textures/resources/scarfy_run.gif | Bin 0 -> 21261 bytes examples/textures/textures_gif_player.c | 119 +++++++++++++++++++++++++++++ examples/textures/textures_gif_player.png | Bin 0 -> 19690 bytes 5 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 examples/textures/resources/scarfy_run.gif create mode 100644 examples/textures/textures_gif_player.c create mode 100644 examples/textures/textures_gif_player.png (limited to 'examples') diff --git a/examples/Makefile b/examples/Makefile index 8517a91c..82e41f63 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -431,7 +431,7 @@ TEXTURES = \ textures/textures_blend_modes \ textures/textures_draw_tiled \ textures/textures_polygon \ - textures/textures_gif_anim + textures/textures_gif_player TEXT = \ text/text_raylib_fonts \ diff --git a/examples/README.md b/examples/README.md index 6fb65514..4769ff55 100644 --- a/examples/README.md +++ b/examples/README.md @@ -96,7 +96,7 @@ Examples using raylib textures functionality, including image/textures loading/g | 57 | [textures_blend_modes](textures/textures_blend_modes.c) | textures_blend_modes | [Karlo Licudine](https://github.com/accidentalrebel) | | | 58 | [textures_draw_tiled](textures/textures_draw_tiled.c) | textures_draw_tiled | [Vlad Adrian](https://github.com/demizdor) | | | -- | [textures_poly](textures/textures_poly.c) | textures_poly | [Chris Camacho](https://github.com/codifies) | ⭐️ | -| -- | [textures_gif_anim](textures/textures_gif_anim.c) | textures_gif_anim | [Chris Camacho](https://github.com/codifies) | ⭐️ | +| -- | [textures_gif_player](textures/textures_gif_player.c) | textures_gif_player | [Chris Camacho](https://github.com/codifies) | ⭐️ | ### category: text diff --git a/examples/textures/resources/scarfy_run.gif b/examples/textures/resources/scarfy_run.gif new file mode 100644 index 00000000..f0f712cc Binary files /dev/null and b/examples/textures/resources/scarfy_run.gif differ diff --git a/examples/textures/textures_gif_player.c b/examples/textures/textures_gif_player.c new file mode 100644 index 00000000..0bb42f16 --- /dev/null +++ b/examples/textures/textures_gif_player.c @@ -0,0 +1,119 @@ +/******************************************************************************************* +* +* raylib [textures] example - gif playing +* +* This example has been created using raylib 4.2 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2021-2022 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#define MAX_FRAME_DELAY 20 +#define MIN_FRAME_DELAY 1 + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [textures] example - gif playing"); + + int animFrames = 0; + + // Load all GIF animation frames into a single Image + // NOTE: GIF data is always loaded as RGBA (32bit) by default + // NOTE: Frames are just appended one after another in image.data memory + Image imScarfyAnim = LoadImageAnim("resources/scarfy_run.gif", &animFrames); + + // Load texture from image + // NOTE: We will update this texture when required with next frame data + // WARNING: It's not recommended to use this technique for sprites animation, + // use spritesheets instead, like illustrated in textures_sprite_anim example + Texture2D texScarfyAnim = LoadTextureFromImage(imScarfyAnim); + + unsigned int nextFrameDataOffset = 0; // Current byte offset to next frame in image.data + + int currentAnimFrame = 0; // Current animation frame to load and draw + int frameDelay = 8; // Frame delay to switch between animation frames + int frameCounter = 0; // General frames counter + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + frameCounter++; + if (frameCounter >= frameDelay) + { + // Move to next frame + // NOTE: If final frame is reached we return to first frame + currentAnimFrame++; + if (currentAnimFrame >= animFrames) currentAnimFrame = 0; + + // Get memory offset position for next frame data in image.data + nextFrameDataOffset = imScarfyAnim.width*imScarfyAnim.height*4*currentAnimFrame; + + // Update GPU texture data with next frame image data + // WARNING: Data size (frame size) and pixel format must match already created texture + UpdateTexture(texScarfyAnim, ((unsigned char *)imScarfyAnim.data) + nextFrameDataOffset); + + frameCounter = 0; + } + + // Control frames delay + if (IsKeyPressed(KEY_RIGHT)) frameDelay++; + else if (IsKeyPressed(KEY_LEFT)) frameDelay--; + + if (frameDelay > MAX_FRAME_DELAY) frameDelay = MAX_FRAME_DELAY; + else if (frameDelay < MIN_FRAME_DELAY) frameDelay = MIN_FRAME_DELAY; + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText(TextFormat("TOTAL GIF FRAMES: %02i", animFrames), 50, 30, 20, LIGHTGRAY); + DrawText(TextFormat("CURRENT FRAME: %02i", currentAnimFrame), 50, 60, 20, GRAY); + DrawText(TextFormat("CURRENT FRAME IMAGE.DATA OFFSET: %02i", nextFrameDataOffset), 50, 90, 20, GRAY); + + DrawText("FRAMES DELAY: ", 100, 305, 10, DARKGRAY); + DrawText(TextFormat("%02i frames", frameDelay), 620, 305, 10, DARKGRAY); + DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 350, 10, DARKGRAY); + + for (int i = 0; i < MAX_FRAME_DELAY; i++) + { + if (i < frameDelay) DrawRectangle(190 + 21*i, 300, 20, 20, RED); + DrawRectangleLines(190 + 21*i, 300, 20, 20, MAROON); + } + + DrawTexture(texScarfyAnim, GetScreenWidth()/2 - texScarfyAnim.width/2, 140, WHITE); + + DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadTexture(texScarfyAnim); // Unload texture + UnloadImage(imScarfyAnim); // Unload image (contains all frames) + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/examples/textures/textures_gif_player.png b/examples/textures/textures_gif_player.png new file mode 100644 index 00000000..20db76ed Binary files /dev/null and b/examples/textures/textures_gif_player.png differ -- cgit v1.2.3