screen buffer
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:not checked yet The browser comparison does not cover this example yet.
//! raylib [textures] example - screen buffer
//!
//! Zig-API translation of examples/textures/textures_screen_buffer.c (raylib 6.2).
//! Example contributed by Agnis Aldiņš (@nezvers) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2025 Agnis Aldiņš (@nezvers)
const rl = @import("raylib");
const max_colors = 256;
const scale_factor = 2;
const screen_width = 800;
const screen_height = 450;
const image_width = screen_width / scale_factor;
const image_height = screen_height / scale_factor;
const flame_width = screen_width / scale_factor;
pub fn main() !void {
// Initialization
rl.initWindow(screen_width, screen_height, "raylib [textures] example - screen buffer");
defer rl.closeWindow(); // Close window and OpenGL context
// Palette index of every pixel, and the flame intensity along the bottom row
var index_buffer: [image_height][image_width]u8 = @splat(@splat(0));
var flame_root_buffer: [flame_width]u8 = @splat(0);
var screen_image = rl.genImageColor(image_width, image_height, .black);
defer rl.unloadImage(screen_image);
const screen_texture = try rl.loadTextureFromImage(screen_image);
defer rl.unloadTexture(screen_texture);
// Generate flame color palette
var palette: [max_colors]rl.Color = undefined;
for (&palette, 0..) |*color, i| {
const t = @as(f32, @floatFromInt(i)) / @as(f32, max_colors - 1);
const hue = t * t;
const saturation = t;
const value = t;
color.* = rl.colorFromHSV(250.0 + 150.0 * hue, saturation, value);
}
rl.setTargetFPS(60);
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
// Grow flameRoot
for (flame_root_buffer[2..]) |*flame| {
const grown = @as(i32, flame.*) + rl.getRandomValue(0, 2);
flame.* = @intCast(@min(grown, 255));
}
// Transfer flameRoot to indexBuffer
index_buffer[image_height - 1] = flame_root_buffer;
// Clear top row, because it can't move any higher
index_buffer[0] = @splat(0);
// Skip top row, it is already cleared
for (1..image_height) |y| {
for (0..image_width) |x| {
var color_index = index_buffer[y][x];
if (color_index != 0) {
// Move pixel a row above
index_buffer[y][x] = 0;
const move_x = rl.getRandomValue(0, 2) - 1;
const new_x = @as(i32, @intCast(x)) + move_x;
if ((new_x > 0) and (new_x < image_width)) {
const decay = rl.getRandomValue(0, 3);
color_index -|= @intCast(decay);
index_buffer[y - 1][@intCast(new_x)] = color_index;
}
}
}
}
// Update screenImage with palette colors
for (index_buffer[1..], 1..) |row, y| {
for (row, 0..) |color_index, x| {
rl.imageDrawPixel(&screen_image, @intCast(x), @intCast(y), palette[color_index]);
}
}
rl.updateTexture(screen_texture, screen_image.data.?);
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
rl.drawTextureEx(screen_texture, .{ .x = 0, .y = 0 }, 0.0, 2.0, .white);
}
}/*******************************************************************************************
*
* raylib [textures] example - screen buffer
*
* Example complexity rating: [★★☆☆] 2/4
*
* Example originally created with raylib 5.5, last time updated with raylib 5.5
*
* Example contributed by Agnis Aldiņš (@nezvers) 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 Agnis Aldiņš (@nezvers)
*
********************************************************************************************/
#include "raylib.h"
#include <stdlib.h> // Required for: calloc(), free()
#define MAX_COLORS 256
#define SCALE_FACTOR 2
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - screen buffer");
int imageWidth = screenWidth/SCALE_FACTOR;
int imageHeight = screenHeight/SCALE_FACTOR;
int flameWidth = screenWidth/SCALE_FACTOR;
Color palette[MAX_COLORS] = { 0 };
unsigned char *indexBuffer = RL_CALLOC(imageWidth*imageWidth, sizeof(unsigned char));
unsigned char *flameRootBuffer = RL_CALLOC(flameWidth, sizeof(unsigned char));
Image screenImage = GenImageColor(imageWidth, imageHeight, BLACK);
Texture screenTexture = LoadTextureFromImage(screenImage);
// Generate flame color palette
for (int i = 0; i < MAX_COLORS; i++)
{
float t = (float)i/(float)(MAX_COLORS - 1);
float hue = t*t;
float saturation = t;
float value = t;
palette[i] = ColorFromHSV(250.0f + 150.0f*hue, saturation, value);
}
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Grow flameRoot
for (int x = 2; x < flameWidth; x++)
{
int flame = (int)flameRootBuffer[x];
flame += GetRandomValue(0, 2);
flameRootBuffer[x] = (flame > 255)? 255: (unsigned char)flame;
}
// Transfer flameRoot to indexBuffer
for (int x = 0; x < flameWidth; x++)
{
int i = x + (imageHeight - 1)*imageWidth;
indexBuffer[i] = flameRootBuffer[x];
}
// Clear top row, because it can't move any higher
for (int x = 0; x < imageWidth; x++)
{
if (indexBuffer[x] != 0) indexBuffer[x] = 0;
}
// Skip top row, it is already cleared
for (int y = 1; y < imageHeight; y++)
{
for (int x = 0; x < imageWidth; x++)
{
unsigned int i = x + y*imageWidth;
unsigned char colorIndex = indexBuffer[i];
if (colorIndex != 0)
{
// Move pixel a row above
indexBuffer[i] = 0;
int moveX = GetRandomValue(0, 2) - 1;
int newX = x + moveX;
if ((newX > 0) && (newX < imageWidth))
{
unsigned int iabove = i - imageWidth + moveX;
int decay = GetRandomValue(0, 3);
colorIndex -= (decay < colorIndex)? decay : colorIndex;
indexBuffer[iabove] = colorIndex;
}
}
}
}
// Update screenImage with palette colors
for (int y = 1; y < imageHeight; y++)
{
for (int x = 0; x < imageWidth; x++)
{
unsigned int i = x + y*imageWidth;
unsigned char colorIndex = indexBuffer[i];
Color col = palette[colorIndex];
ImageDrawPixel(&screenImage, x, y, col);
}
}
UpdateTexture(screenTexture, screenImage.data);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTextureEx(screenTexture, (Vector2){ 0, 0 }, 0.0f, 2.0f, WHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
RL_FREE(indexBuffer);
RL_FREE(flameRootBuffer);
UnloadTexture(screenTexture);
UnloadImage(screenImage);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.