summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorDalton Overmyer <[email protected]>2024-02-29 11:26:49 -0600
committerGitHub <[email protected]>2024-02-29 18:26:49 +0100
commit077ab6d56bea4fc464cd5c07e02071e911eac64a (patch)
treeef1e8770b35b60e08055e7b87e64a12551106bb2 /examples
parented9a6d862b9e31acaa59c6ffffd1f8aa398d54d8 (diff)
downloadraylib-077ab6d56bea4fc464cd5c07e02071e911eac64a.tar.gz
raylib-077ab6d56bea4fc464cd5c07e02071e911eac64a.zip
Add an example that generates a random sequence. (#3846)
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile1
-rw-r--r--examples/Makefile.Web4
-rw-r--r--examples/README.md1
-rw-r--r--examples/core/core_random_sequence.c176
-rw-r--r--examples/core/core_random_sequence.pngbin0 -> 34025 bytes
5 files changed, 182 insertions, 0 deletions
diff --git a/examples/Makefile b/examples/Makefile
index 8362b8c8..9ce05da5 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -453,6 +453,7 @@ CORE = \
core/core_input_mouse_wheel \
core/core_input_multitouch \
core/core_loading_thread \
+ core/core_random_sequence \
core/core_random_values \
core/core_scissor_test \
core/core_smooth_pixelperfect \
diff --git a/examples/Makefile.Web b/examples/Makefile.Web
index 204db50c..af3325aa 100644
--- a/examples/Makefile.Web
+++ b/examples/Makefile.Web
@@ -359,6 +359,7 @@ CORE = \
core/core_input_mouse_wheel \
core/core_input_multitouch \
core/core_loading_thread \
+ core/core_random_sequence \
core/core_random_values \
core/core_scissor_test \
core/core_smooth_pixelperfect \
@@ -588,6 +589,9 @@ core/core_input_multitouch: core/core_input_multitouch.c
core/core_loading_thread: core/core_loading_thread.c
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) -sUSE_PTHREADS=1
+core/core_random_sequence: core/core_random_sequence.c
+ $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
+
core/core_random_values: core/core_random_values.c
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
diff --git a/examples/README.md b/examples/README.md
index 0fed8acd..94f5369d 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -55,6 +55,7 @@ Examples using raylib core platform functionality like window creation, inputs,
| 29 | [core_custom_frame_control](core/core_custom_frame_control.c) | <img src="core/core_custom_frame_control.png" alt="core_custom_frame_control" width="80"> | ⭐️⭐️⭐️⭐️ | **4.0** | **4.0** | [Ray](https://github.com/raysan5) |
| 30 | [core_smooth_pixelperfect](core/core_smooth_pixelperfect.c) | <img src="core/core_smooth_pixelperfect.png" alt="core_smooth_pixelperfect" width="80"> | ⭐️⭐️⭐️☆ | 3.7 | **4.0** | [Giancamillo Alessandroni](https://github.com/NotManyIdeasDev) |
| 31 | [core_window_should_close](core/core_window_should_close.c) | <img src="core/core_window_should_close.png" alt="core_window_should_close" width="80"> | ⭐️⭐️☆☆ | **4.2** | **4.2** | [Ray](https://github.com/raysan5) |
+| 32 | [core_random_sequence](core/core_random_sequence.c) | <img src="core/core_random_sequence.png" alt="core_random_sequence" width="80"> | ⭐️☆☆☆ | **5.0** | **5.0** | [REDl3east](https://github.com/REDl3east) |
### category: shapes
diff --git a/examples/core/core_random_sequence.c b/examples/core/core_random_sequence.c
new file mode 100644
index 00000000..c946b64d
--- /dev/null
+++ b/examples/core/core_random_sequence.c
@@ -0,0 +1,176 @@
+/*******************************************************************************************
+*
+* raylib [core] example - Generates a random sequence
+*
+* Example originally created with raylib 5.0, last time updated with raylib 5.0
+*
+* Example contributed by Dalton Overmyer (@REDl3east) 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) 2023 Dalton Overmyer (@REDl3east)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+#include "raymath.h"
+
+#include <stdlib.h> // Required for: malloc() and free()
+
+typedef struct ColorRect{
+ Color c;
+ Rectangle r;
+} ColorRect;
+
+static Color GenerateRandomColor();
+static ColorRect* GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight);
+static void ShuffleColorRectSequence(ColorRect* rectangles, int rectCount);
+static void DrawTextCenterKeyHelp(const char* key, const char* text, int posX, int posY, int fontSize, Color color);
+
+//------------------------------------------------------------------------------------
+// Program main entry point
+//------------------------------------------------------------------------------------
+int main(void) {
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ const int screenWidth = 800;
+ const int screenHeight = 450;
+
+ InitWindow(screenWidth, screenHeight, "raylib [core] example - Generates a random sequence");
+
+ int rectCount = 20;
+ float rectSize = (float)screenWidth/rectCount;
+ ColorRect* rectangles = GenerateRandomColorRectSequence(rectCount, rectSize, screenWidth, 0.75f * screenHeight);
+
+ SetTargetFPS(60);
+ //--------------------------------------------------------------------------------------
+
+ // Main game loop
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+
+ if(IsKeyPressed(KEY_SPACE))
+ {
+ ShuffleColorRectSequence(rectangles, rectCount);
+ }
+
+ if(IsKeyPressed(KEY_UP))
+ {
+ rectCount++;
+ rectSize = (float)screenWidth/rectCount;
+ free(rectangles);
+ rectangles = GenerateRandomColorRectSequence(rectCount, rectSize, screenWidth, 0.75f * screenHeight);
+ }
+
+ if(IsKeyPressed(KEY_DOWN))
+ {
+ if(rectCount >= 4){
+ rectCount--;
+ rectSize = (float)screenWidth/rectCount;
+ free(rectangles);
+ rectangles = GenerateRandomColorRectSequence(rectCount, rectSize, screenWidth, 0.75f * screenHeight);
+ }
+ }
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ int fontSize = 20;
+ for(int x=0;x<rectCount;x++)
+ {
+ DrawRectangleRec(rectangles[x].r, rectangles[x].c);
+ DrawTextCenterKeyHelp("SPACE", "to shuffle the sequence.", 10, screenHeight - 96, fontSize, BLACK);
+ DrawTextCenterKeyHelp("UP", "to add a rectangle and generate a new sequence.", 10, screenHeight - 64, fontSize, BLACK);
+ DrawTextCenterKeyHelp("DOWN", "to remove a rectangle and generate a new sequence.", 10, screenHeight - 32, fontSize, BLACK);
+ }
+
+ const char* rectCountText = TextFormat("%d rectangles", rectCount);
+ int rectCountTextSize = MeasureText(rectCountText, fontSize);
+ DrawText(rectCountText, screenWidth - rectCountTextSize - 10, 10, fontSize, BLACK);
+
+ DrawFPS(10, 10);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+
+ free(rectangles);
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
+
+static Color GenerateRandomColor()
+{
+ return CLITERAL(Color){
+ GetRandomValue(0, 255),
+ GetRandomValue(0, 255),
+ GetRandomValue(0, 255),
+ 255,
+ };
+}
+
+static ColorRect* GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight){
+ int *seq = LoadRandomSequence(rectCount, 0, rectCount-1);
+ ColorRect* rectangles = (ColorRect *)malloc(rectCount*sizeof(ColorRect));
+
+ float rectSeqWidth = rectCount * rectWidth;
+ int startX = (screenWidth - rectSeqWidth) * 0.5f;
+
+ for(int x=0;x<rectCount;x++){
+ int rectHeight = Remap(seq[x], 0, rectCount-1, 0, screenHeight);
+ rectangles[x].c = GenerateRandomColor();
+ rectangles[x].r = CLITERAL(Rectangle){
+ startX + x * rectWidth, screenHeight - rectHeight, rectWidth, rectHeight
+ };
+ }
+ UnloadRandomSequence(seq);
+ return rectangles;
+}
+
+static void ShuffleColorRectSequence(ColorRect* rectangles, int rectCount)
+{
+ int *seq = LoadRandomSequence(rectCount, 0, rectCount-1);
+ for(int i1=0;i1<rectCount;i1++){
+ ColorRect* r1 = &rectangles[i1];
+ ColorRect* r2 = &rectangles[seq[i1]];
+
+ // swap only the color and height
+ ColorRect tmp = *r1;
+ r1->c = r2->c;
+ r1->r.height = r2->r.height;
+ r1->r.y = r2->r.y;
+ r2->c = tmp.c;
+ r2->r.height = tmp.r.height;
+ r2->r.y = tmp.r.y;
+
+ }
+ UnloadRandomSequence(seq);
+}
+
+static void DrawTextCenterKeyHelp(const char* key, const char* text, int posX, int posY, int fontSize, Color color)
+{
+ int spaceSize = MeasureText(" ", fontSize);
+ int pressSize = MeasureText("Press", fontSize);
+ int keySize = MeasureText(key, fontSize);
+ int textSize = MeasureText(text, fontSize);
+ int totalSize = pressSize + 2 * spaceSize + keySize + 2 * spaceSize + textSize;
+ int textSizeCurrent = 0;
+
+ DrawText("Press", posX, posY, fontSize, color);
+ textSizeCurrent += pressSize + 2 * spaceSize;
+ DrawText(key, posX + textSizeCurrent, posY, fontSize, RED);
+ DrawRectangle(posX + textSizeCurrent, posY + fontSize, keySize, 3, RED);
+ textSizeCurrent += keySize + 2 * spaceSize;
+ DrawText(text, posX + textSizeCurrent, posY, fontSize, color);
+} \ No newline at end of file
diff --git a/examples/core/core_random_sequence.png b/examples/core/core_random_sequence.png
new file mode 100644
index 00000000..206aa8b4
--- /dev/null
+++ b/examples/core/core_random_sequence.png
Binary files differ