Advertisement
KDOXG

Animation Test

Oct 20th, 2019
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <climits>
  4. #include <thread>
  5. //#include <mutex>
  6. #include <conio.h>
  7.  
  8. //std::mutex m;
  9.  
  10. struct control {
  11.     unsigned x;
  12.     unsigned y;
  13.     int* stop;
  14.  
  15.     control(unsigned x, unsigned y, int* action)
  16.     {
  17.         this->x = x;
  18.         this->y = y;
  19.         stop = action;
  20.     }
  21. };
  22.  
  23. void move(int* action);
  24. void animation(control* action);
  25.  
  26. int main(int argc, char* argv[])
  27. {
  28.     system("cls");
  29.     int ch;
  30.     std::thread anim(move, &ch);
  31.     //pthread_t thread;
  32.     //pthread_create(&thread, NULL, move, (void*)& ch);
  33.     while ((ch = _getch()) != EOF
  34.         && ch != '\n');
  35.     anim.join();
  36.     //pthread_join(thread, NULL);
  37.     return 0;
  38. }
  39.  
  40. void move(int* action)
  41. {
  42.     int aux;
  43.     control movement = control(0, 0, action);
  44.     //pthread_t thread;
  45.     //pthread_create(&thread, NULL, animation, (void*)& movement);
  46.     //std::thread anim(animation, &movement);
  47.     do
  48.     {
  49.         system("cls");
  50.         animation(&movement);
  51.         aux = *action;
  52.             if (aux == '\n')
  53.                 break;
  54.         switch (aux)
  55.         {
  56.         case 'w':
  57.             if (movement.y > 0)
  58.                 movement.y--;
  59.             break;
  60.         case 'a':
  61.             if (movement.x > 0)
  62.                 movement.x--;
  63.             break;
  64.         case 's':
  65.             if (movement.y < 16)
  66.                 movement.y++;
  67.             break;
  68.         case 'd':
  69.             if (movement.x < 16)
  70.                 movement.x++;
  71.             break;
  72.         default:
  73.             break;
  74.         }
  75.         //m.lock();
  76.         //pthread_mutex_lock(&m);
  77.         //*action = 0;
  78.         //m.unlock();
  79.         //pthread_mutex_unlock(&m);
  80.         //while (*action == 0) std::this_thread::yield();
  81.     } while (1);
  82.     //anim.join();
  83.     //pthread_join(thread, NULL);
  84.     return;
  85. }
  86.  
  87. void animation(control* action)
  88. {
  89.     for (unsigned k = 0; k < action->y; k++)
  90.         printf("\n");
  91.     for (unsigned j = 0; j < action->x; j++)
  92.         printf(" ");
  93.     printf("O");
  94.     //for (unsigned short n = 0; n < USHRT_MAX; n++);
  95.     //std::this_thread::yield();
  96.     //sched_yield();
  97.     return;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement