← ziglang.inraylib-zigGitHub

sprite explosion

★★☆☆by Ramon Santamaria (@raysan5)

There is no browser build of this example yet.

Is it the same as C?

//! raylib [textures] example - sprite explosion
//!
//! Zig-API translation of examples/textures/textures_sprite_explosion.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2019-2025 Ramon Santamaria (@raysan5)

const rl = @import("raylib");

const num_frames_per_line = 5;
const num_lines = 5;

pub fn main() !void {
    // Initialization
    const screen_width = 800;
    const screen_height = 450;

    rl.initWindow(screen_width, screen_height, "raylib [textures] example - sprite explosion");
    defer rl.closeWindow(); // Close window and OpenGL context

    rl.initAudioDevice();
    defer rl.closeAudioDevice();

    // Load explosion sound
    const fx_boom = try rl.loadSound("resources/boom.wav");
    defer rl.unloadSound(fx_boom); // Unload sound

    // Load explosion texture
    const explosion = try rl.loadTexture("resources/explosion.png");
    defer rl.unloadTexture(explosion); // Unload texture

    // Init variables for animation
    const frame_width = @as(f32, @floatFromInt(explosion.width)) / num_frames_per_line; // Sprite one frame rectangle width
    const frame_height = @as(f32, @floatFromInt(explosion.height)) / num_lines; // Sprite one frame rectangle height
    var current_frame: u32 = 0;
    var current_line: u32 = 0;

    var frame_rec: rl.Rectangle = .{ .x = 0, .y = 0, .width = frame_width, .height = frame_height };
    var position: rl.Vector2 = .{ .x = 0.0, .y = 0.0 };

    var active = false;
    var frames_counter: u32 = 0;

    rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second

    // Main game loop
    while (!rl.windowShouldClose()) { // Detect window close button or ESC key
        // Update

        // Check for mouse button pressed and activate explosion (if not active)
        if (rl.isMouseButtonPressed(.left) and !active) {
            position = rl.getMousePosition();
            active = true;

            position.x -= frame_width / 2.0;
            position.y -= frame_height / 2.0;

            rl.playSound(fx_boom);
        }

        // Compute explosion animation frames
        if (active) {
            frames_counter += 1;

            if (frames_counter > 2) {
                current_frame += 1;

                if (current_frame >= num_frames_per_line) {
                    current_frame = 0;
                    current_line += 1;

                    if (current_line >= num_lines) {
                        current_line = 0;
                        active = false;
                    }
                }

                frames_counter = 0;
            }
        }

        frame_rec.x = frame_width * @as(f32, @floatFromInt(current_frame));
        frame_rec.y = frame_height * @as(f32, @floatFromInt(current_line));

        // Draw
        rl.beginDrawing();
        defer rl.endDrawing();

        rl.clearBackground(.ray_white);

        // Draw explosion required frame rectangle
        if (active) rl.drawTextureRec(explosion, frame_rec, position, .white);
    }
}
/*******************************************************************************************
*
*   raylib [textures] example - sprite explosion
*
*   Example complexity rating: [★★☆☆] 2/4
*
*   Example originally created with raylib 2.5, last time updated with raylib 3.5
*
*   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)
*
********************************************************************************************/

#include "raylib.h"

#define NUM_FRAMES_PER_LINE     5
#define NUM_LINES               5

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite explosion");

    InitAudioDevice();

    // Load explosion sound
    Sound fxBoom = LoadSound("resources/boom.wav");

    // Load explosion texture
    Texture2D explosion = LoadTexture("resources/explosion.png");

    // Init variables for animation
    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
    int currentFrame = 0;
    int currentLine = 0;

    Rectangle frameRec = { 0, 0, frameWidth, frameHeight };
    Vector2 position = { 0.0f, 0.0f };

    bool active = false;
    int framesCounter = 0;

    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //---------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------

        // Check for mouse button pressed and activate explosion (if not active)
        if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !active)
        {
            position = GetMousePosition();
            active = true;

            position.x -= frameWidth/2.0f;
            position.y -= frameHeight/2.0f;

            PlaySound(fxBoom);
        }

        // Compute explosion animation frames
        if (active)
        {
            framesCounter++;

            if (framesCounter > 2)
            {
                currentFrame++;

                if (currentFrame >= NUM_FRAMES_PER_LINE)
                {
                    currentFrame = 0;
                    currentLine++;

                    if (currentLine >= NUM_LINES)
                    {
                        currentLine = 0;
                        active = false;
                    }
                }

                framesCounter = 0;
            }
        }

        frameRec.x = frameWidth*currentFrame;
        frameRec.y = frameHeight*currentLine;
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            // Draw explosion required frame rectangle
            if (active) DrawTextureRec(explosion, frameRec, position, WHITE);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadTexture(explosion);   // Unload texture
    UnloadSound(fxBoom);        // Unload sound

    CloseAudioDevice();

    CloseWindow();              // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

From raylib 6.2. The original on GitHub.