Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <limits.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <termios.h>
- #include <unistd.h>
- /* reads from keypress, doesn't echo */
- int getch(void)
- {
- struct termios oldattr, newattr;
- int ch;
- tcgetattr( STDIN_FILENO, &oldattr );
- newattr = oldattr;
- newattr.c_lflag &= ~( ICANON | ECHO );
- tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
- ch = getchar();
- tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
- return ch;
- }
- pthread_mutex_t m;
- struct control{
- int x;
- int y;
- int *stop;
- };
- void* move(void* arg);
- void* animation(void* arg);
- int main()
- {
- system("clear");
- int ch;
- pthread_t thread;
- pthread_create(&thread,NULL,move,(void*)&ch);
- ch = getch();
- while ((ch = getch()) != EOF
- && ch != '\n');
- pthread_join(thread,NULL);
- return 0;
- }
- void* move(void* arg)
- {
- int *action = (int*)arg;
- int aux;
- pthread_t thread;
- struct control movement = {
- .x = 0,
- .y = 0,
- .stop = action
- };
- pthread_create(&thread,NULL,animation,(void*)&movement);
- do
- {
- aux = *action
- if (aux == '\n')
- break;
- switch(aux)
- {
- case 'w':
- if (movement.y > 0)
- movement.y--;
- break;
- case 'a':
- if (movement.x > 0)
- movement.x--;
- break;
- case 's':
- if (movement.y < 16)
- movement.y++;
- break;
- case 'd':
- if (movement.x < 16)
- movement.x++;
- break;
- default:
- break;
- }
- pthread_mutex_lock(&m);
- *action = 0;
- pthread_mutex_unlock(&m);
- while (*action == 0) sched_yield();
- }while (1);
- pthread_join(thread,NULL);
- return NULL;
- }
- void* animation(void* arg)
- {
- struct control *action = (struct control*)arg;
- unsigned long long i;
- unsigned j, k;
- for (i = 0; i < ULLONG_MAX && *(action->stop) == 0; i++)
- {
- for (k = 0; k < action->y; k++)
- printf("\n");
- for (j = 0; j < action->x; j++)
- printf(" ");
- printf("O");
- for (unsigned short n = 0; n < USHRT_MAX; n++);
- sched_yield();
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement