Update core_random_sequence.c

This commit is contained in:
Ray
2025-09-02 12:18:19 +02:00
parent 8f32c502a7
commit 8b3c68f8b5

View File

@@ -24,8 +24,8 @@
// Types and Structures Definition // Types and Structures Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
typedef struct ColorRect { typedef struct ColorRect {
Color c; Color color;
Rectangle r; Rectangle rect;
} ColorRect; } ColorRect;
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
@@ -34,7 +34,6 @@ typedef struct ColorRect {
static Color GenerateRandomColor(); static Color GenerateRandomColor();
static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight); static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight);
static void ShuffleColorRectSequence(ColorRect *rectangles, int rectCount); 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 // Program main entry point
@@ -66,7 +65,9 @@ int main(void)
{ {
rectCount++; rectCount++;
rectSize = (float)screenWidth/rectCount; rectSize = (float)screenWidth/rectCount;
free(rectangles); RL_FREE(rectangles);
// Re-generate random sequence with new count
rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight); rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight);
} }
@@ -76,7 +77,9 @@ int main(void)
{ {
rectCount--; rectCount--;
rectSize = (float)screenWidth/rectCount; rectSize = (float)screenWidth/rectCount;
free(rectangles); RL_FREE(rectangles);
// Re-generate random sequence with new count
rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight); rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight);
} }
} }
@@ -88,20 +91,20 @@ int main(void)
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
int fontSize = 20;
for (int i = 0; i < rectCount; i++) for (int i = 0; i < rectCount; i++)
{ {
DrawRectangleRec(rectangles[i].r, rectangles[i].c); DrawRectangleRec(rectangles[i].rect, rectangles[i].color);
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); DrawText("Press SPACE to shuffle the sequence", 10, screenHeight - 96, 20, BLACK);
DrawTextCenterKeyHelp("DOWN", "to remove a rectangle and generate a new sequence.", 10, screenHeight - 32, fontSize, BLACK);
DrawText("Press SPACE to shuffle the current sequence", 10, screenHeight - 96, 20, BLACK);
DrawText("Press UP to add a rectangle and generate a new sequence", 10, screenHeight - 64, 20, BLACK);
DrawText("Press DOWN to remove a rectangle and generate a new sequence", 10, screenHeight - 32, 20, BLACK);
} }
const char *rectCountText = TextFormat("%d rectangles", rectCount); DrawText(TextFormat("Count: %d rectangles", rectCount), 10, 10, 20, MAROON);
int rectCountTextSize = MeasureText(rectCountText, fontSize);
DrawText(rectCountText, screenWidth - rectCountTextSize - 10, 10, fontSize, BLACK);
DrawFPS(10, 10); DrawFPS(screenWidth - 80, 10);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@@ -133,7 +136,8 @@ static Color GenerateRandomColor()
static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight) static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight)
{ {
ColorRect *rectangles = (ColorRect *)malloc((int)rectCount*sizeof(ColorRect)); ColorRect *rectangles = (ColorRect *)RL_CALLOC((int)rectCount, sizeof(ColorRect));
int *seq = LoadRandomSequence((unsigned int)rectCount, 0, (unsigned int)rectCount - 1); int *seq = LoadRandomSequence((unsigned int)rectCount, 0, (unsigned int)rectCount - 1);
float rectSeqWidth = rectCount*rectWidth; float rectSeqWidth = rectCount*rectWidth;
float startX = (screenWidth - rectSeqWidth)*0.5f; float startX = (screenWidth - rectSeqWidth)*0.5f;
@@ -142,8 +146,8 @@ static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWid
{ {
int rectHeight = (int)Remap((float)seq[i], 0, rectCount - 1, 0, screenHeight); int rectHeight = (int)Remap((float)seq[i], 0, rectCount - 1, 0, screenHeight);
rectangles[i].c = GenerateRandomColor(); rectangles[i].color = GenerateRandomColor();
rectangles[i].r = CLITERAL(Rectangle){ startX + i*rectWidth, screenHeight - rectHeight, rectWidth, (float)rectHeight }; rectangles[i].rect = CLITERAL(Rectangle){ startX + i*rectWidth, screenHeight - rectHeight, rectWidth, (float)rectHeight };
} }
UnloadRandomSequence(seq); UnloadRandomSequence(seq);
@@ -162,28 +166,13 @@ static void ShuffleColorRectSequence(ColorRect *rectangles, int rectCount)
// Swap only the color and height // Swap only the color and height
ColorRect tmp = *r1; ColorRect tmp = *r1;
r1->c = r2->c; r1->color = r2->color;
r1->r.height = r2->r.height; r1->rect.height = r2->rect.height;
r1->r.y = r2->r.y; r1->rect.y = r2->rect.y;
r2->c = tmp.c; r2->color = tmp.color;
r2->r.height = tmp.r.height; r2->rect.height = tmp.rect.height;
r2->r.y = tmp.r.y; r2->rect.y = tmp.rect.y;
} }
UnloadRandomSequence(seq); 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 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);
}