Skip to content

Commit 34e11d3

Browse files
authored
Merge branch 'master' into examples/Fixed_shaders_game_of_life_For_Web
2 parents 4eff7ea + 2d8e346 commit 34e11d3

File tree

9 files changed

+100
-68
lines changed

9 files changed

+100
-68
lines changed

.github/workflows/update_examples.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ jobs:
2929
shell: bash
3030

3131
- name: Build and run rexm tool (requires GNU Makefile)
32-
# "rexm update" validates and updates all required examples in raylib and even raylib.com repos,
33-
# note that it calls examples/Makefile.Web internally, so it requires [make] tool available
32+
# "rexm validate" validates examples collection, looking for inconsistencies, it does not rebuild examples
33+
# "rexm update" validates and updates all examples with inconsistencies, pushing fixes to raylib and raylib.com repos
34+
# note that rexm calls examples/Makefile.Web internally, so it requires [make] tool available
3435
run: |
3536
sudo apt-get update && sudo apt-get install -y libopengl0 libglu1-mesa libx11-dev libxrandr-dev libxcursor-dev libxi-dev libxinerama-dev libgl1-mesa-dev libglu1-mesa-dev
3637
cd "${{ github.workspace }}/src"
@@ -47,7 +48,7 @@ jobs:
4748
export REXM_EXAMPLES_COLLECTION_FILE_PATH="${{ github.workspace }}/examples/examples_list.txt"
4849
export REXM_EXAMPLES_VS2022_SLN_FILE="${{ github.workspace }}/projects/VS2022/raylib.sln"
4950
export EMSDK_PATH="${{ github.workspace }}/emsdk-cache/emsdk-main"
50-
./rexm update
51+
./rexm validate
5152
shell: bash
5253

5354
- name: Commit changes to raylib repo (DISABLED)

examples/core/core_clipboard_text.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
*
33
* raylib [core] example - clipboard text
44
*
5+
* Example complexity rating: [★★☆☆] 2/4
6+
*
57
* Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
68
*
79
* Example contributed by Ananth S (@Ananth1839) and reviewed by Ramon Santamaria (@raysan5)

examples/examples_list.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ core;core_input_actions;★★☆☆;5.5;5.6;2025;2025;"Jett";@JettMonstersGoBoo
5151
core;core_directory_files;★☆☆☆;5.5;5.6;2025;2025;"Hugo ARNAL";@hugoarnal
5252
core;core_highdpi_testbed;★☆☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
5353
core;core_screen_recording;★★☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
54-
core;core_clipboard_text;★☆☆;5.6-dev;5.6-dev;2025;2025;"Ananth S";@Ananth1839
54+
core;core_clipboard_text;★☆☆;5.6-dev;5.6-dev;2025;2025;"Ananth S";@Ananth1839
5555
core;core_text_file_loading;★☆☆☆;5.5;5.6;0;0;"Aanjishnu Bhattacharyya";@NimComPoo-04
5656
core;core_compute_hash;★★☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
5757
shapes;shapes_basic_shapes;★☆☆☆;1.0;4.2;2014;2025;"Ramon Santamaria";@raysan5
@@ -91,6 +91,7 @@ shapes;shapes_math_angle_rotation;★☆☆☆;5.6-dev;5.6;2025;2025;"Kris";@kri
9191
shapes;shapes_rlgl_color_wheel;★★★☆;5.6-dev;5.6-dev;2025;2025;"Robin";@RobinsAviary
9292
shapes;shapes_rlgl_triangle;★★☆☆;5.6-dev;5.6-dev;2025;2025;"Robin";@RobinsAviary
9393
shapes;shapes_ball_physics;★★☆☆;5.6-dev;5.6-dev;2025;2025;"David Buzatto";@davidbuzatto
94+
shapes;shapes_penrose_tile;★★★★;5.5;5.6-dev;2025;2025;"David Buzatto";@davidbuzatto
9495
textures;textures_logo_raylib;★☆☆☆;1.0;1.0;2014;2025;"Ramon Santamaria";@raysan5
9596
textures;textures_srcrec_dstrec;★★★☆;1.3;1.3;2015;2025;"Ramon Santamaria";@raysan5
9697
textures;textures_image_drawing;★★☆☆;1.4;1.4;2016;2025;"Ramon Santamaria";@raysan5
@@ -119,6 +120,7 @@ textures;textures_image_rotate;★★☆☆;1.0;1.0;2014;2025;"Ramon Santamaria"
119120
textures;textures_screen_buffer;★★☆☆;5.5;5.5;2025;2025;"Agnis Aldiņš";@nezvers
120121
textures;textures_textured_curve;★★★☆;4.5;4.5;2022;2025;"Jeffery Myers";@JeffM2501
121122
textures;textures_sprite_stacking;★★☆☆;5.6-dev;6.0;2025;2025;"Robin";@RobinsAviary
123+
textures;textures_cellular_automata;★★☆☆;5.6;5.6;2025;2025;"Jordi Santonja";@JordSant
122124
text;text_sprite_fonts;★☆☆☆;1.7;3.7;2017;2025;"Ramon Santamaria";@raysan5
123125
text;text_font_spritefont;★☆☆☆;1.0;1.0;2014;2025;"Ramon Santamaria";@raysan5
124126
text;text_font_filters;★★☆☆;1.3;4.2;2015;2025;"Ramon Santamaria";@raysan5
@@ -134,6 +136,7 @@ text;text_3d_drawing;★★★★;3.5;4.0;2021;2025;"Vlad Adrian";@demizdor
134136
text;text_codepoints_loading;★★★☆;4.2;4.2;2022;2025;"Ramon Santamaria";@raysan5
135137
text;text_inline_styling;★★★☆;5.6-dev;5.6-dev;2025;2025;"Wagner Barongello";@SultansOfCode
136138
text;text_words_alignment;★☆☆☆;5.6-dev;5.6-dev;2025;2025;"JP Mortiboys";@themushroompirates
139+
text;text_strings_management;★★★☆;5.6-dev;5.6-dev;2025;2025;"David Buzatto";@davidbuzatto
137140
models;models_animation_playing;★★☆☆;2.5;3.5;2019;2025;"Culacant";@culacant
138141
models;models_billboard_rendering;★★★☆;1.3;3.5;2015;2025;"Ramon Santamaria";@raysan5
139142
models;models_box_collisions;★☆☆☆;1.3;3.5;2015;2025;"Ramon Santamaria";@raysan5
@@ -193,6 +196,7 @@ shaders;shaders_basic_pbr;★★★★;5.0;5.5;2023;2025;"Afan OLOVCIC";@_DevDad
193196
shaders;shaders_lightmap_rendering;★★★☆;4.5;4.5;2019;2025;"Jussi Viitala";@nullstare
194197
shaders;shaders_rounded_rectangle;★★★☆;5.5;5.5;2025;2025;"Anstro Pleuton";@anstropleuton
195198
shaders;shaders_depth_rendering;★★★☆;5.6-dev;5.6-dev;2025;2025;"Luís Almeida";@luis605
199+
shaders;shaders_game_of_life;★★★☆;5.6;5.6;2025;2025;"Jordi Santonja";@JordSant
196200
audio;audio_module_playing;★☆☆☆;1.5;3.5;2016;2025;"Ramon Santamaria";@raysan5
197201
audio;audio_music_stream;★☆☆☆;1.3;4.2;2015;2025;"Ramon Santamaria";@raysan5
198202
audio;audio_raw_stream;★★★☆;1.6;4.2;2015;2025;"Ramon Santamaria";@raysan5
@@ -208,7 +212,3 @@ others;easings_testbed;★★★☆;2.5;3.0;2019;2025;"Juan Miguel López";@flas
208212
others;raylib_opengl_interop;★★★★;3.8;4.0;2021;2025;"Stephan Soller";@arkanis
209213
others;embedded_files_loading;★★☆☆;3.0;3.5;2020;2025;"Kristian Holmgren";@defutura
210214
others;web_basic_window;★☆☆☆;5.6-dev;5.6-dev;2014;2025;"Ramon Santamaria";@raysan5
211-
shaders;shaders_game_of_life;★★★☆;5.6;5.6;2025;2025;"Jordi Santonja";@JordSant
212-
shapes;shapes_penrose_tile;★★★★;5.5;5.6-dev;2025;2025;"David Buzatto";@davidbuzatto
213-
text;text_strings_management;★★★☆;5.6-dev;5.6-dev;2025;2025;"David Buzatto";@davidbuzatto
214-
textures;textures_cellular_automata;★★☆☆;5.6;5.6;2025;2025;"Jordi Santonja";@JordSant

examples/shaders/shaders_game_of_life.c

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ int main(void)
5959
//--------------------------------------------------------------------------------------
6060
const int screenWidth = 800;
6161
const int screenHeight = 450;
62+
63+
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - game of life");
6264

6365
const int menuWidth = 100;
6466
const int windowWidth = screenWidth - menuWidth;
@@ -79,10 +81,9 @@ int main(void)
7981
{ "Puffer train", { 0.1f, 0.5f } }, { "Glider Gun", { 0.2f, 0.2f } }, { "Breeder", { 0.1f, 0.5f } },
8082
{ "Random", { 0.5f, 0.5f } }
8183
};
82-
const int numberOfPresets = sizeof(presetPatterns) / sizeof(presetPatterns[0]);
84+
85+
const int numberOfPresets = sizeof(presetPatterns)/sizeof(presetPatterns[0]);
8386

84-
// Variable declaration
85-
//--------------------------------------------------------------------------------------
8687
int zoom = 1;
8788
float offsetX = (worldWidth - windowWidth)/2.0f; // Centered on window
8889
float offsetY = (worldHeight - windowHeight)/2.0f; // Centered on window
@@ -96,8 +97,6 @@ int main(void)
9697
bool buttonFaster = false;
9798
bool buttonSlower = false;
9899

99-
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - game of life");
100-
101100
// Load shader
102101
Shader shdrGameOfLife = LoadShader(0, TextFormat("resources/shaders/glsl%i/game_of_life.fs", GLSL_VERSION));
103102

@@ -114,7 +113,7 @@ int main(void)
114113
EndTextureMode();
115114

116115
Image startPattern = LoadImage("resources/game_of_life/r_pentomino.png");
117-
UpdateTextureRec(world2.texture, (Rectangle) { worldWidth / 2.0f, worldHeight / 2.0f, (float)(startPattern.width), (float)(startPattern.height) }, startPattern.data);
116+
UpdateTextureRec(world2.texture, (Rectangle){ worldWidth/2.0f, worldHeight/2.0f, (float)(startPattern.width), (float)(startPattern.height) }, startPattern.data);
118117
UnloadImage(startPattern);
119118

120119
// Pointers to the two textures, to be swapped
@@ -142,10 +141,8 @@ int main(void)
142141

143142
const float centerX = offsetX + (windowWidth/2.0f)/zoom;
144143
const float centerY = offsetY + (windowHeight/2.0f)/zoom;
145-
if (buttonZoomIn || (mouseWheelMove > 0.0f))
146-
zoom *= 2;
147-
if ((buttonZomOut || (mouseWheelMove < 0.0f)) && (zoom > 1))
148-
zoom /= 2;
144+
if (buttonZoomIn || (mouseWheelMove > 0.0f)) zoom *= 2;
145+
if ((buttonZomOut || (mouseWheelMove < 0.0f)) && (zoom > 1)) zoom /= 2;
149146
offsetX = centerX - (windowWidth/2.0f)/zoom;
150147
offsetY = centerY - (windowHeight/2.0f)/zoom;
151148
}
@@ -155,7 +152,6 @@ int main(void)
155152
if (buttonSlower) framesPerStep++;
156153

157154
// Mouse management
158-
//----------------------------------------------------------------------------------
159155
if ((mode == MODE_RUN) || (mode == MODE_PAUSE))
160156
{
161157
FreeImageToDraw(&imageToDraw); // Free the image to draw: no longer needed in these modes
@@ -176,10 +172,8 @@ int main(void)
176172
const float offsetDecimalY = offsetY - floorf(offsetY);
177173
int sizeInWorldX = (int)(ceilf((float)(windowWidth + offsetDecimalX*zoom)/zoom));
178174
int sizeInWorldY = (int)(ceilf((float)(windowHeight + offsetDecimalY*zoom)/zoom));
179-
if (offsetX + sizeInWorldX >= worldWidth)
180-
sizeInWorldX = worldWidth - (int)floorf(offsetX);
181-
if (offsetY + sizeInWorldY >= worldHeight)
182-
sizeInWorldY = worldHeight - (int)floorf(offsetY);
175+
if (offsetX + sizeInWorldX >= worldWidth) sizeInWorldX = worldWidth - (int)floorf(offsetX);
176+
if (offsetY + sizeInWorldY >= worldHeight) sizeInWorldY = worldHeight - (int)floorf(offsetY);
183177

184178
// Create image to draw if not created yet
185179
if (imageToDraw == NULL)
@@ -191,6 +185,7 @@ int main(void)
191185
EndTextureMode();
192186
imageToDraw = (Image*)RL_MALLOC(sizeof(Image));
193187
*imageToDraw = LoadImageFromTexture(worldOnScreen.texture);
188+
194189
UnloadRenderTexture(worldOnScreen);
195190
}
196191

@@ -200,23 +195,19 @@ int main(void)
200195
{
201196
int mouseX = (int)(mousePosition.x + offsetDecimalX*zoom)/zoom;
202197
int mouseY = (int)(mousePosition.y + offsetDecimalY*zoom)/zoom;
203-
if (mouseX >= sizeInWorldX)
204-
mouseX = sizeInWorldX - 1;
205-
if (mouseY >= sizeInWorldY)
206-
mouseY = sizeInWorldY - 1;
207-
if (firstColor == -1)
208-
firstColor = (GetImageColor(*imageToDraw, mouseX, mouseY).r < 5)? 0 : 1;
198+
if (mouseX >= sizeInWorldX) mouseX = sizeInWorldX - 1;
199+
if (mouseY >= sizeInWorldY) mouseY = sizeInWorldY - 1;
200+
if (firstColor == -1) firstColor = (GetImageColor(*imageToDraw, mouseX, mouseY).r < 5)? 0 : 1;
209201
const int prevColor = (GetImageColor(*imageToDraw, mouseX, mouseY).r < 5)? 0 : 1;
202+
210203
ImageDrawPixel(imageToDraw, mouseX, mouseY, (firstColor) ? BLACK : RAYWHITE);
211-
if (prevColor != firstColor)
212-
UpdateTextureRec(currentWorld->texture, (Rectangle){ floorf(offsetX), floorf(offsetY), (float)(sizeInWorldX), (float)(sizeInWorldY) }, imageToDraw->data);
204+
205+
if (prevColor != firstColor) UpdateTextureRec(currentWorld->texture, (Rectangle){ floorf(offsetX), floorf(offsetY), (float)(sizeInWorldX), (float)(sizeInWorldY) }, imageToDraw->data);
213206
}
214-
else
215-
firstColor = -1;
207+
else firstColor = -1;
216208
}
217209

218210
// Load selected preset
219-
//----------------------------------------------------------------------------------
220211
if (preset >= 0)
221212
{
222213
Image pattern;
@@ -237,6 +228,7 @@ int main(void)
237228
BeginTextureMode(*currentWorld);
238229
ClearBackground(RAYWHITE);
239230
EndTextureMode();
231+
240232
UpdateTextureRec(currentWorld->texture, (Rectangle){ worldWidth*presetPatterns[preset].position.x - pattern.width/2.0f,
241233
worldHeight*presetPatterns[preset].position.y - pattern.height/2.0f,
242234
(float)(pattern.width), (float)(pattern.height) }, pattern.data);
@@ -250,9 +242,12 @@ int main(void)
250242
{
251243
ImageClearBackground(&pattern, RAYWHITE);
252244
for (int x = 0; x < pattern.width; x++)
245+
{
253246
for (int y = 0; y < pattern.height; y++)
254-
if (GetRandomValue(0, 100) < 15)
255-
ImageDrawPixel(&pattern, x, y, BLACK);
247+
{
248+
if (GetRandomValue(0, 100) < 15) ImageDrawPixel(&pattern, x, y, BLACK);
249+
}
250+
}
256251
UpdateTextureRec(currentWorld->texture,
257252
(Rectangle){ (float)(pattern.width*i), (float)(pattern.height*j),
258253
(float)(pattern.width), (float)(pattern.height) }, pattern.data);
@@ -261,26 +256,25 @@ int main(void)
261256
}
262257

263258
UnloadImage(pattern);
259+
264260
mode = MODE_PAUSE;
265-
offsetX = worldWidth * presetPatterns[preset].position.x - windowWidth/zoom/2.0f;
266-
offsetY = worldHeight * presetPatterns[preset].position.y - windowHeight/zoom/2.0f;
261+
offsetX = worldWidth*presetPatterns[preset].position.x - windowWidth/zoom/2.0f;
262+
offsetY = worldHeight*presetPatterns[preset].position.y - windowHeight/zoom/2.0f;
267263
}
268264

269265
// Check window draw inside world limits
270266
if (offsetX < 0) offsetX = 0;
271267
if (offsetY < 0) offsetY = 0;
272-
if (offsetX > worldWidth - (float)(windowWidth)/zoom)
273-
offsetX = worldWidth - (float)(windowWidth)/zoom;
274-
if (offsetY > worldHeight - (float)(windowHeight)/zoom)
275-
offsetY = worldHeight - (float)(windowHeight)/zoom;
268+
if (offsetX > worldWidth - (float)(windowWidth)/zoom) offsetX = worldWidth - (float)(windowWidth)/zoom;
269+
if (offsetY > worldHeight - (float)(windowHeight)/zoom) offsetY = worldHeight - (float)(windowHeight)/zoom;
276270

277271
// Rectangles for drawing texture portion to screen
278-
//----------------------------------------------------------------------------------
279272
const Rectangle textureSourceToScreen = { offsetX, offsetY, (float)windowWidth/zoom, (float)windowHeight/zoom };
273+
//----------------------------------------------------------------------------------
280274

281275
// Draw to texture
282276
//----------------------------------------------------------------------------------
283-
if ((mode == MODE_RUN) && ((frame % framesPerStep) == 0))
277+
if ((mode == MODE_RUN) && ((frame%framesPerStep) == 0))
284278
{
285279
// Swap worlds
286280
RenderTexture2D *tempWorld = currentWorld;
@@ -294,10 +288,12 @@ int main(void)
294288
EndShaderMode();
295289
EndTextureMode();
296290
}
291+
//----------------------------------------------------------------------------------
297292

298293
// Draw to screen
299294
//----------------------------------------------------------------------------------
300295
BeginDrawing();
296+
301297
DrawTexturePro(currentWorld->texture, textureSourceToScreen, textureOnScreen, (Vector2){ 0, 0 }, 0.0f, WHITE);
302298

303299
DrawLine(windowWidth, 0, windowWidth, screenHeight, (Color){ 218, 218, 218, 255 });
@@ -311,8 +307,7 @@ int main(void)
311307
DrawText("Presets", 710, 58, 8, GRAY);
312308
preset = -1;
313309
for (int i = 0; i < numberOfPresets; i++)
314-
if (GuiButton((Rectangle){ 710.0f, 70.0f + 18*i, 80.0f, 16.0f }, presetPatterns[i].name))
315-
preset = i;
310+
if (GuiButton((Rectangle){ 710.0f, 70.0f + 18*i, 80.0f, 16.0f }, presetPatterns[i].name)) preset = i;
316311

317312
GuiToggleGroup((Rectangle){ 710, 258, 80, 16 }, "Run\nPause\nDraw", &mode);
318313

@@ -324,8 +319,6 @@ int main(void)
324319
buttonFaster = GuiButton((Rectangle){ 710, 382, 80, 16 }, "Faster");
325320
buttonSlower = GuiButton((Rectangle){ 710, 400, 80, 16 }, "Slower");
326321

327-
//------------------------------------------------------------------------------
328-
329322
DrawFPS(712, 426);
330323

331324
EndDrawing();

src/rcore.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1742,8 +1742,33 @@ int GetRandomValue(int min, int max)
17421742
{
17431743
TRACELOG(LOG_WARNING, "Invalid GetRandomValue() arguments, range should not be higher than %i", RAND_MAX);
17441744
}
1745+
1746+
// NOTE: This one-line approach produces a non-uniform distribution,
1747+
// as stated by Donald Knuth in the book The Art of Programming, so
1748+
// using below approach for more uniform results
1749+
//value = (rand()%(abs(max - min) + 1) + min);
1750+
1751+
// More uniform range solution
1752+
int range = (max - min) + 1;
1753+
1754+
// Degenerate/overflow case: fall back to min (same behavior as "always min" instead of UB)
1755+
if (range <= 0) value = min;
1756+
else
1757+
{
1758+
// Rejection sampling to get a uniform integer in [min, max]
1759+
unsigned long c = (unsigned long)RAND_MAX + 1UL; // number of possible rand() results
1760+
unsigned long m = (unsigned long)range; // size of the target interval
1761+
unsigned long t = c - (c%m); // largest multiple of m <= c
1762+
unsigned long r = 0;
17451763

1746-
value = (rand()%(abs(max - min) + 1) + min);
1764+
for (;;)
1765+
{
1766+
r = (unsigned long)rand();
1767+
if (r < t) break; // Only accept values within the fair region
1768+
}
1769+
1770+
value = min + (int)(r%m);
1771+
}
17471772
#endif
17481773
return value;
17491774
}

src/rlgl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3384,10 +3384,12 @@ unsigned int rlLoadTexture(const void *data, int width, int height, int format,
33843384
#if defined(GRAPHICS_API_OPENGL_33)
33853385
if (mipmapCount > 1)
33863386
{
3387-
// Activate Trilinear filtering if mipmaps are available
3387+
// Activate trilinear filtering if mipmaps are available
33883388
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
33893389
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
3390-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mipmapCount); // Required for user-defined mip count
3390+
3391+
// Define thee maximum number of mipmap levels to be used, 0 is default texture size
3392+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mipmapCount - 1);
33913393
}
33923394
#endif
33933395

0 commit comments

Comments
 (0)