1/*******************************************************************************************
2*
3* raylib [textures] example - sprite explosion
4*
5* This example has been created using raylib 2.5 (www.raylib.com)
6* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
7*
8* Copyright (c) 2019 Anata and Ramon Santamaria (@raysan5)
9*
10********************************************************************************************/
11
12#include "raylib.h"
13
14#define NUM_FRAMES 8
15#define NUM_LINES 6
16
17int main(void)
18{
19 // Initialization
20 //--------------------------------------------------------------------------------------
21 const int screenWidth = 800;
22 const int screenHeight = 450;
23
24 InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite explosion");
25
26 InitAudioDevice();
27
28 // Load explosion sound
29 Sound fxBoom = LoadSound("resources/boom.wav");
30
31 // Load explosion texture
32 Texture2D explosion = LoadTexture("resources/explosion.png");
33
34 // Init variables for animation
35 int frameWidth = explosion.width/NUM_FRAMES; // Sprite one frame rectangle width
36 int frameHeight = explosion.height/NUM_LINES; // Sprite one frame rectangle height
37 int currentFrame = 0;
38 int currentLine = 0;
39
40 Rectangle frameRec = { 0, 0, frameWidth, frameHeight };
41 Vector2 position = { 0.0f, 0.0f };
42
43 bool active = false;
44 int framesCounter = 0;
45
46 SetTargetFPS(120);
47 //--------------------------------------------------------------------------------------
48
49 // Main game loop
50 while (!WindowShouldClose()) // Detect window close button or ESC key
51 {
52 // Update
53 //----------------------------------------------------------------------------------
54
55 // Check for mouse button pressed and activate explosion (if not active)
56 if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && !active)
57 {
58 position = GetMousePosition();
59 active = true;
60
61 position.x -= frameWidth/2;
62 position.y -= frameHeight/2;
63
64 PlaySound(fxBoom);
65 }
66
67 // Compute explosion animation frames
68 if (active)
69 {
70 framesCounter++;
71
72 if (framesCounter > 2)
73 {
74 currentFrame++;
75
76 if (currentFrame >= NUM_FRAMES)
77 {
78 currentFrame = 0;
79 currentLine++;
80
81 if (currentLine >= NUM_LINES)
82 {
83 currentLine = 0;
84 active = false;
85 }
86 }
87
88 framesCounter = 0;
89 }
90 }
91
92 frameRec.x = frameWidth*currentFrame;
93 frameRec.y = frameHeight*currentLine;
94 //----------------------------------------------------------------------------------
95
96 // Draw
97 //----------------------------------------------------------------------------------
98 BeginDrawing();
99
100 ClearBackground(RAYWHITE);
101
102 // Draw explosion required frame rectangle
103 if (active) DrawTextureRec(explosion, frameRec, position, WHITE);
104
105 EndDrawing();
106 //----------------------------------------------------------------------------------
107 }
108
109 // De-Initialization
110 //--------------------------------------------------------------------------------------
111 UnloadTexture(explosion); // Unload texture
112 UnloadSound(fxBoom); // Unload sound
113
114 CloseAudioDevice();
115
116 CloseWindow(); // Close window and OpenGL context
117 //--------------------------------------------------------------------------------------
118
119 return 0;
120}