textures_sprite_stacking.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - sprite stacking
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 6.0
  8. *
  9. * Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Redbooth model (c) 2017-2025 @kluchek under https://creativecommons.org/licenses/by/4.0/ https://github.com/kluchek/vox-models/
  15. * Copyright (c) 2025 Robin (@RobinsAviary)
  16. *
  17. ********************************************************************************************/
  18. #include "raylib.h"
  19. #include "raymath.h" // Required for: Clamp()
  20. //------------------------------------------------------------------------------------
  21. // Program main entry point
  22. //------------------------------------------------------------------------------------
  23. int main(void)
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite stacking");
  30. Texture2D booth = LoadTexture("resources/booth.png");
  31. float stackScale = 3.0f; // Overall scale of the stacked sprite
  32. float stackSpacing = 2.0f; // Vertical spacing between each layer
  33. unsigned int stackCount = 122; // Number of layers, used for calculating the size of a single slice
  34. float rotationSpeed = 30.0f; // Stacked sprites rotation speed
  35. float rotation = 0.0f; // Current rotation of the stacked sprite
  36. const float speedChange = 0.25f; // Amount speed will change by when the user presses A/D
  37. SetTargetFPS(60);
  38. //--------------------------------------------------------------------------------------
  39. // Main game loop
  40. while (!WindowShouldClose()) // Detect window close button or ESC key
  41. {
  42. // Update
  43. //----------------------------------------------------------------------------------
  44. // Use mouse wheel to affect stack separation
  45. stackSpacing += GetMouseWheelMove()*0.1f;
  46. stackSpacing = Clamp(stackSpacing, 0.0f, 5.0f);
  47. // Add a positive/negative offset to spin right/left at different speeds
  48. if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) rotationSpeed -= speedChange;
  49. if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) rotationSpeed += speedChange;
  50. rotation += rotationSpeed*GetFrameTime();
  51. //----------------------------------------------------------------------------------
  52. // Draw
  53. //----------------------------------------------------------------------------------
  54. BeginDrawing();
  55. ClearBackground(RAYWHITE);
  56. // Get the size of a single slice
  57. float frameWidth = (float)booth.width;
  58. float frameHeight = (float)booth.height/(float)stackCount;
  59. // Get the scaled resolution to draw at
  60. float scaledWidth = frameWidth*stackScale;
  61. float scaledHeight = frameHeight*stackScale;
  62. // Draw the stacked sprite, rotated to the correct angle, with an vertical offset applied based on its y location
  63. for (int i = stackCount - 1; i >= 0; i--)
  64. {
  65. // Center vertically
  66. Rectangle source = { 0.0f, (float)i*frameHeight, frameWidth, frameHeight };
  67. Rectangle dest = { screenWidth/2.0f, (screenHeight/2.0f) + (i*stackSpacing) - (stackSpacing*stackCount/2.0f), scaledWidth, scaledHeight };
  68. Vector2 origin = { scaledWidth/2.0f, scaledHeight/2.0f };
  69. DrawTexturePro(booth, source, dest, origin, rotation, WHITE);
  70. }
  71. DrawText("A/D to spin\nmouse wheel to change separation (aka 'angle')", 10, 10, 20, DARKGRAY);
  72. DrawText(TextFormat("current spacing: %.01f", stackSpacing), 10, 50, 20, DARKGRAY);
  73. DrawText(TextFormat("current speed: %.02f", rotationSpeed), 10, 70, 20, DARKGRAY);
  74. DrawText("redbooth model (c) kluchek under cc 4.0", 10, 420, 20, DARKGRAY);
  75. EndDrawing();
  76. //----------------------------------------------------------------------------------
  77. }
  78. // De-Initialization
  79. //--------------------------------------------------------------------------------------
  80. UnloadTexture(booth);
  81. CloseWindow(); // Close window and OpenGL context
  82. //--------------------------------------------------------------------------------------
  83. return 0;
  84. }