starfield effect
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 - starfield effect
//!
//! Zig-API translation of examples/shapes/shapes_starfield_effect.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2025 JP Mortiboys (@themushroompirates)
const rl = @import("raylib");
const rm = rl.math;
const star_count = 420;
const screen_width = 800;
const screen_height = 450;
/// A star with a random position, at the start of its life (z = 1)
fn randomStar() rl.Vector3 {
const x: f32 = @floatFromInt(rl.getRandomValue(-screen_width / 2, screen_width / 2));
const y: f32 = @floatFromInt(rl.getRandomValue(-screen_height / 2, screen_height / 2));
return .{ .x = x, .y = y, .z = 1.0 };
}
pub fn main() void {
// Initialization
rl.initWindow(screen_width, screen_height, "raylib [shapes] example - starfield effect");
defer rl.closeWindow(); // Close window and OpenGL context
const bg_color = rl.colorLerp(.dark_blue, .black, 0.69);
// Speed at which we fly forward
var speed: f32 = 10.0 / 9.0;
// We're either drawing lines or circles
var draw_lines = true;
// Setup the stars with a random position
var stars: [star_count]rl.Vector3 = undefined;
for (&stars) |*star| star.* = randomStar();
var stars_screen_pos: [star_count]rl.Vector2 = @splat(.{ .x = 0, .y = 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
// Change speed based on mouse
const mouse_move = rl.getMouseWheelMove();
if (@as(i32, @intFromFloat(mouse_move)) != 0) speed += 2.0 * mouse_move / 9.0;
if (speed < 0.0) {
speed = 0.1;
} else if (speed > 2.0) speed = 2.0;
// Toggle lines / points with space bar
if (rl.isKeyPressed(.space)) draw_lines = !draw_lines;
const dt = rl.getFrameTime();
for (&stars, &stars_screen_pos) |*star, *screen_pos| {
// Update star's timer
star.z -= dt * speed;
// Calculate the screen position
screen_pos.* = .{
.x = screen_width * 0.5 + star.x / star.z,
.y = screen_height * 0.5 + star.y / star.z,
};
// If the star is too old, or offscreen, it dies and we make a new random one
if (star.z < 0.0 or screen_pos.x < 0 or screen_pos.y < 0.0 or
screen_pos.x > screen_width or screen_pos.y > screen_height)
{
star.* = randomStar();
}
}
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(bg_color);
for (stars, stars_screen_pos) |star, screen_pos| {
if (draw_lines) {
// Get the time a little while ago for this star, but clamp it
const t = rm.clamp(star.z + 1.0 / 32.0, 0.0, 1.0);
// If it's different enough from the current time, we proceed
// (compared in double precision, as the C `1e-3` literal is a double)
if (@as(f64, t - star.z) > 1e-3) {
// Calculate the screen position of the old point
const start_pos: rl.Vector2 = .{
.x = screen_width * 0.5 + star.x / t,
.y = screen_height * 0.5 + star.y / t,
};
// Draw a line connecting the old point to the current point
rl.drawLineV(start_pos, screen_pos, .ray_white);
}
} else {
// Make the radius grow as the star ages
const radius = rm.lerp(star.z, 1.0, 5.0);
// Draw the circle
rl.drawCircleV(screen_pos, radius, .ray_white);
}
}
rl.drawText(rl.textFormat("[MOUSE WHEEL] Current Speed: {d:.0}", .{9.0 * speed / 2.0}), 10, 40, 20, .ray_white);
rl.drawText(rl.textFormat("[SPACE] Current draw mode: {s}", .{if (draw_lines) "Lines" else "Circles"}), 10, 70, 20, .ray_white);
rl.drawFPS(10, 10);
}
}/*******************************************************************************************
*
* raylib [shapes] example - starfield effect
*
* Example complexity rating: [★★☆☆] 2/4
*
* Example originally created with raylib 5.5, last time updated with raylib 6.0
*
* Example contributed by JP Mortiboys (@themushroompirates) and reviewed by Ramon Santamaria (@raysan5)
*
* 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 JP Mortiboys (@themushroompirates)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h" // Required for: Lerp()
#define STAR_COUNT 420
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - starfield effect");
Color bgColor = ColorLerp(DARKBLUE, BLACK, 0.69f);
// Speed at which we fly forward
float speed = 10.0f/9.0f;
// We're either drawing lines or circles
bool drawLines = true;
Vector3 stars[STAR_COUNT] = { 0 };
Vector2 starsScreenPos[STAR_COUNT] = { 0 };
// Setup the stars with a random position
for (int i = 0; i < STAR_COUNT; i++)
{
stars[i].x = (float)GetRandomValue(-screenWidth / 2, (int)screenWidth / 2);
stars[i].y = (float)GetRandomValue(-screenHeight / 2, (int)screenHeight / 2);
stars[i].z = 1.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
//----------------------------------------------------------------------------------
// Change speed based on mouse
float mouseMove = GetMouseWheelMove();
if ((int)mouseMove != 0) speed += 2.0f*mouseMove/9.0f;
if (speed < 0.0f) speed = 0.1f;
else if (speed > 2.0f) speed = 2.0f;
// Toggle lines / points with space bar
if (IsKeyPressed(KEY_SPACE)) drawLines = !drawLines;
float dt = GetFrameTime();
for (int i = 0; i < STAR_COUNT; i++)
{
// Update star's timer
stars[i].z -= dt*speed;
// Calculate the screen position
starsScreenPos[i] = (Vector2){
screenWidth*0.5f + stars[i].x/stars[i].z,
screenHeight*0.5f + stars[i].y/stars[i].z,
};
// If the star is too old, or offscreen, it dies and we make a new random one
if ((stars[i].z < 0.0f) || (starsScreenPos[i].x < 0) || (starsScreenPos[i].y < 0.0f) ||
(starsScreenPos[i].x > screenWidth) || (starsScreenPos[i].y > screenHeight))
{
stars[i].x = (float)GetRandomValue(-screenWidth / 2, screenWidth / 2);
stars[i].y = (float)GetRandomValue(-screenHeight / 2, screenHeight / 2);
stars[i].z = 1.0f;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(bgColor);
for (int i = 0; i < STAR_COUNT; i++)
{
if (drawLines)
{
// Get the time a little while ago for this star, but clamp it
float t = Clamp(stars[i].z + 1.0f/32.0f, 0.0f, 1.0f);
// If it's different enough from the current time, we proceed
if ((t - stars[i].z) > 1e-3)
{
// Calculate the screen position of the old point
Vector2 startPos = (Vector2){
screenWidth*0.5f + stars[i].x/t,
screenHeight*0.5f + stars[i].y/t,
};
// Draw a line connecting the old point to the current point
DrawLineV(startPos, starsScreenPos[i], RAYWHITE);
}
}
else
{
// Make the radius grow as the star ages
float radius = Lerp(stars[i].z, 1.0f, 5.0f);
// Draw the circle
DrawCircleV(starsScreenPos[i], radius, RAYWHITE);
}
}
DrawText(TextFormat("[MOUSE WHEEL] Current Speed: %.0f", 9.0f*speed/2.0f), 10, 40, 20, RAYWHITE);
DrawText(TextFormat("[SPACE] Current draw mode: %s", drawLines ? "Lines" : "Circles"), 10, 70, 20, RAYWHITE);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.