Files
raylib/examples/textures/textures_sprite_explosion.c

127 lines
4.1 KiB
C
Raw Normal View History

2019-05-03 15:56:07 +02:00
/*******************************************************************************************
*
* raylib [textures] example - sprite explosion
*
* Example complexity rating: [] 2/4
*
2022-07-20 01:28:37 +02:00
* Example originally created with raylib 2.5, last time updated with raylib 3.5
2019-05-03 15:56:07 +02:00
*
2022-07-20 01:28:37 +02:00
* 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) 2019-2025 Ramon Santamaria (@raysan5)
2019-05-03 15:56:07 +02:00
*
********************************************************************************************/
#include "raylib.h"
#define NUM_FRAMES_PER_LINE 5
#define NUM_LINES 5
2019-05-03 15:56:07 +02:00
2022-06-21 19:53:18 +02:00
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
int main(void)
2019-05-03 15:56:07 +02:00
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite explosion");
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
InitAudioDevice();
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
// Load explosion sound
Sound fxBoom = LoadSound("resources/boom.wav");
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
// Load explosion texture
2019-05-17 01:17:40 +02:00
Texture2D explosion = LoadTexture("resources/explosion.png");
2021-04-22 18:55:24 +02:00
2019-05-03 15:56:07 +02:00
// Init variables for animation
2021-10-25 01:21:16 -07:00
float frameWidth = (float)(explosion.width/NUM_FRAMES_PER_LINE); // Sprite one frame rectangle width
float frameHeight = (float)(explosion.height/NUM_LINES); // Sprite one frame rectangle height
2019-05-03 15:56:07 +02:00
int currentFrame = 0;
int currentLine = 0;
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
Rectangle frameRec = { 0, 0, frameWidth, frameHeight };
2019-05-27 00:18:15 +02:00
Vector2 position = { 0.0f, 0.0f };
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
bool active = false;
int framesCounter = 0;
2019-05-20 16:36:42 +02:00
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//---------------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
// Check for mouse button pressed and activate explosion (if not active)
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !active)
2019-05-03 15:56:07 +02:00
{
position = GetMousePosition();
active = true;
2019-05-20 16:36:42 +02:00
position.x -= frameWidth/2.0f;
position.y -= frameHeight/2.0f;
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
PlaySound(fxBoom);
}
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
// Compute explosion animation frames
if (active)
{
framesCounter++;
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
if (framesCounter > 2)
{
currentFrame++;
2019-05-20 16:36:42 +02:00
if (currentFrame >= NUM_FRAMES_PER_LINE)
2019-05-03 15:56:07 +02:00
{
currentFrame = 0;
currentLine++;
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
if (currentLine >= NUM_LINES)
{
currentLine = 0;
active = false;
}
}
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
framesCounter = 0;
}
}
2019-05-20 16:36:42 +02:00
frameRec.x = frameWidth*currentFrame;
frameRec.y = frameHeight*currentLine;
2019-05-03 15:56:07 +02:00
//----------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
ClearBackground(RAYWHITE);
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
// Draw explosion required frame rectangle
if (active) DrawTextureRec(explosion, frameRec, position, WHITE);
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(explosion); // Unload texture
2019-05-20 16:36:42 +02:00
UnloadSound(fxBoom); // Unload sound
2019-05-03 15:56:07 +02:00
CloseAudioDevice();
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
2019-05-03 15:56:07 +02:00
return 0;
}