examples: core_loading_thread: use symbolic names for state machine states

And while at it, use a switch clause to make the state machine
structure clearer.
This commit is contained in:
Ahmad Fatoum 2019-05-04 17:15:48 +02:00
parent 0fe409bfa9
commit 53d9beb534
No known key found for this signature in database
GPG key ID: C3EAC3DE9321D59B

View file

@ -34,7 +34,7 @@ int main()
pthread_t threadId; // Loading data thread id pthread_t threadId; // Loading data thread id
int state = 0; // 0-Waiting, 1-Loading, 2-Finished enum { STATE_WAITING, STATE_LOADING, STATE_FINISHED } state = STATE_WAITING;
int framesCounter = 0; int framesCounter = 0;
SetTargetFPS(60); SetTargetFPS(60);
@ -45,35 +45,35 @@ int main()
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (state == 0) switch (state)
{ {
case STATE_WAITING:
if (IsKeyPressed(KEY_ENTER)) if (IsKeyPressed(KEY_ENTER))
{ {
int error = pthread_create(&threadId, NULL, &LoadDataThread, NULL); int error = pthread_create(&threadId, NULL, &LoadDataThread, NULL);
if (error != 0) TraceLog(LOG_ERROR, "Error creating loading thread"); if (error != 0) TraceLog(LOG_ERROR, "Error creating loading thread");
else TraceLog(LOG_INFO, "Loading thread initialized successfully"); else TraceLog(LOG_INFO, "Loading thread initialized successfully");
state = 1; state = STATE_LOADING;
} }
} break;
else if (state == 1) case STATE_LOADING:
{
framesCounter++; framesCounter++;
if (dataLoaded) if (dataLoaded)
{ {
framesCounter = 0; framesCounter = 0;
state = 2; state = STATE_FINISHED;
} }
} break;
else if (state == 2) case STATE_FINISHED:
{ if (IsKeyPressed(KEY_ENTER))
if (IsKeyPressed(KEY_ENTER))
{ {
// Reset everything to launch again // Reset everything to launch again
dataLoaded = false; dataLoaded = false;
dataProgress = 0; dataProgress = 0;
state = 0; state = STATE_WAITING;
} }
break;
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -83,16 +83,19 @@ int main()
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
if (state == 0) DrawText("PRESS ENTER to START LOADING DATA", 150, 170, 20, DARKGRAY); switch(state) {
else if (state == 1) case STATE_WAITING:
{ DrawText("PRESS ENTER to START LOADING DATA", 150, 170, 20, DARKGRAY);
break;
case STATE_LOADING:
DrawRectangle(150, 200, dataProgress, 60, SKYBLUE); DrawRectangle(150, 200, dataProgress, 60, SKYBLUE);
if ((framesCounter/15)%2) DrawText("LOADING DATA...", 240, 210, 40, DARKBLUE); if ((framesCounter/15)%2)
} DrawText("LOADING DATA...", 240, 210, 40, DARKBLUE);
else if (state == 2) break;
{ case STATE_FINISHED:
DrawRectangle(150, 200, 500, 60, LIME); DrawRectangle(150, 200, 500, 60, LIME);
DrawText("DATA LOADED!", 250, 210, 40, GREEN); DrawText("DATA LOADED!", 250, 210, 40, GREEN);
break;
} }
DrawRectangleLines(150, 200, 500, 60, DARKGRAY); DrawRectangleLines(150, 200, 500, 60, DARKGRAY);
@ -130,4 +133,4 @@ static void *LoadDataThread(void *arg)
dataLoaded = true; dataLoaded = true;
return NULL; return NULL;
} }