Advertisement
KDOXG

Input test

Oct 19th, 2019
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <termios.h>
  6. #include <unistd.h>
  7.  
  8. /* reads from keypress, doesn't echo */
  9. int getch(void)
  10. {
  11.     struct termios oldattr, newattr;
  12.     int ch;
  13.     tcgetattr( STDIN_FILENO, &oldattr );
  14.     newattr = oldattr;
  15.     newattr.c_lflag &= ~( ICANON | ECHO );
  16.     tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
  17.     ch = getchar();
  18.     tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
  19.     return ch;
  20. }
  21.  
  22. pthread_mutex_t m;
  23.  
  24. struct control{
  25.     int x;
  26.     int y;
  27.     int *stop;
  28. };
  29.  
  30. void* move(void* arg);
  31. void* animation(void* arg);
  32.  
  33. int main()
  34. {
  35.     system("clear");
  36.     int ch;
  37.     pthread_t thread;
  38.     pthread_create(&thread,NULL,move,(void*)&ch);
  39.     ch = getch();
  40.     while ((ch = getch()) != EOF
  41.             && ch != '\n');
  42.     pthread_join(thread,NULL);
  43.     return 0;
  44. }
  45.  
  46. void* move(void* arg)
  47. {
  48.     int *action = (int*)arg;
  49.     int aux;
  50.     pthread_t thread;
  51.     struct control movement = {
  52.         .x = 0,
  53.         .y = 0,
  54.         .stop = action
  55.     };
  56.     pthread_create(&thread,NULL,animation,(void*)&movement);
  57.     do
  58.     {
  59.         aux = *action
  60.         if (aux == '\n')
  61.             break;
  62.         switch(aux)
  63.         {
  64.             case 'w':
  65.                 if (movement.y > 0)
  66.                     movement.y--;
  67.                 break;
  68.             case 'a':
  69.                 if (movement.x > 0)
  70.                     movement.x--;
  71.                 break;
  72.             case 's':
  73.                 if (movement.y < 16)
  74.                     movement.y++;
  75.                 break;
  76.             case 'd':
  77.                 if (movement.x < 16)
  78.                     movement.x++;
  79.                 break;
  80.             default:
  81.                 break;
  82.         }
  83.         pthread_mutex_lock(&m);
  84.         *action = 0;
  85.         pthread_mutex_unlock(&m);
  86.         while (*action == 0) sched_yield();
  87.     }while (1);
  88.     pthread_join(thread,NULL);
  89.     return NULL;
  90. }
  91.  
  92. void* animation(void* arg)
  93. {
  94.     struct control *action = (struct control*)arg;
  95.     unsigned long long i;
  96.     unsigned j, k;
  97.     for (i = 0; i < ULLONG_MAX && *(action->stop) == 0; i++)
  98.     {
  99.         for (k = 0; k < action->y; k++)
  100.             printf("\n");
  101.         for (j = 0; j < action->x; j++)
  102.             printf(" ");
  103.         printf("O");
  104.         for (unsigned short n = 0; n < USHRT_MAX; n++);
  105.         sched_yield();
  106.     }
  107.     return NULL;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement