chore: clean recommit
This commit is contained in:
parent
6bb62d9932
commit
8739e3b347
134 changed files with 15442 additions and 10648 deletions
|
|
@ -1,11 +1,15 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Texture loading and drawing a part defined by a rectangle
|
||||
* raylib [textures] example - sprite animation
|
||||
*
|
||||
* This example has been created using raylib 1.3 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
|
||||
* Example originally created with raylib 1.3, last time updated with raylib 1.3
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
|
@ -15,108 +19,133 @@ using static Raylib_cs.Raylib;
|
|||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public class SpriteAnim
|
||||
public partial class SpriteAnim : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
public const int MaxFrameSpeed = 15;
|
||||
public const int MinFrameSpeed = 1;
|
||||
|
||||
public string Name => "Textures / Sprite Anim";
|
||||
|
||||
public string Title => "raylib [textures] example - sprite animation";
|
||||
|
||||
private Texture2D scarfy;
|
||||
private Vector2 position;
|
||||
private Rectangle frameRec;
|
||||
private int currentFrame;
|
||||
private int framesCounter;
|
||||
private int framesSpeed;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
||||
scarfy = LoadTexture("resources/scarfy.png"); // Texture loading
|
||||
|
||||
position = new(350.0f, 280.0f);
|
||||
frameRec = new(0.0f, 0.0f, (float)scarfy.Width / 6, (float)scarfy.Height);
|
||||
currentFrame = 0;
|
||||
|
||||
framesCounter = 0;
|
||||
framesSpeed = 8; // Number of spritesheet frames shown by second
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
framesCounter++;
|
||||
|
||||
if (framesCounter >= (60 / framesSpeed))
|
||||
{
|
||||
framesCounter = 0;
|
||||
currentFrame++;
|
||||
|
||||
if (currentFrame > 5)
|
||||
{
|
||||
currentFrame = 0;
|
||||
}
|
||||
|
||||
frameRec.X = (float)currentFrame * (float)scarfy.Width / 6;
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.Right))
|
||||
{
|
||||
framesSpeed++;
|
||||
}
|
||||
else if (IsKeyPressed(KeyboardKey.Left))
|
||||
{
|
||||
framesSpeed--;
|
||||
}
|
||||
|
||||
framesSpeed = Math.Clamp(framesSpeed, MinFrameSpeed, MaxFrameSpeed);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
DrawTexture(scarfy, 15, 40, Color.White);
|
||||
DrawRectangleLines(15, 40, scarfy.Width, scarfy.Height, Color.Lime);
|
||||
DrawRectangleLines(
|
||||
15 + (int)frameRec.X,
|
||||
40 + (int)frameRec.Y,
|
||||
(int)frameRec.Width,
|
||||
(int)frameRec.Height,
|
||||
Color.Red
|
||||
);
|
||||
|
||||
DrawText("FRAME SPEED: ", 165, 210, 10, Color.DarkGray);
|
||||
DrawText($"{framesSpeed:D2} FPS", 575, 210, 10, Color.DarkGray);
|
||||
DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, Color.DarkGray);
|
||||
|
||||
for (var i = 0; i < MaxFrameSpeed; i++)
|
||||
{
|
||||
if (i < framesSpeed)
|
||||
{
|
||||
DrawRectangle(250 + 21 * i, 205, 20, 20, Color.Red);
|
||||
}
|
||||
DrawRectangleLines(250 + 21 * i, 205, 20, 20, Color.Maroon);
|
||||
}
|
||||
|
||||
DrawTextureRec(scarfy, frameRec, position, Color.White); // Draw part of the texture
|
||||
|
||||
DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, Color.Gray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadTexture(scarfy); // Texture unloading
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite animation");
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [texture] example - texture rectangle");
|
||||
|
||||
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
||||
Texture2D scarfy = LoadTexture("resources/scarfy.png");
|
||||
|
||||
Vector2 position = new(350.0f, 280.0f);
|
||||
Rectangle frameRec = new(0.0f, 0.0f, (float)scarfy.Width / 6, (float)scarfy.Height);
|
||||
int currentFrame = 0;
|
||||
|
||||
int framesCounter = 0;
|
||||
|
||||
// Number of spritesheet frames shown by second
|
||||
int framesSpeed = 8;
|
||||
|
||||
SetTargetFPS(60);
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new SpriteAnim();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
framesCounter++;
|
||||
|
||||
if (framesCounter >= (60 / framesSpeed))
|
||||
{
|
||||
framesCounter = 0;
|
||||
currentFrame++;
|
||||
|
||||
if (currentFrame > 5)
|
||||
{
|
||||
currentFrame = 0;
|
||||
}
|
||||
|
||||
frameRec.X = (float)currentFrame * (float)scarfy.Width / 6;
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.Right))
|
||||
{
|
||||
framesSpeed++;
|
||||
}
|
||||
else if (IsKeyPressed(KeyboardKey.Left))
|
||||
{
|
||||
framesSpeed--;
|
||||
}
|
||||
|
||||
framesSpeed = Math.Clamp(framesSpeed, MinFrameSpeed, MaxFrameSpeed);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
DrawTexture(scarfy, 15, 40, Color.White);
|
||||
DrawRectangleLines(15, 40, scarfy.Width, scarfy.Height, Color.Lime);
|
||||
DrawRectangleLines(
|
||||
15 + (int)frameRec.X,
|
||||
40 + (int)frameRec.Y,
|
||||
(int)frameRec.Width,
|
||||
(int)frameRec.Height,
|
||||
Color.Red
|
||||
);
|
||||
|
||||
DrawText("FRAME SPEED: ", 165, 210, 10, Color.DarkGray);
|
||||
DrawText($"{framesSpeed:2F} FPS", 575, 210, 10, Color.DarkGray);
|
||||
DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, Color.DarkGray);
|
||||
|
||||
for (int i = 0; i < MaxFrameSpeed; i++)
|
||||
{
|
||||
if (i < framesSpeed)
|
||||
{
|
||||
DrawRectangle(250 + 21 * i, 205, 20, 20, Color.Red);
|
||||
}
|
||||
DrawRectangleLines(250 + 21 * i, 205, 20, 20, Color.Maroon);
|
||||
}
|
||||
|
||||
// Draw part of the texture
|
||||
DrawTextureRec(scarfy, frameRec, position, Color.White);
|
||||
DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, Color.Gray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(scarfy);
|
||||
|
||||
CloseWindow();
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Reference in a new issue