Skip to content

Commit 596d3bc

Browse files
authored
[examples] Added: textures_screen_buffer (#5357)
* Example textures_screen_buffer * remove resource preload for web makefile * update description * code formatting
1 parent 5c2747e commit 596d3bc

File tree

8 files changed

+766
-2
lines changed

8 files changed

+766
-2
lines changed

examples/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ TEXTURES = \
619619
textures/textures_sprite_button \
620620
textures/textures_sprite_explosion \
621621
textures/textures_srcrec_dstrec \
622+
textures/textures_screen_buffer \
622623
textures/textures_textured_curve \
623624
textures/textures_tiled_drawing \
624625
textures/textures_to_image

examples/Makefile.Web

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ TEXTURES = \
607607
textures/textures_sprite_button \
608608
textures/textures_sprite_explosion \
609609
textures/textures_srcrec_dstrec \
610+
textures/textures_screen_buffer \
610611
textures/textures_textured_curve \
611612
textures/textures_tiled_drawing \
612613
textures/textures_to_image
@@ -1058,6 +1059,9 @@ textures/textures_srcrec_dstrec: textures/textures_srcrec_dstrec.c
10581059
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
10591060
--preload-file textures/resources/scarfy.png@resources/scarfy.png
10601061

1062+
textures/textures_screen_buffer: textures/textures_screen_buffer.c
1063+
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
1064+
10611065
textures/textures_textured_curve: textures/textures_textured_curve.c
10621066
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
10631067
--preload-file textures/resources/road.png@resources/road.png

examples/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You may find it easier to use than other toolchains, especially when it comes to
1717
- `zig build [module]` to compile all examples for a module (e.g. `zig build core`)
1818
- `zig build [example]` to compile _and run_ a particular example (e.g. `zig build core_basic_window`)
1919

20-
## EXAMPLES COLLECTION [TOTAL: 194]
20+
## EXAMPLES COLLECTION [TOTAL: 195]
2121

2222
### category: core [47]
2323

@@ -114,7 +114,7 @@ Examples using raylib shapes drawing functionality, provided by raylib [shapes](
114114
| [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) |
115115
| [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) |
116116

117-
### category: textures [26]
117+
### category: textures [27]
118118

119119
Examples using raylib textures functionality, including image/textures loading/generation and drawing, provided by raylib [textures](../src/rtextures.c) module.
120120

@@ -145,6 +145,7 @@ Examples using raylib textures functionality, including image/textures loading/g
145145
| [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) |
146146
| [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) |
147147
| [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) |
148+
| [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) |
148149
| [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) |
149150

150151
### category: text [15]

examples/examples_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ textures;textures_gif_player;★★★☆;4.2;4.2;2021;2025;"Ramon Santamaria";@
113113
textures;textures_image_kernel;★★★★;1.3;1.3;2015;2025;"Karim Salem";@kimo-s
114114
textures;textures_image_channel;★★☆☆;5.5;5.5;2024;2025;"Bruno Cabral";@brccabral
115115
textures;textures_image_rotate;★★☆☆;1.0;1.0;2014;2025;"Ramon Santamaria";@raysan5
116+
textures;textures_screen_buffer;★★☆☆;5.5;5.5;2014;2025;"Agnis Aldins";@nezvers
116117
textures;textures_textured_curve;★★★☆;4.5;4.5;2022;2025;"Jeffery Myers";@JeffM2501
117118
text;text_sprite_fonts;★☆☆☆;1.7;3.7;2017;2025;"Ramon Santamaria";@raysan5
118119
text;text_font_spritefont;★☆☆☆;1.0;1.0;2014;2025;"Ramon Santamaria";@raysan5
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [textures] example - screen buffer / update Image as screen buffer and display with texture
4+
*
5+
* Example complexity rating: [★★☆☆] 2/4
6+
*
7+
* Example originally created with raylib 5.5, last time updated with raylib 5.6
8+
*
9+
* Example contributed by Agnis Aldiņš (@nezvers) and reviewed by Ramon Santamaria (@raysan5)
10+
*
11+
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12+
* BSD-like license that allows static linking with closed source software
13+
*
14+
* Copyright (c) 2025 Agnis Aldiņš (@nezvers)
15+
*
16+
********************************************************************************************/
17+
18+
#include "raylib.h"
19+
20+
#define MAX_COLORS 256
21+
#define SCREEN_WIDTH 800
22+
#define SCREEN_HEIGHT 450
23+
#define SCALE_FACTOR 2
24+
// buffer size at least for screenImage pixel count
25+
#define INDEX_BUFFER_SIZE ((SCREEN_WIDTH * SCREEN_HEIGHT) / SCALE_FACTOR)
26+
#define FLAME_WIDTH (SCREEN_WIDTH / SCALE_FACTOR)
27+
28+
static void GeneretePalette(Color *palette);
29+
static void ClearIndexBuffer(unsigned char *buffer, int count);
30+
31+
//------------------------------------------------------------------------------------
32+
// Program main entry point
33+
//------------------------------------------------------------------------------------
34+
int main(void)
35+
{
36+
// Initialization
37+
//--------------------------------------------------------------------------------------
38+
const int screenWidth = SCREEN_WIDTH;
39+
const int screenHeight = SCREEN_HEIGHT;
40+
const int pixelScale = SCALE_FACTOR;
41+
const int imageWidth = screenWidth / pixelScale;
42+
const int imageHeight = screenHeight / pixelScale;
43+
InitWindow(screenWidth, screenHeight, "raylib [<module>] example - <name>");
44+
45+
Color palette[MAX_COLORS] = {0};
46+
unsigned char indexBuffer[INDEX_BUFFER_SIZE] = {0};
47+
unsigned char flameRootBuffer[FLAME_WIDTH] = {0};
48+
49+
Image screenImage = GenImageColor(imageWidth, imageHeight, BLACK);
50+
Texture screenTexture = LoadTextureFromImage(screenImage);
51+
GeneretePalette(palette);
52+
ClearIndexBuffer(indexBuffer, INDEX_BUFFER_SIZE);
53+
ClearIndexBuffer(flameRootBuffer, FLAME_WIDTH);
54+
55+
SetTargetFPS(60);
56+
//--------------------------------------------------------------------------------------
57+
58+
// Main game loop
59+
while (!WindowShouldClose()) // Detect window close button or ESC key
60+
{
61+
// Grow flameRoot
62+
for (int x = 2; x < FLAME_WIDTH; ++x)
63+
{
64+
unsigned short flame = flameRootBuffer[x];
65+
if (flame == 255) continue;
66+
flame += GetRandomValue(0, 2);
67+
if (flame > 255) flame = 255;
68+
flameRootBuffer[x] = flame;
69+
}
70+
71+
// transfer flameRoot to indexBuffer
72+
for (int x = 0; x < FLAME_WIDTH; ++x)
73+
{
74+
int i = x + (imageHeight - 1) * imageWidth;
75+
indexBuffer[i] = flameRootBuffer[x];
76+
}
77+
78+
// Clear top row, because it can't move any higher
79+
for (int x = 0; x < imageWidth; ++x)
80+
{
81+
if (indexBuffer[x] == 0) continue;
82+
indexBuffer[x] = 0;
83+
}
84+
85+
// Skip top row, it is already cleared
86+
for (int y = 1; y < imageHeight; ++y)
87+
{
88+
for (int x = 0; x < imageWidth; ++x)
89+
{
90+
unsigned i = x + y * imageWidth;
91+
unsigned char colorIndex = indexBuffer[i];
92+
if (colorIndex == 0) continue;
93+
94+
// Move pixel a row above
95+
indexBuffer[i] = 0;
96+
int moveX = GetRandomValue(0, 2) - 1;
97+
int newX = x + moveX;
98+
if (newX < 0 || newX >= imageWidth) continue;
99+
100+
unsigned i_above = i - imageWidth + moveX;
101+
int decay = GetRandomValue(0, 3);
102+
colorIndex -= (decay < colorIndex) ? decay : colorIndex;
103+
indexBuffer[i_above] = colorIndex;
104+
}
105+
}
106+
107+
// Update screenImage with palette colors
108+
for (int y = 1; y < imageHeight; ++y)
109+
{
110+
for (int x = 0; x < imageWidth; ++x)
111+
{
112+
unsigned i = x + y * imageWidth;
113+
unsigned char colorIndex = indexBuffer[i];
114+
Color col = palette[colorIndex];
115+
ImageDrawPixel(&screenImage, x, y, col);
116+
}
117+
}
118+
119+
UpdateTexture(screenTexture, screenImage.data);
120+
// Draw
121+
//----------------------------------------------------------------------------------
122+
BeginDrawing();
123+
const Vector2 origin = (Vector2){0, 0};
124+
const float rotation = 0.f;
125+
DrawTextureEx(screenTexture, origin, rotation, pixelScale, WHITE);
126+
EndDrawing();
127+
//----------------------------------------------------------------------------------
128+
}
129+
130+
// De-Initialization
131+
//--------------------------------------------------------------------------------------
132+
133+
UnloadTexture(screenTexture);
134+
UnloadImage(screenImage);
135+
136+
CloseWindow(); // Close window and OpenGL context
137+
//--------------------------------------------------------------------------------------
138+
139+
return 0;
140+
}
141+
142+
static void GeneretePalette(Color *palette)
143+
{
144+
for (int i = 0; i < MAX_COLORS; ++i)
145+
{
146+
float t = (float)i/(float)(MAX_COLORS - 1);
147+
float hue = t * t;
148+
float saturation = t;
149+
float value = t;
150+
palette[i] = ColorFromHSV(250.f + 150.f * hue, saturation, value);
151+
}
152+
}
153+
154+
static void ClearIndexBuffer(unsigned char *buffer, int count)
155+
{
156+
// Use memset to set to ZERO, but for demonstration a plain for loop is used
157+
for (int i = 0; i < count; ++i)
158+
{
159+
buffer[i] = 0;
160+
}
161+
}
30.8 KB
Loading

0 commit comments

Comments
 (0)