core_loading_thread.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <stdatomic.h>
  17. #include <time.h> // Required for clock() function
  18. static atomic_bool dataLoaded = ATOMIC_VAR_INIT(false); // Data Loaded completion indicator
  19. static void *LoadDataThread(void *arg); // Loading data thread function declaration
  20. static int dataProgress = 0; // Data progress accumulator
  21. int main()
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. int screenWidth = 800;
  26. int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [core] example - loading thread");
  28. pthread_t threadId; // Loading data thread id
  29. enum { STATE_WAITING, STATE_LOADING, STATE_FINISHED } state = STATE_WAITING;
  30. int framesCounter = 0;
  31. SetTargetFPS(60);
  32. //--------------------------------------------------------------------------------------
  33. // Main game loop
  34. while (!WindowShouldClose()) // Detect window close button or ESC key
  35. {
  36. // Update
  37. //----------------------------------------------------------------------------------
  38. switch (state)
  39. {
  40. case STATE_WAITING:
  41. if (IsKeyPressed(KEY_ENTER))
  42. {
  43. int error = pthread_create(&threadId, NULL, &LoadDataThread, NULL);
  44. if (error != 0) TraceLog(LOG_ERROR, "Error creating loading thread");
  45. else TraceLog(LOG_INFO, "Loading thread initialized successfully");
  46. state = STATE_LOADING;
  47. }
  48. break;
  49. case STATE_LOADING:
  50. framesCounter++;
  51. if (atomic_load(&dataLoaded))
  52. {
  53. framesCounter = 0;
  54. state = STATE_FINISHED;
  55. }
  56. break;
  57. case STATE_FINISHED:
  58. if (IsKeyPressed(KEY_ENTER))
  59. {
  60. // Reset everything to launch again
  61. atomic_store(&dataLoaded, false);
  62. dataProgress = 0;
  63. state = STATE_WAITING;
  64. }
  65. break;
  66. }
  67. //----------------------------------------------------------------------------------
  68. // Draw
  69. //----------------------------------------------------------------------------------
  70. BeginDrawing();
  71. ClearBackground(RAYWHITE);
  72. switch(state) {
  73. case STATE_WAITING:
  74. DrawText("PRESS ENTER to START LOADING DATA", 150, 170, 20, DARKGRAY);
  75. break;
  76. case STATE_LOADING:
  77. DrawRectangle(150, 200, dataProgress, 60, SKYBLUE);
  78. if ((framesCounter/15)%2)
  79. DrawText("LOADING DATA...", 240, 210, 40, DARKBLUE);
  80. break;
  81. case STATE_FINISHED:
  82. DrawRectangle(150, 200, 500, 60, LIME);
  83. DrawText("DATA LOADED!", 250, 210, 40, GREEN);
  84. break;
  85. }
  86. DrawRectangleLines(150, 200, 500, 60, DARKGRAY);
  87. EndDrawing();
  88. //----------------------------------------------------------------------------------
  89. }
  90. // De-Initialization
  91. //--------------------------------------------------------------------------------------
  92. CloseWindow(); // Close window and OpenGL context
  93. //--------------------------------------------------------------------------------------
  94. return 0;
  95. }
  96. // Loading data thread function definition
  97. static void *LoadDataThread(void *arg)
  98. {
  99. int timeCounter = 0; // Time counted in ms
  100. clock_t prevTime = clock(); // Previous time
  101. // We simulate data loading with a time counter for 5 seconds
  102. while (timeCounter < 5000)
  103. {
  104. clock_t currentTime = clock() - prevTime;
  105. timeCounter = currentTime*1000/CLOCKS_PER_SEC;
  106. // We accumulate time over a global variable to be used in
  107. // main thread as a progress bar
  108. dataProgress = timeCounter/10;
  109. }
  110. // When data has finished loading, we set global variable
  111. atomic_store(&dataLoaded, true);
  112. return NULL;
  113. }