textures_sprite_animation.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - sprite animation
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 1.3, last time updated with raylib 1.3
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #define MAX_FRAME_SPEED 15
  17. #define MIN_FRAME_SPEED 1
  18. //------------------------------------------------------------------------------------
  19. // Program main entry point
  20. //------------------------------------------------------------------------------------
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite animation");
  28. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  29. Texture2D scarfy = LoadTexture("resources/scarfy.png"); // Texture loading
  30. Vector2 position = { 350.0f, 280.0f };
  31. Rectangle frameRec = { 0.0f, 0.0f, (float)scarfy.width/6, (float)scarfy.height };
  32. int currentFrame = 0;
  33. int framesCounter = 0;
  34. int framesSpeed = 8; // Number of spritesheet frames shown by second
  35. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. framesCounter++;
  43. if (framesCounter >= (60/framesSpeed))
  44. {
  45. framesCounter = 0;
  46. currentFrame++;
  47. if (currentFrame > 5) currentFrame = 0;
  48. frameRec.x = (float)currentFrame*(float)scarfy.width/6;
  49. }
  50. // Control frames speed
  51. if (IsKeyPressed(KEY_RIGHT)) framesSpeed++;
  52. else if (IsKeyPressed(KEY_LEFT)) framesSpeed--;
  53. if (framesSpeed > MAX_FRAME_SPEED) framesSpeed = MAX_FRAME_SPEED;
  54. else if (framesSpeed < MIN_FRAME_SPEED) framesSpeed = MIN_FRAME_SPEED;
  55. //----------------------------------------------------------------------------------
  56. // Draw
  57. //----------------------------------------------------------------------------------
  58. BeginDrawing();
  59. ClearBackground(RAYWHITE);
  60. DrawTexture(scarfy, 15, 40, WHITE);
  61. DrawRectangleLines(15, 40, scarfy.width, scarfy.height, LIME);
  62. DrawRectangleLines(15 + (int)frameRec.x, 40 + (int)frameRec.y, (int)frameRec.width, (int)frameRec.height, RED);
  63. DrawText("FRAME SPEED: ", 165, 210, 10, DARKGRAY);
  64. DrawText(TextFormat("%02i FPS", framesSpeed), 575, 210, 10, DARKGRAY);
  65. DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, DARKGRAY);
  66. for (int i = 0; i < MAX_FRAME_SPEED; i++)
  67. {
  68. if (i < framesSpeed) DrawRectangle(250 + 21*i, 205, 20, 20, RED);
  69. DrawRectangleLines(250 + 21*i, 205, 20, 20, MAROON);
  70. }
  71. DrawTextureRec(scarfy, frameRec, position, WHITE); // Draw part of the texture
  72. DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY);
  73. EndDrawing();
  74. //----------------------------------------------------------------------------------
  75. }
  76. // De-Initialization
  77. //--------------------------------------------------------------------------------------
  78. UnloadTexture(scarfy); // Texture unloading
  79. CloseWindow(); // Close window and OpenGL context
  80. //--------------------------------------------------------------------------------------
  81. return 0;
  82. }