Projekty z kursu Systemy operacyjne 2 na PWr
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "Platform.h"
  2. #include <iostream>
  3. #include <ncurses.h>
  4. #include <unistd.h>
  5. Platform::Platform()
  6. {
  7. //ctor
  8. points = 0;
  9. color = 0;
  10. sprite = "<--->";
  11. }
  12. Platform::~Platform()
  13. {
  14. //dtor
  15. }
  16. const char *Platform::getSprite()
  17. {
  18. return sprite.c_str();
  19. }
  20. int Platform::getPosition()
  21. {
  22. return position;
  23. }
  24. int Platform::getEnd()
  25. {
  26. int endPos = position + sprite.length() - 1;
  27. return endPos;
  28. }
  29. int Platform::getColor()
  30. {
  31. return color;
  32. }
  33. void Platform::terminateThreads()
  34. {
  35. running = false;
  36. }
  37. void Platform::moveKey()
  38. {
  39. if(!initialized)
  40. {
  41. std::cout << "Scene size not initialized!" << std::endl;
  42. return;
  43. }
  44. int key;
  45. while(running)
  46. {
  47. ncursesMutex.lock();
  48. key = getch();
  49. ncursesMutex.unlock();
  50. // Freeze game
  51. if(freezed)
  52. {
  53. std::unique_lock<std::mutex> freezeLock(freezeMutex);
  54. while(freezed)
  55. {
  56. freezeCondition.wait(freezeLock);
  57. }
  58. // Ignore keys pressed when frozen
  59. flushinp();
  60. }
  61. switch(key)
  62. {
  63. case 'a':
  64. if(position > 0)
  65. {
  66. position--;
  67. }
  68. break;
  69. case 'd':
  70. if(position < xMax - sprite.length())
  71. {
  72. position++;
  73. }
  74. }
  75. }
  76. }
  77. std::thread Platform::moveKeyThread()
  78. {
  79. return std::thread(&Platform::moveKey, this);
  80. }
  81. void Platform::colorChange()
  82. {
  83. const int idleSeconds = 15;
  84. while(running)
  85. {
  86. color = rand() % 6 + 1;
  87. // Waiting - this must be interruptable, so it can't be just sleep(idleSeconds)
  88. for(int i = 0; i < idleSeconds * 10; i++)
  89. {
  90. // Freeze game
  91. if(freezed)
  92. {
  93. std::unique_lock<std::mutex> freezeLock(freezeMutex);
  94. while(freezed)
  95. {
  96. freezeCondition.wait(freezeLock);
  97. }
  98. }
  99. if(!running)
  100. {
  101. break;
  102. }
  103. // Every tick = 100 ms
  104. usleep(100000);
  105. }
  106. }
  107. }
  108. std::thread Platform::colorChangeThread()
  109. {
  110. return std::thread(&Platform::colorChange, this);
  111. }