Skip to content

Commit 8b3c68f

Browse files
committed
Update core_random_sequence.c
1 parent 8f32c50 commit 8b3c68f

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

examples/core/core_random_sequence.c

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
// Types and Structures Definition
2525
//----------------------------------------------------------------------------------
2626
typedef struct ColorRect {
27-
Color c;
28-
Rectangle r;
27+
Color color;
28+
Rectangle rect;
2929
} ColorRect;
3030

3131
//------------------------------------------------------------------------------------
@@ -34,7 +34,6 @@ typedef struct ColorRect {
3434
static Color GenerateRandomColor();
3535
static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight);
3636
static void ShuffleColorRectSequence(ColorRect *rectangles, int rectCount);
37-
static void DrawTextCenterKeyHelp(const char *key, const char *text, int posX, int posY, int fontSize, Color color);
3837

3938
//------------------------------------------------------------------------------------
4039
// Program main entry point
@@ -66,7 +65,9 @@ int main(void)
6665
{
6766
rectCount++;
6867
rectSize = (float)screenWidth/rectCount;
69-
free(rectangles);
68+
RL_FREE(rectangles);
69+
70+
// Re-generate random sequence with new count
7071
rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight);
7172
}
7273

@@ -76,7 +77,9 @@ int main(void)
7677
{
7778
rectCount--;
7879
rectSize = (float)screenWidth/rectCount;
79-
free(rectangles);
80+
RL_FREE(rectangles);
81+
82+
// Re-generate random sequence with new count
8083
rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight);
8184
}
8285
}
@@ -88,20 +91,20 @@ int main(void)
8891

8992
ClearBackground(RAYWHITE);
9093

91-
int fontSize = 20;
9294
for (int i = 0; i < rectCount; i++)
9395
{
94-
DrawRectangleRec(rectangles[i].r, rectangles[i].c);
95-
DrawTextCenterKeyHelp("SPACE", "to shuffle the sequence.", 10, screenHeight - 96, fontSize, BLACK);
96-
DrawTextCenterKeyHelp("UP", "to add a rectangle and generate a new sequence.", 10, screenHeight - 64, fontSize, BLACK);
97-
DrawTextCenterKeyHelp("DOWN", "to remove a rectangle and generate a new sequence.", 10, screenHeight - 32, fontSize, BLACK);
96+
DrawRectangleRec(rectangles[i].rect, rectangles[i].color);
97+
98+
DrawText("Press SPACE to shuffle the sequence", 10, screenHeight - 96, 20, BLACK);
99+
100+
DrawText("Press SPACE to shuffle the current sequence", 10, screenHeight - 96, 20, BLACK);
101+
DrawText("Press UP to add a rectangle and generate a new sequence", 10, screenHeight - 64, 20, BLACK);
102+
DrawText("Press DOWN to remove a rectangle and generate a new sequence", 10, screenHeight - 32, 20, BLACK);
98103
}
99104

100-
const char *rectCountText = TextFormat("%d rectangles", rectCount);
101-
int rectCountTextSize = MeasureText(rectCountText, fontSize);
102-
DrawText(rectCountText, screenWidth - rectCountTextSize - 10, 10, fontSize, BLACK);
105+
DrawText(TextFormat("Count: %d rectangles", rectCount), 10, 10, 20, MAROON);
103106

104-
DrawFPS(10, 10);
107+
DrawFPS(screenWidth - 80, 10);
105108

106109
EndDrawing();
107110
//----------------------------------------------------------------------------------
@@ -133,7 +136,8 @@ static Color GenerateRandomColor()
133136

134137
static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight)
135138
{
136-
ColorRect *rectangles = (ColorRect *)malloc((int)rectCount*sizeof(ColorRect));
139+
ColorRect *rectangles = (ColorRect *)RL_CALLOC((int)rectCount, sizeof(ColorRect));
140+
137141
int *seq = LoadRandomSequence((unsigned int)rectCount, 0, (unsigned int)rectCount - 1);
138142
float rectSeqWidth = rectCount*rectWidth;
139143
float startX = (screenWidth - rectSeqWidth)*0.5f;
@@ -142,8 +146,8 @@ static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWid
142146
{
143147
int rectHeight = (int)Remap((float)seq[i], 0, rectCount - 1, 0, screenHeight);
144148

145-
rectangles[i].c = GenerateRandomColor();
146-
rectangles[i].r = CLITERAL(Rectangle){ startX + i*rectWidth, screenHeight - rectHeight, rectWidth, (float)rectHeight };
149+
rectangles[i].color = GenerateRandomColor();
150+
rectangles[i].rect = CLITERAL(Rectangle){ startX + i*rectWidth, screenHeight - rectHeight, rectWidth, (float)rectHeight };
147151
}
148152

149153
UnloadRandomSequence(seq);
@@ -162,28 +166,13 @@ static void ShuffleColorRectSequence(ColorRect *rectangles, int rectCount)
162166

163167
// Swap only the color and height
164168
ColorRect tmp = *r1;
165-
r1->c = r2->c;
166-
r1->r.height = r2->r.height;
167-
r1->r.y = r2->r.y;
168-
r2->c = tmp.c;
169-
r2->r.height = tmp.r.height;
170-
r2->r.y = tmp.r.y;
169+
r1->color = r2->color;
170+
r1->rect.height = r2->rect.height;
171+
r1->rect.y = r2->rect.y;
172+
r2->color = tmp.color;
173+
r2->rect.height = tmp.rect.height;
174+
r2->rect.y = tmp.rect.y;
171175
}
172176

173177
UnloadRandomSequence(seq);
174178
}
175-
176-
static void DrawTextCenterKeyHelp(const char *key, const char *text, int posX, int posY, int fontSize, Color color)
177-
{
178-
int spaceSize = MeasureText(" ", fontSize);
179-
int pressSize = MeasureText("Press", fontSize);
180-
int keySize = MeasureText(key, fontSize);
181-
int textSizeCurrent = 0;
182-
183-
DrawText("Press", posX, posY, fontSize, color);
184-
textSizeCurrent += pressSize + 2*spaceSize;
185-
DrawText(key, posX + textSizeCurrent, posY, fontSize, RED);
186-
DrawRectangle(posX + textSizeCurrent, posY + fontSize, keySize, 3, RED);
187-
textSizeCurrent += keySize + 2*spaceSize;
188-
DrawText(text, posX + textSizeCurrent, posY, fontSize, color);
189-
}

0 commit comments

Comments
 (0)