[examples] Added: textures_screen_buffer (#5357)

* Example textures_screen_buffer

* remove resource preload for web makefile

* update description

* code formatting
This commit is contained in:
Agnis Aldiņš "NeZvērs
2025-11-16 20:40:49 +02:00
committed by GitHub
parent 5c2747e3a8
commit 596d3bcb7e
8 changed files with 766 additions and 2 deletions

View File

@@ -619,6 +619,7 @@ TEXTURES = \
textures/textures_sprite_button \
textures/textures_sprite_explosion \
textures/textures_srcrec_dstrec \
textures/textures_screen_buffer \
textures/textures_textured_curve \
textures/textures_tiled_drawing \
textures/textures_to_image

View File

@@ -607,6 +607,7 @@ TEXTURES = \
textures/textures_sprite_button \
textures/textures_sprite_explosion \
textures/textures_srcrec_dstrec \
textures/textures_screen_buffer \
textures/textures_textured_curve \
textures/textures_tiled_drawing \
textures/textures_to_image
@@ -1058,6 +1059,9 @@ textures/textures_srcrec_dstrec: textures/textures_srcrec_dstrec.c
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
--preload-file textures/resources/scarfy.png@resources/scarfy.png
textures/textures_screen_buffer: textures/textures_screen_buffer.c
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
textures/textures_textured_curve: textures/textures_textured_curve.c
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
--preload-file textures/resources/road.png@resources/road.png

View File

@@ -17,7 +17,7 @@ You may find it easier to use than other toolchains, especially when it comes to
- `zig build [module]` to compile all examples for a module (e.g. `zig build core`)
- `zig build [example]` to compile _and run_ a particular example (e.g. `zig build core_basic_window`)
## EXAMPLES COLLECTION [TOTAL: 194]
## EXAMPLES COLLECTION [TOTAL: 195]
### category: core [47]
@@ -114,7 +114,7 @@ Examples using raylib shapes drawing functionality, provided by raylib [shapes](
| [shapes_lines_drawing](shapes/shapes_lines_drawing.c) | <img src="shapes/shapes_lines_drawing.png" alt="shapes_lines_drawing" width="80"> | ⭐☆☆☆ | 5.6-dev | 5.6 | [Robin](https://github.com/RobinsAviary) |
| [shapes_math_angle_rotation](shapes/shapes_math_angle_rotation.c) | <img src="shapes/shapes_math_angle_rotation.png" alt="shapes_math_angle_rotation" width="80"> | ⭐☆☆☆ | 5.6-dev | 5.6 | [Kris](https://github.com/krispy-snacc) |
### category: textures [26]
### category: textures [27]
Examples using raylib textures functionality, including image/textures loading/generation and drawing, provided by raylib [textures](../src/rtextures.c) module.
@@ -145,6 +145,7 @@ Examples using raylib textures functionality, including image/textures loading/g
| [textures_image_kernel](textures/textures_image_kernel.c) | <img src="textures/textures_image_kernel.png" alt="textures_image_kernel" width="80"> | ⭐⭐⭐⭐️ | 1.3 | 1.3 | [Karim Salem](https://github.com/kimo-s) |
| [textures_image_channel](textures/textures_image_channel.c) | <img src="textures/textures_image_channel.png" alt="textures_image_channel" width="80"> | ⭐⭐☆☆ | 5.5 | 5.5 | [Bruno Cabral](https://github.com/brccabral) |
| [textures_image_rotate](textures/textures_image_rotate.c) | <img src="textures/textures_image_rotate.png" alt="textures_image_rotate" width="80"> | ⭐⭐☆☆ | 1.0 | 1.0 | [Ramon Santamaria](https://github.com/raysan5) |
| [textures_screen_buffer](textures/textures_screen_buffer.c) | <img src="textures/textures_screen_buffer.png" alt="textures_screen_buffer" width="80"> | ⭐⭐☆☆ | 5.5 | 5.5 | [Agnis Aldins](https://github.com/nezvers) |
| [textures_textured_curve](textures/textures_textured_curve.c) | <img src="textures/textures_textured_curve.png" alt="textures_textured_curve" width="80"> | ⭐⭐⭐☆ | 4.5 | 4.5 | [Jeffery Myers](https://github.com/JeffM2501) |
### category: text [15]

View File

@@ -113,6 +113,7 @@ textures;textures_gif_player;★★★☆;4.2;4.2;2021;2025;"Ramon Santamaria";@
textures;textures_image_kernel;★★★★;1.3;1.3;2015;2025;"Karim Salem";@kimo-s
textures;textures_image_channel;★★☆☆;5.5;5.5;2024;2025;"Bruno Cabral";@brccabral
textures;textures_image_rotate;★★☆☆;1.0;1.0;2014;2025;"Ramon Santamaria";@raysan5
textures;textures_screen_buffer;★★☆☆;5.5;5.5;2014;2025;"Agnis Aldins";@nezvers
textures;textures_textured_curve;★★★☆;4.5;4.5;2022;2025;"Jeffery Myers";@JeffM2501
text;text_sprite_fonts;★☆☆☆;1.7;3.7;2017;2025;"Ramon Santamaria";@raysan5
text;text_font_spritefont;★☆☆☆;1.0;1.0;2014;2025;"Ramon Santamaria";@raysan5

View File

@@ -0,0 +1,161 @@
/*******************************************************************************************
*
* raylib [textures] example - screen buffer / update Image as screen buffer and display with texture
*
* Example complexity rating: [★★☆☆] 2/4
*
* Example originally created with raylib 5.5, last time updated with raylib 5.6
*
* 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"
#define MAX_COLORS 256
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 450
#define SCALE_FACTOR 2
// buffer size at least for screenImage pixel count
#define INDEX_BUFFER_SIZE ((SCREEN_WIDTH * SCREEN_HEIGHT) / SCALE_FACTOR)
#define FLAME_WIDTH (SCREEN_WIDTH / SCALE_FACTOR)
static void GeneretePalette(Color *palette);
static void ClearIndexBuffer(unsigned char *buffer, int count);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = SCREEN_WIDTH;
const int screenHeight = SCREEN_HEIGHT;
const int pixelScale = SCALE_FACTOR;
const int imageWidth = screenWidth / pixelScale;
const int imageHeight = screenHeight / pixelScale;
InitWindow(screenWidth, screenHeight, "raylib [<module>] example - <name>");
Color palette[MAX_COLORS] = {0};
unsigned char indexBuffer[INDEX_BUFFER_SIZE] = {0};
unsigned char flameRootBuffer[FLAME_WIDTH] = {0};
Image screenImage = GenImageColor(imageWidth, imageHeight, BLACK);
Texture screenTexture = LoadTextureFromImage(screenImage);
GeneretePalette(palette);
ClearIndexBuffer(indexBuffer, INDEX_BUFFER_SIZE);
ClearIndexBuffer(flameRootBuffer, FLAME_WIDTH);
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Grow flameRoot
for (int x = 2; x < FLAME_WIDTH; ++x)
{
unsigned short flame = flameRootBuffer[x];
if (flame == 255) continue;
flame += GetRandomValue(0, 2);
if (flame > 255) flame = 255;
flameRootBuffer[x] = flame;
}
// transfer flameRoot to indexBuffer
for (int x = 0; x < FLAME_WIDTH; ++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) continue;
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 i = x + y * imageWidth;
unsigned char colorIndex = indexBuffer[i];
if (colorIndex == 0) continue;
// Move pixel a row above
indexBuffer[i] = 0;
int moveX = GetRandomValue(0, 2) - 1;
int newX = x + moveX;
if (newX < 0 || newX >= imageWidth) continue;
unsigned i_above = i - imageWidth + moveX;
int decay = GetRandomValue(0, 3);
colorIndex -= (decay < colorIndex) ? decay : colorIndex;
indexBuffer[i_above] = colorIndex;
}
}
// Update screenImage with palette colors
for (int y = 1; y < imageHeight; ++y)
{
for (int x = 0; x < imageWidth; ++x)
{
unsigned i = x + y * imageWidth;
unsigned char colorIndex = indexBuffer[i];
Color col = palette[colorIndex];
ImageDrawPixel(&screenImage, x, y, col);
}
}
UpdateTexture(screenTexture, screenImage.data);
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
const Vector2 origin = (Vector2){0, 0};
const float rotation = 0.f;
DrawTextureEx(screenTexture, origin, rotation, pixelScale, WHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(screenTexture);
UnloadImage(screenImage);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
static void GeneretePalette(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.f + 150.f * hue, saturation, value);
}
}
static void ClearIndexBuffer(unsigned char *buffer, int count)
{
// Use memset to set to ZERO, but for demonstration a plain for loop is used
for (int i = 0; i < count; ++i)
{
buffer[i] = 0;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB