simple particles
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:verified In headless Chrome, the Zig version gives a final frame byte-identical to the C example built for the web.
//! raylib [shapes] example - simple particles
//!
//! Zig-API translation of examples/shapes/shapes_simple_particles.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2025 Jordi Santonja (@JordSant)
const std = @import("std");
const rl = @import("raylib");
/// libc's `rand()`: the C example draws from it (not raylib's GetRandomValue),
/// so the same unseeded sequence is used here.
extern "c" fn rand() c_int;
const max_particles = 3000; // Max number of particles
const ParticleType = enum {
water,
smoke,
fire,
fn name(particle_type: ParticleType) [:0]const u8 {
return switch (particle_type) {
.water => "WATER",
.smoke => "SMOKE",
.fire => "FIRE",
};
}
};
const Particle = struct {
type: ParticleType, // Particle type (WATER, SMOKE, FIRE)
position: rl.Vector2, // Particle position on screen
velocity: rl.Vector2, // Particle current speed and direction
radius: f32, // Particle radius
color: rl.Color, // Particle color
life_time: f32, // Particle life time
alive: bool, // Particle alive: inside screen and life time
/// Update particle life and position; out-of-screen particles die
fn update(particle: *Particle, screen_width: f32, screen_height: f32) void {
particle.life_time += 1.0 / 60.0; // 60 FPS -> 1/60 seconds per frame
switch (particle.type) {
.water => {
particle.position.x += particle.velocity.x;
particle.velocity.y += 0.2; // Gravity
particle.position.y += particle.velocity.y;
},
.smoke => {
particle.position.x += particle.velocity.x;
particle.velocity.y -= 0.05; // Upwards
particle.position.y += particle.velocity.y;
particle.radius += 0.5; // Increment radius: smoke expands
particle.color.a -%= 4; // Decrement alpha: smoke fades
// If alpha transparent, particle dies
if (particle.color.a < 4) particle.alive = false;
},
.fire => {
// Add a little horizontal oscillation to fire particles
particle.position.x += particle.velocity.x + @cos(particle.life_time * 215.0);
particle.velocity.y -= 0.05; // Upwards
particle.position.y += particle.velocity.y;
particle.radius -= 0.15; // Decrement radius: fire shrinks
particle.color.g -%= 3; // Decrement green: fire turns reddish starting from yellow
// If radius too small, particle dies
if (particle.radius <= 0.02) particle.alive = false;
},
}
// Disable particle when out of screen
const center = particle.position;
const radius = particle.radius;
if (center.x < -radius or center.x > screen_width + radius or
center.y < -radius or center.y > screen_height + radius)
{
particle.alive = false;
}
}
};
/// Fixed-capacity ring of particles: `head` is the next write, `tail` the next read
const CircularBuffer = struct {
head: usize = 0,
tail: usize = 0,
buffer: []Particle,
/// Next slot to fill, or null when the buffer is full
fn add(cb: *CircularBuffer) ?*Particle {
const next = (cb.head + 1) % max_particles;
if (next == cb.tail) return null;
const particle = &cb.buffer[cb.head];
cb.head = next;
return particle;
}
fn emit(cb: *CircularBuffer, emitter_position: rl.Vector2, particle_type: ParticleType) void {
// If buffer is full, there is no new particle
const particle = cb.add() orelse return;
var speed = @as(f32, @floatFromInt(@rem(rand(), 10))) / 5.0;
const radius: f32, const color: rl.Color = switch (particle_type) {
.water => .{ 5.0, .blue },
.smoke => .{ 7.0, .gray },
.fire => blk: {
speed /= 10.0;
break :blk .{ 10.0, .yellow };
},
};
const direction: f32 = @floatFromInt(@rem(rand(), 360));
particle.* = .{
.type = particle_type,
.position = emitter_position,
.velocity = .{ .x = speed * @cos(direction * rl.deg2rad), .y = speed * @sin(direction * rl.deg2rad) },
.radius = radius,
.color = color,
.life_time = 0.0,
.alive = true,
};
}
fn update(cb: *CircularBuffer, screen_width: f32, screen_height: f32) void {
var i = cb.tail;
while (i != cb.head) : (i = (i + 1) % max_particles) cb.buffer[i].update(screen_width, screen_height);
// Remove dead particles: advance tail over them
while (cb.tail != cb.head and !cb.buffer[cb.tail].alive) cb.tail = (cb.tail + 1) % max_particles;
}
fn draw(cb: *const CircularBuffer) void {
var i = cb.tail;
while (i != cb.head) : (i = (i + 1) % max_particles) {
const particle = cb.buffer[i];
if (particle.alive) rl.drawCircleV(particle.position, particle.radius, particle.color);
}
}
};
pub fn main() !void {
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.initWindow(screen_width, screen_height, "raylib [shapes] example - simple particles");
defer rl.closeWindow(); // Close window and OpenGL context
// Definition of particles
const particles = try std.heap.page_allocator.alloc(Particle, max_particles); // Particle array
defer std.heap.page_allocator.free(particles);
var circular_buffer: CircularBuffer = .{ .buffer = particles };
// Particle emitter parameters
var emission_rate: i32 = -2; // Negative: on average every -X frames. Positive: particles per frame
var current_type: ParticleType = .water;
var emitter_position: rl.Vector2 = .{ .x = screen_width / 2.0, .y = screen_height / 2.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
// Emit new particles: when emission_rate is 1, emit every frame
if (emission_rate < 0) {
if (@rem(rand(), -emission_rate) == 0) circular_buffer.emit(emitter_position, current_type);
} else {
for (0..@intCast(emission_rate + 1)) |_| circular_buffer.emit(emitter_position, current_type);
}
// Update the parameters of each particle, then remove dead ones
circular_buffer.update(screen_width, screen_height);
// Change Particle Emission Rate (UP/DOWN arrows)
if (rl.isKeyPressed(.up)) emission_rate += 1;
if (rl.isKeyPressed(.down)) emission_rate -= 1;
// Change Particle Type (LEFT/RIGHT arrows)
if (rl.isKeyPressed(.right)) current_type = switch (current_type) {
.water => .smoke,
.smoke => .fire,
.fire => .water,
};
if (rl.isKeyPressed(.left)) current_type = switch (current_type) {
.water => .fire,
.smoke => .water,
.fire => .smoke,
};
if (rl.isMouseButtonDown(.left)) emitter_position = rl.getMousePosition();
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
// Call the function with a loop to draw all particles
circular_buffer.draw();
// Draw UI and Instructions
rl.drawRectangle(5, 5, 315, 75, rl.fade(.sky_blue, 0.5));
rl.drawRectangleLines(5, 5, 315, 75, .blue);
rl.drawText("CONTROLS:", 15, 15, 10, .black);
rl.drawText("UP/DOWN: Change Particle Emission Rate", 15, 35, 10, .black);
rl.drawText("LEFT/RIGHT: Change Particle Type (Water, Smoke, Fire)", 15, 55, 10, .black);
if (emission_rate < 0) {
rl.drawText(rl.textFormat("Particles every {d} frames | Type: {s}", .{ -emission_rate, current_type.name() }), 15, 95, 10, .dark_gray);
} else {
rl.drawText(rl.textFormat("{d} Particles per frame | Type: {s}", .{ emission_rate + 1, current_type.name() }), 15, 95, 10, .dark_gray);
}
rl.drawFPS(screen_width - 80, 10);
}
}/*******************************************************************************************
*
* raylib [shapes] example - simple particles
*
* Example complexity rating: [★★☆☆] 2/4
*
* Example originally created with raylib 5.6, last time updated with raylib 5.6
*
* Example contributed by Jordi Santonja (@JordSant)
*
* 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) 2025 Jordi Santonja (@JordSant)
*
********************************************************************************************/
#include "raylib.h"
#include <stdlib.h> // Required for: calloc(), free()
#include <math.h> // Required for: cosf(), sinf()
#define MAX_PARTICLES 3000 // Max number of particles
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef enum ParticleType {
WATER = 0,
SMOKE,
FIRE
} ParticleType;
static const char particleTypeNames[3][10] = { "WATER", "SMOKE", "FIRE" };
typedef struct Particle {
ParticleType type; // Particle type (WATER, SMOKE, FIRE)
Vector2 position; // Particle position on screen
Vector2 velocity; // Particle current speed and direction
float radius; // Particle radius
Color color; // Particle color
float lifeTime; // Particle life time
bool alive; // Particle alive: inside screen and life time
} Particle;
typedef struct CircularBuffer {
int head; // Index for the next write
int tail; // Index for the next read
Particle *buffer; // Particle buffer array
} CircularBuffer;
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
static void EmitParticle(CircularBuffer *circularBuffer, Vector2 emitterPosition, ParticleType type);
static Particle *AddToCircularBuffer(CircularBuffer *circularBuffer);
static void UpdateParticles(CircularBuffer *circularBuffer, int screenWidth, int screenHeight);
static void UpdateCircularBuffer(CircularBuffer *circularBuffer);
static void DrawParticles(CircularBuffer *circularBuffer);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - simple particles");
// Definition of particles
Particle *particles = (Particle*)RL_CALLOC(MAX_PARTICLES, sizeof(Particle)); // Particle array
CircularBuffer circularBuffer = { 0, 0, particles };
// Particle emitter parameters
int emissionRate = -2; // Negative: on average every -X frames. Positive: particles per frame
ParticleType currentType = WATER;
Vector2 emitterPosition = { screenWidth/2.0f, screenHeight/2.0f };
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
//----------------------------------------------------------------------------------
// Emit new particles: when emissionRate is 1, emit every frame
if (emissionRate < 0)
{
if (rand()%(-emissionRate) == 0) EmitParticle(&circularBuffer, emitterPosition, currentType);
}
else
{
for (int i = 0; i <= emissionRate; i++) EmitParticle(&circularBuffer, emitterPosition, currentType);
}
// Update the parameters of each particle
UpdateParticles(&circularBuffer, screenWidth, screenHeight);
// Remove dead particles from the circular buffer
UpdateCircularBuffer(&circularBuffer);
// Change Particle Emission Rate (UP/DOWN arrows)
if (IsKeyPressed(KEY_UP)) emissionRate++;
if (IsKeyPressed(KEY_DOWN)) emissionRate--;
// Change Particle Type (LEFT/RIGHT arrows)
if (IsKeyPressed(KEY_RIGHT)) (currentType == FIRE)? (currentType = WATER) : currentType++;
if (IsKeyPressed(KEY_LEFT)) (currentType == WATER)? (currentType = FIRE) : currentType--;
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) emitterPosition = GetMousePosition();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Call the function with a loop to draw all particles
DrawParticles(&circularBuffer);
// Draw UI and Instructions
DrawRectangle(5, 5, 315, 75, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(5, 5, 315, 75, BLUE);
DrawText("CONTROLS:", 15, 15, 10, BLACK);
DrawText("UP/DOWN: Change Particle Emission Rate", 15, 35, 10, BLACK);
DrawText("LEFT/RIGHT: Change Particle Type (Water, Smoke, Fire)", 15, 55, 10, BLACK);
if (emissionRate < 0) DrawText(TextFormat("Particles every %d frames | Type: %s", -emissionRate, particleTypeNames[currentType]), 15, 95, 10, DARKGRAY);
else DrawText(TextFormat("%d Particles per frame | Type: %s", emissionRate + 1, particleTypeNames[currentType]), 15, 95, 10, DARKGRAY);
DrawFPS(screenWidth - 80, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
RL_FREE(particles); // Free particles array data
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
static void EmitParticle(CircularBuffer *circularBuffer, Vector2 emitterPosition, ParticleType type)
{
Particle *newParticle = AddToCircularBuffer(circularBuffer);
// If buffer is full, newParticle is NULL
if (newParticle != NULL)
{
// Fill particle properties
newParticle->position = emitterPosition;
newParticle->alive = true;
newParticle->lifeTime = 0.0f;
newParticle->type = type;
float speed = (float)(rand()%10)/5.0f;
switch (type)
{
case WATER:
{
newParticle->radius = 5.0f;
newParticle->color = BLUE;
} break;
case SMOKE:
{
newParticle->radius = 7.0f;
newParticle->color = GRAY;
} break;
case FIRE:
{
newParticle->radius = 10.0f;
newParticle->color = YELLOW;
speed /= 10.0f;
} break;
default: break;
}
float direction = (float)(rand()%360);
newParticle->velocity = (Vector2){ speed*cosf(direction*DEG2RAD), speed*sinf(direction*DEG2RAD) };
}
}
static Particle *AddToCircularBuffer(CircularBuffer *circularBuffer)
{
Particle *particle = NULL;
// Check if buffer full
if (((circularBuffer->head + 1)%MAX_PARTICLES) != circularBuffer->tail)
{
// Add new particle to the head position and advance head
particle = &circularBuffer->buffer[circularBuffer->head];
circularBuffer->head = (circularBuffer->head + 1)%MAX_PARTICLES;
}
return particle;
}
static void UpdateParticles(CircularBuffer *circularBuffer, int screenWidth, int screenHeight)
{
for (int i = circularBuffer->tail; i != circularBuffer->head; i = (i + 1)%MAX_PARTICLES)
{
// Update particle life and positions
circularBuffer->buffer[i].lifeTime += 1.0f/60.0f; // 60 FPS -> 1/60 seconds per frame
switch (circularBuffer->buffer[i].type)
{
case WATER:
{
circularBuffer->buffer[i].position.x += circularBuffer->buffer[i].velocity.x;
circularBuffer->buffer[i].velocity.y += 0.2f; // Gravity
circularBuffer->buffer[i].position.y += circularBuffer->buffer[i].velocity.y;
} break;
case SMOKE:
{
circularBuffer->buffer[i].position.x += circularBuffer->buffer[i].velocity.x;
circularBuffer->buffer[i].velocity.y -= 0.05f; // Upwards
circularBuffer->buffer[i].position.y += circularBuffer->buffer[i].velocity.y;
circularBuffer->buffer[i].radius += 0.5f; // Increment radius: smoke expands
circularBuffer->buffer[i].color.a -= 4; // Decrement alpha: smoke fades
// If alpha transparent, particle dies
if (circularBuffer->buffer[i].color.a < 4) circularBuffer->buffer[i].alive = false;
} break;
case FIRE:
{
// Add a little horizontal oscillation to fire particles
circularBuffer->buffer[i].position.x += circularBuffer->buffer[i].velocity.x + cosf(circularBuffer->buffer[i].lifeTime*215.0f);
circularBuffer->buffer[i].velocity.y -= 0.05f; // Upwards
circularBuffer->buffer[i].position.y += circularBuffer->buffer[i].velocity.y;
circularBuffer->buffer[i].radius -= 0.15f; // Decrement radius: fire shrinks
circularBuffer->buffer[i].color.g -= 3; // Decrement green: fire turns reddish starting from yellow
// If radius too small, particle dies
if (circularBuffer->buffer[i].radius <= 0.02f) circularBuffer->buffer[i].alive = false;
} break;
default: break;
}
// Disable particle when out of screen
Vector2 center = circularBuffer->buffer[i].position;
float radius = circularBuffer->buffer[i].radius;
if ((center.x < -radius) || (center.x > (screenWidth + radius)) ||
(center.y < -radius) || (center.y > (screenHeight + radius)))
{
circularBuffer->buffer[i].alive = false;
}
}
}
static void UpdateCircularBuffer(CircularBuffer *circularBuffer)
{
// Update circular buffer: advance tail over dead particles
while ((circularBuffer->tail != circularBuffer->head) && !circularBuffer->buffer[circularBuffer->tail].alive)
{
circularBuffer->tail = (circularBuffer->tail + 1)%MAX_PARTICLES;
}
}
static void DrawParticles(CircularBuffer *circularBuffer)
{
for (int i = circularBuffer->tail; i != circularBuffer->head; i = (i + 1)%MAX_PARTICLES)
{
if (circularBuffer->buffer[i].alive)
{
DrawCircleV(circularBuffer->buffer[i].position,
circularBuffer->buffer[i].radius,
circularBuffer->buffer[i].color);
}
}
}From raylib 6.2. The original on GitHub.