raylib/examples/textures/textures_sprite_explosion.c

125 lines
4.0 KiB
C
Raw Normal View History

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