Skip to content

Commit 68befcc

Browse files
committed
Update shapes_penrose_tile.c
1 parent b1f8cde commit 68befcc

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

examples/shapes/shapes_penrose_tile.c

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ static void PushTurtleState(TurtleState state);
5757
static TurtleState PopTurtleState(void);
5858
static PenroseLSystem CreatePenroseLSystem(float drawLength);
5959
static void BuildProductionStep(PenroseLSystem *ls);
60-
static void BuildPenroseLSystem(PenroseLSystem *ls, float drawLength, int generations);
6160
static void DrawPenroseLSystem(PenroseLSystem *ls);
6261

6362
//------------------------------------------------------------------------------------
@@ -78,10 +77,11 @@ int main(void)
7877
int maxGenerations = 4;
7978
int generations = 0;
8079

81-
PenroseLSystem ls = {0};
82-
BuildPenroseLSystem(&ls, drawLength*(generations/(float)maxGenerations), generations);
80+
// Initializee new penrose tile
81+
PenroseLSystem ls = CreatePenroseLSystem(drawLength*(generations/(float)maxGenerations));
82+
for (int i = 0; i < generations; i++) BuildProductionStep(&ls);
8383

84-
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
84+
SetTargetFPS(120); // Set our game to run at 60 frames-per-second
8585
//---------------------------------------------------------------------------------------
8686

8787
// Main game loop
@@ -107,7 +107,12 @@ int main(void)
107107
}
108108
}
109109

110-
if (rebuild) BuildPenroseLSystem(&ls, drawLength*(generations/(float)maxGenerations), generations);
110+
if (rebuild)
111+
{
112+
RL_FREE(ls.production); // Free previous production for re-creation
113+
ls = CreatePenroseLSystem(drawLength*(generations/(float)maxGenerations));
114+
for (int i = 0; i < generations; i++) BuildProductionStep(&ls);
115+
}
111116
//----------------------------------------------------------------------------------
112117

113118
// Draw
@@ -137,22 +142,26 @@ int main(void)
137142
//----------------------------------------------------------------------------------
138143
// Module Functions Definition
139144
//----------------------------------------------------------------------------------
140-
void PushTurtleState(TurtleState state)
145+
// Push turtle state for next step
146+
static void PushTurtleState(TurtleState state)
141147
{
142148
if (turtleTop < (TURTLE_STACK_MAX_SIZE - 1)) turtleStack[++turtleTop] = state;
143149
else TraceLog(LOG_WARNING, "TURTLE STACK OVERFLOW!");
144150
}
145151

146-
TurtleState PopTurtleState(void)
152+
// Pop turtle state step
153+
static TurtleState PopTurtleState(void)
147154
{
148155
if (turtleTop >= 0) return turtleStack[turtleTop--];
149156
else TraceLog(LOG_WARNING, "TURTLE STACK UNDERFLOW!");
150157

151158
return (TurtleState){ 0 };
152159
}
153160

154-
PenroseLSystem CreatePenroseLSystem(float drawLength)
161+
// Create a new penrose tile structure
162+
static PenroseLSystem CreatePenroseLSystem(float drawLength)
155163
{
164+
// TODO: Review constant values assignment on recreation?
156165
PenroseLSystem ls = {
157166
.steps = 0,
158167
.ruleW = "YF++ZF4-XF[-YF4-WF]++",
@@ -170,7 +179,8 @@ PenroseLSystem CreatePenroseLSystem(float drawLength)
170179
return ls;
171180
}
172181

173-
void BuildProductionStep(PenroseLSystem *ls)
182+
// Build next penrose step
183+
static void BuildProductionStep(PenroseLSystem *ls)
174184
{
175185
char *newProduction = (char *)RL_MALLOC(sizeof(char)*STR_MAX_SIZE);
176186
newProduction[0] = '\0';
@@ -205,18 +215,13 @@ void BuildProductionStep(PenroseLSystem *ls)
205215
RL_FREE(newProduction);
206216
}
207217

208-
void BuildPenroseLSystem(PenroseLSystem *ls, float drawLength, int generations)
209-
{
210-
*ls = CreatePenroseLSystem(drawLength);
211-
for (int i = 0; i < generations; i++) BuildProductionStep(ls);
212-
}
213-
214-
void DrawPenroseLSystem(PenroseLSystem *ls)
218+
// Draw penrose tile lines
219+
static void DrawPenroseLSystem(PenroseLSystem *ls)
215220
{
216221
Vector2 screenCenter = { GetScreenWidth()/2, GetScreenHeight()/2 };
217222

218223
TurtleState turtle = {
219-
.origin = {0},
224+
.origin = { 0 },
220225
.angle = -90.0f
221226
};
222227

@@ -257,18 +262,9 @@ void DrawPenroseLSystem(PenroseLSystem *ls)
257262

258263
repeats = 1;
259264
}
260-
else if (step == '[')
261-
{
262-
PushTurtleState(turtle);
263-
}
264-
else if (step == ']')
265-
{
266-
turtle = PopTurtleState();
267-
}
268-
else if ((step >= 48) && (step <= 57))
269-
{
270-
repeats = (int) step - 48;
271-
}
265+
else if (step == '[') PushTurtleState(turtle);
266+
else if (step == ']') turtle = PopTurtleState();
267+
else if ((step >= 48) && (step <= 57)) repeats = (int) step - 48;
272268
}
273269

274270
turtleTop = -1;

0 commit comments

Comments
 (0)