From 34feacc1480f0cdff8a39169dda30bd9446e4504 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 2 May 2019 12:22:54 +0200 Subject: [PATCH] new example: core_loading_thread Data loading in a second thread with progress bar in main thread --- examples/core/core_loading_thread.c | 133 ++++++++++++++++++++++++++ examples/core/core_loading_thread.png | Bin 0 -> 15032 bytes 2 files changed, 133 insertions(+) create mode 100644 examples/core/core_loading_thread.c create mode 100644 examples/core/core_loading_thread.png diff --git a/examples/core/core_loading_thread.c b/examples/core/core_loading_thread.c new file mode 100644 index 000000000..baf4100dc --- /dev/null +++ b/examples/core/core_loading_thread.c @@ -0,0 +1,133 @@ +/******************************************************************************************* +* +* raylib example - loading thread +* +* 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) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#include "pthread.h" // POSIX style threads management + +#include // Required for clock() function + +static bool dataLoaded = false; // Loading data semaphore +static void *LoadDataThread(void *arg); // Loading data thread function declaration + +static int dataProgress = 0; // Data progress accumulator + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - loading thread"); + + pthread_t threadId; // Loading data thread id + + int state = 0; // 0-Waiting, 1-Loading, 2-Finished + int framesCounter = 0; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (state == 0) + { + 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 = 1; + } + } + else if (state == 1) + { + framesCounter++; + if (dataLoaded) + { + framesCounter = 0; + state = 2; + } + } + else if (state == 2) + { + if (IsKeyPressed(KEY_ENTER)) + { + // Reset everything to launch again + dataLoaded = false; + dataProgress = 0; + state = 0; + } + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + if (state == 0) DrawText("PRESS ENTER to START LOADING DATA", 150, 170, 20, DARKGRAY); + else if (state == 1) + { + DrawRectangle(150, 200, dataProgress, 60, SKYBLUE); + if ((framesCounter/15)%2) DrawText("LOADING DATA...", 240, 210, 40, DARKBLUE); + } + else if (state == 2) + { + DrawRectangle(150, 200, 500, 60, LIME); + DrawText("DATA LOADED!", 250, 210, 40, GREEN); + } + + DrawRectangleLines(150, 200, 500, 60, DARKGRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + 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 + dataLoaded = true; + + return NULL; +} \ No newline at end of file diff --git a/examples/core/core_loading_thread.png b/examples/core/core_loading_thread.png new file mode 100644 index 0000000000000000000000000000000000000000..957bd19840a8067b5dbc9d77cd90136f3623bf5b GIT binary patch literal 15032 zcmeAS@N?(olHy`uVBq!ia0y~yU{+vYU_8XZ#=yWJp1k%11A|q)r;B4qMO^ZqUteF> zw*?wVF)dcaL6mSXF1r{Y$J=;OKmselSk-zUgFVG$ZExFw42~Wwgh5Z4gPZU{?2*GaR-HNW5+6QQL;o4Z9$2*oe~&ASYbL;l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