選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

120 行
5.2 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - gif playing
  4. *
  5. * Example originally created with raylib 4.2, last time updated with raylib 4.2
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2021-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define MAX_FRAME_DELAY 20
  15. #define MIN_FRAME_DELAY 1
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [textures] example - gif playing");
  26. int animFrames = 0;
  27. // Load all GIF animation frames into a single Image
  28. // NOTE: GIF data is always loaded as RGBA (32bit) by default
  29. // NOTE: Frames are just appended one after another in image.data memory
  30. Image imScarfyAnim = LoadImageAnim("resources/scarfy_run.gif", &animFrames);
  31. // Load texture from image
  32. // NOTE: We will update this texture when required with next frame data
  33. // WARNING: It's not recommended to use this technique for sprites animation,
  34. // use spritesheets instead, like illustrated in textures_sprite_anim example
  35. Texture2D texScarfyAnim = LoadTextureFromImage(imScarfyAnim);
  36. unsigned int nextFrameDataOffset = 0; // Current byte offset to next frame in image.data
  37. int currentAnimFrame = 0; // Current animation frame to load and draw
  38. int frameDelay = 8; // Frame delay to switch between animation frames
  39. int frameCounter = 0; // General frames counter
  40. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  41. //--------------------------------------------------------------------------------------
  42. // Main game loop
  43. while (!WindowShouldClose()) // Detect window close button or ESC key
  44. {
  45. // Update
  46. //----------------------------------------------------------------------------------
  47. frameCounter++;
  48. if (frameCounter >= frameDelay)
  49. {
  50. // Move to next frame
  51. // NOTE: If final frame is reached we return to first frame
  52. currentAnimFrame++;
  53. if (currentAnimFrame >= animFrames) currentAnimFrame = 0;
  54. // Get memory offset position for next frame data in image.data
  55. nextFrameDataOffset = imScarfyAnim.width*imScarfyAnim.height*4*currentAnimFrame;
  56. // Update GPU texture data with next frame image data
  57. // WARNING: Data size (frame size) and pixel format must match already created texture
  58. UpdateTexture(texScarfyAnim, ((unsigned char *)imScarfyAnim.data) + nextFrameDataOffset);
  59. frameCounter = 0;
  60. }
  61. // Control frames delay
  62. if (IsKeyPressed(KEY_RIGHT)) frameDelay++;
  63. else if (IsKeyPressed(KEY_LEFT)) frameDelay--;
  64. if (frameDelay > MAX_FRAME_DELAY) frameDelay = MAX_FRAME_DELAY;
  65. else if (frameDelay < MIN_FRAME_DELAY) frameDelay = MIN_FRAME_DELAY;
  66. //----------------------------------------------------------------------------------
  67. // Draw
  68. //----------------------------------------------------------------------------------
  69. BeginDrawing();
  70. ClearBackground(RAYWHITE);
  71. DrawText(TextFormat("TOTAL GIF FRAMES: %02i", animFrames), 50, 30, 20, LIGHTGRAY);
  72. DrawText(TextFormat("CURRENT FRAME: %02i", currentAnimFrame), 50, 60, 20, GRAY);
  73. DrawText(TextFormat("CURRENT FRAME IMAGE.DATA OFFSET: %02i", nextFrameDataOffset), 50, 90, 20, GRAY);
  74. DrawText("FRAMES DELAY: ", 100, 305, 10, DARKGRAY);
  75. DrawText(TextFormat("%02i frames", frameDelay), 620, 305, 10, DARKGRAY);
  76. DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 350, 10, DARKGRAY);
  77. for (int i = 0; i < MAX_FRAME_DELAY; i++)
  78. {
  79. if (i < frameDelay) DrawRectangle(190 + 21*i, 300, 20, 20, RED);
  80. DrawRectangleLines(190 + 21*i, 300, 20, 20, MAROON);
  81. }
  82. DrawTexture(texScarfyAnim, GetScreenWidth()/2 - texScarfyAnim.width/2, 140, WHITE);
  83. DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY);
  84. EndDrawing();
  85. //----------------------------------------------------------------------------------
  86. }
  87. // De-Initialization
  88. //--------------------------------------------------------------------------------------
  89. UnloadTexture(texScarfyAnim); // Unload texture
  90. UnloadImage(imScarfyAnim); // Unload image (contains all frames)
  91. CloseWindow(); // Close window and OpenGL context
  92. //--------------------------------------------------------------------------------------
  93. return 0;
  94. }