Browse Source

[examples] Added: `textures_sprite_stacking` example (#5345)

* Added sprite stacking example

* added link

* formatting
pull/5348/head
Robin ❤️ 3 weeks ago
committed by GitHub
parent
commit
d8601121da
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
3 changed files with 115 additions and 0 deletions
  1. BIN
      examples/textures/resources/booth.png
  2. +115
    -0
      examples/textures/textures_sprite_stacking.c
  3. BIN
      examples/textures/textures_sprite_stacking.png

BIN
examples/textures/resources/booth.png View File

Before After
Width: 112  |  Height: 11468  |  Size: 62 KiB

+ 115
- 0
examples/textures/textures_sprite_stacking.c View File

@ -0,0 +1,115 @@
/*******************************************************************************************
*
* raylib [textures] example - sprite stacking
*
* Example complexity rating: [] 2/4
*
* Example originally created with raylib 5.6-dev, last time updated with raylib 6.0
*
* Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Redbooth model (c) 2017-2025 @kluchek under https://creativecommons.org/licenses/by/4.0/ https://github.com/kluchek/vox-models/
* Copyright (c) 2025-2025 Robin (@RobinsAviary)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite stacking");
Texture2D booth = LoadTexture("resources/booth.png");
// The overall scale of the stacked sprite
float stackScale = 3.0f;
// The vertical spacing between each layer
float stackSpacing = 2.0f;
// The number of layers. Used for calculating the size of a single slice
unsigned int stackCount = 122;
// The speed to rotate the stacked sprite
float rotationSpeed = 30.0f;
// The current rotation of the stacked sprite
float rotation = 0.0f;
// The amount that speed will change by when the user presses A/D
const float speedChange = 0.25f;
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Use mouse wheel to affect stack separation
stackSpacing += GetMouseWheelMove()*0.1f;
stackSpacing = Clamp(stackSpacing, 0.0f, 5.0f);
// Add a positive/negative offset to spin right/left at different speeds
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A))
{
rotationSpeed -= speedChange;
}
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D))
{
rotationSpeed += speedChange;
}
rotation += rotationSpeed * GetFrameTime();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Get the size of a single slice
float frameWidth = (float)booth.width;
float frameHeight = (float)booth.height/(float)stackCount;
// Get the scaled resolution to draw at
float scaledWidth = frameWidth*stackScale;
float scaledHeight = frameHeight*stackScale;
// Draw the stacked sprite, rotated to the correct angle, with an vertical offset applied based on its y location
for (int i = stackCount - 1; i >= 0; i--)
{
Rectangle source = { 0.0f, (float)i*frameHeight, frameWidth, frameHeight };
// Center vertically
Rectangle dest = { screenWidth/2.0f, (screenHeight/2.0f) + (i*stackSpacing) - (stackSpacing*stackCount/2.0f), scaledWidth, scaledHeight };
Vector2 origin = { scaledWidth/2.0f, scaledHeight/2.0f };
DrawTexturePro(booth, source, dest, origin, rotation, WHITE);
}
DrawText("a/d to spin\nmouse wheel to change separation (aka 'angle')", 10, 10, 20, DARKGRAY);
const char *spacingText = TextFormat("current spacing: %.01f", stackSpacing);
DrawText(spacingText, 10, 50, 20, DARKGRAY);
const char *speedText = TextFormat("current speed: %.02f", rotationSpeed);
DrawText(speedText, 10, 70, 20, DARKGRAY);
DrawText("redbooth model (c) kluchek under cc 4.0", 10, 420, 20, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(booth);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

BIN
examples/textures/textures_sprite_stacking.png View File

Before After
Width: 800  |  Height: 450  |  Size: 51 KiB

Loading…
Cancel
Save