浏览代码

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.
pull/832/head
Ahmad Fatoum 5 年前
父节点
当前提交
53d9beb534
找不到此签名对应的密钥 GPG 密钥 ID: C3EAC3DE9321D59B
共有 1 个文件被更改,包括 25 次插入22 次删除
  1. +25
    -22
      examples/core/core_loading_thread.c

+ 25
- 22
examples/core/core_loading_thread.c 查看文件

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

正在加载...
取消
保存