diff options
Diffstat (limited to 'examples/web/core/core_loading_thread.c')
| -rw-r--r-- | examples/web/core/core_loading_thread.c | 74 |
1 files changed, 37 insertions, 37 deletions
diff --git a/examples/web/core/core_loading_thread.c b/examples/web/core/core_loading_thread.c index b867ddd..64e34e0 100644 --- a/examples/web/core/core_loading_thread.c +++ b/examples/web/core/core_loading_thread.c @@ -2,7 +2,7 @@ * * raylib example - loading thread * -* NOTE: This example requires linking with pthreads library, +* NOTE: This example requires linking with pthreads library, * on MinGW, it can be accomplished passing -static parameter to compiler * * This example has been created using raylib 2.5 (www.raylib.com) @@ -16,7 +16,6 @@ #include "pthread.h" // POSIX style threads management -#include <stdatomic.h> // C11 atomic data types #include <time.h> // Required for: clock() #if defined(PLATFORM_WEB) @@ -25,7 +24,7 @@ // Using C11 atomics for synchronization // NOTE: A plain bool (or any plain data type for that matter) can't be used for inter-thread synchronization -static atomic_bool dataLoaded = ATOMIC_VAR_INIT(false); // Data Loaded completion indicator +static bool dataLoaded = false; // Data Loaded completion indicator static void *LoadDataThread(void *arg); // Loading data thread function declaration static int dataProgress = 0; // Data progress accumulator @@ -49,20 +48,20 @@ int framesCounter = 0; void UpdateDrawFrame(void); // Update and Draw one frame //---------------------------------------------------------------------------------- -// Main Enry Point +// Program Main Entry Point //---------------------------------------------------------------------------------- -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- InitWindow(screenWidth, screenHeight, "raylib [core] example - loading thread"); - + #if defined(PLATFORM_WEB) emscripten_set_main_loop(UpdateDrawFrame, 0, 1); #else SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- - + // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { @@ -78,29 +77,6 @@ int main() return 0; } -// Loading data thread function definition -static void *LoadDataThread(void *arg) -{ - int timeCounter = 0; // Time counted in ms - clock_t prevTime = clock(); // Previous time - - // We simulate data loading with a time counter for 5 seconds - while (timeCounter < 5000) - { - clock_t currentTime = clock() - prevTime; - timeCounter = currentTime*1000/CLOCKS_PER_SEC; - - // We accumulate time over a global variable to be used in - // main thread as a progress bar - dataProgress = timeCounter/10; - } - - // When data has finished loading, we set global variable - atomic_store(&dataLoaded, true); - - return NULL; -} - //---------------------------------------------------------------------------------- // Module Functions Definition //---------------------------------------------------------------------------------- @@ -124,7 +100,7 @@ void UpdateDrawFrame(void) case STATE_LOADING: { framesCounter++; - if (atomic_load(&dataLoaded)) + if (dataLoaded) { framesCounter = 0; state = STATE_FINISHED; @@ -135,7 +111,7 @@ void UpdateDrawFrame(void) if (IsKeyPressed(KEY_ENTER)) { // Reset everything to launch again - atomic_store(&dataLoaded, false); + dataLoaded = false; dataProgress = 0; state = STATE_WAITING; } @@ -149,27 +125,51 @@ void UpdateDrawFrame(void) BeginDrawing(); ClearBackground(RAYWHITE); - - switch (state) + + 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); - + } break; case STATE_FINISHED: { DrawRectangle(150, 200, 500, 60, LIME); DrawText("DATA LOADED!", 250, 210, 40, GREEN); - + } break; default: break; } - + DrawRectangleLines(150, 200, 500, 60, DARKGRAY); EndDrawing(); //---------------------------------------------------------------------------------- } + + +// Loading data thread function definition +static void *LoadDataThread(void *arg) +{ + int timeCounter = 0; // Time counted in ms + clock_t prevTime = clock(); // Previous time + + // We simulate data loading with a time counter for 5 seconds + while (timeCounter < 5000) + { + clock_t currentTime = clock() - prevTime; + timeCounter = currentTime*1000/CLOCKS_PER_SEC; + + // We accumulate time over a global variable to be used in + // main thread as a progress bar + dataProgress = timeCounter/10; + } + + // When data has finished loading, we set global variable + dataLoaded = true; + + return NULL; +}
\ No newline at end of file |
