core_loading_thread.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*******************************************************************************************
  2. *
  3. * raylib example - loading thread
  4. *
  5. * NOTE: This example requires linking with pthreads library,
  6. * on MinGW, it can be accomplished passing -static parameter to compiler
  7. *
  8. * This example has been created using raylib 2.5 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #include "pthread.h" // POSIX style threads management
  16. #include <time.h> // Required for clock() function
  17. static bool dataLoaded = false; // Loading data semaphore
  18. static void *LoadDataThread(void *arg); // Loading data thread function declaration
  19. static int dataProgress = 0; // Data progress accumulator
  20. int main()
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. int screenWidth = 800;
  25. int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [core] example - loading thread");
  27. pthread_t threadId; // Loading data thread id
  28. int state = 0; // 0-Waiting, 1-Loading, 2-Finished
  29. int framesCounter = 0;
  30. SetTargetFPS(60);
  31. //--------------------------------------------------------------------------------------
  32. // Main game loop
  33. while (!WindowShouldClose()) // Detect window close button or ESC key
  34. {
  35. // Update
  36. //----------------------------------------------------------------------------------
  37. if (state == 0)
  38. {
  39. if (IsKeyPressed(KEY_ENTER))
  40. {
  41. int error = pthread_create(&threadId, NULL, &LoadDataThread, NULL);
  42. if (error != 0) TraceLog(LOG_ERROR, "Error creating loading thread");
  43. else TraceLog(LOG_INFO, "Loading thread initialized successfully");
  44. state = 1;
  45. }
  46. }
  47. else if (state == 1)
  48. {
  49. framesCounter++;
  50. if (dataLoaded)
  51. {
  52. framesCounter = 0;
  53. state = 2;
  54. }
  55. }
  56. else if (state == 2)
  57. {
  58. if (IsKeyPressed(KEY_ENTER))
  59. {
  60. // Reset everything to launch again
  61. dataLoaded = false;
  62. dataProgress = 0;
  63. state = 0;
  64. }
  65. }
  66. //----------------------------------------------------------------------------------
  67. // Draw
  68. //----------------------------------------------------------------------------------
  69. BeginDrawing();
  70. ClearBackground(RAYWHITE);
  71. if (state == 0) DrawText("PRESS ENTER to START LOADING DATA", 150, 170, 20, DARKGRAY);
  72. else if (state == 1)
  73. {
  74. DrawRectangle(150, 200, dataProgress, 60, SKYBLUE);
  75. if ((framesCounter/15)%2) DrawText("LOADING DATA...", 240, 210, 40, DARKBLUE);
  76. }
  77. else if (state == 2)
  78. {
  79. DrawRectangle(150, 200, 500, 60, LIME);
  80. DrawText("DATA LOADED!", 250, 210, 40, GREEN);
  81. }
  82. DrawRectangleLines(150, 200, 500, 60, DARKGRAY);
  83. EndDrawing();
  84. //----------------------------------------------------------------------------------
  85. }
  86. // De-Initialization
  87. //--------------------------------------------------------------------------------------
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }
  92. // Loading data thread function definition
  93. static void *LoadDataThread(void *arg)
  94. {
  95. int timeCounter = 0; // Time counted in ms
  96. clock_t prevTime = clock(); // Previous time
  97. // We simulate data loading with a time counter for 5 seconds
  98. while (timeCounter < 5000)
  99. {
  100. clock_t currentTime = clock() - prevTime;
  101. timeCounter = currentTime*1000/CLOCKS_PER_SEC;
  102. // We accumulate time over a global variable to be used in
  103. // main thread as a progress bar
  104. dataProgress = timeCounter/10;
  105. }
  106. // When data has finished loading, we set global variable
  107. dataLoaded = true;
  108. return NULL;
  109. }