119 lines
2.9 KiB
C
119 lines
2.9 KiB
C
/*******************\
|
|
| Pong |
|
|
| |
|
|
| By Alnotz |
|
|
\*******************/
|
|
/*
|
|
gcc -o pong_mthread pong_mthread.c `/usr/bin/pkg-config --libs --cflags ncurses` -pthread
|
|
*/
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
#include <time.h>
|
|
/*Ncurses header*/
|
|
#include <curses.h>
|
|
/*Inner window size*/
|
|
#define LINE 40
|
|
#define COL 80
|
|
/*Threads*/
|
|
pthread_t bracket_th;
|
|
pthread_t ball_th;
|
|
/*Mutex*/
|
|
pthread_mutex_t mtx;
|
|
/*Inner window*/
|
|
WINDOW *inwin;
|
|
/*Cursor position*/
|
|
int cursorl=LINE-1, cursorc=COL/2, startl, startc;
|
|
/*Key number*/
|
|
int ch;
|
|
/*Clock*/
|
|
struct timespec ts;
|
|
char buff[20];
|
|
void *bracket_thread(void *win)
|
|
{
|
|
for(int i=0; i<10; i++)
|
|
{
|
|
pthread_mutex_lock(&mtx);
|
|
wmove(inwin, 1, COL/4);
|
|
wattron(inwin, COLOR_PAIR(2));
|
|
wprintw(inwin, "Thread for bracket done!");
|
|
wattroff(inwin, COLOR_PAIR(2));
|
|
wrefresh(inwin);
|
|
pthread_mutex_unlock(&mtx);
|
|
}
|
|
}
|
|
void *ball_thread(void *win)
|
|
{
|
|
for(int i=0; i<10; i++)
|
|
{
|
|
pthread_mutex_lock(&mtx);
|
|
wmove(inwin, 2, COL/4);
|
|
wattron(inwin, COLOR_PAIR(2));
|
|
wprintw(inwin, "Thread for ball done!");
|
|
wattroff(inwin, COLOR_PAIR(2));
|
|
wrefresh(inwin);
|
|
pthread_mutex_unlock(&mtx);
|
|
}
|
|
}
|
|
int main(int argc, char *argv[])
|
|
{
|
|
/*Outer window*/
|
|
initscr();
|
|
startl=LINES/2-LINE/2;
|
|
startc=COLS/2-COL/2;
|
|
/*Color enabled*/
|
|
start_color();
|
|
init_pair(1, COLOR_RED, COLOR_BLACK);
|
|
init_pair(2, COLOR_GREEN, COLOR_BLACK);
|
|
init_pair(3, COLOR_BLUE, COLOR_BLACK);
|
|
/*Cursor hidden*/
|
|
curs_set(0);
|
|
/*Inner window set*/
|
|
inwin = newwin(LINE, COL, startl, startc);
|
|
/*Multithreading*/
|
|
pthread_mutex_init(&mtx, PTHREAD_MUTEX_NORMAL);
|
|
pthread_create(&bracket_th, NULL, bracket_thread, inwin);
|
|
pthread_create(&ball_th, NULL, ball_thread, inwin);
|
|
/*Timeout enabled for wgetch*/
|
|
wtimeout(inwin, 50);
|
|
/*Keypad enabled*/
|
|
keypad(inwin, TRUE);
|
|
/*Inner border*/
|
|
box(inwin, '|', '-');
|
|
/*Inner window title*/
|
|
wmove(inwin, 0, COL/4);
|
|
wattron(inwin, COLOR_PAIR(2));
|
|
wprintw(inwin, "Push 'q' to quit");
|
|
wmove(inwin, 0, COL-11);
|
|
timespec_get(&ts, TIME_UTC);
|
|
strftime(buff, sizeof buff, "%H:%M\'%S\"", gmtime(&ts.tv_sec));
|
|
wprintw(inwin, "%s", buff);
|
|
wattroff(inwin, COLOR_PAIR(2));
|
|
/*Inner window updated*/
|
|
wrefresh(inwin);
|
|
/*Key expected*/
|
|
ch = wgetch(inwin);
|
|
/*Inner window cleared before next update*/
|
|
wclear(inwin);
|
|
while(ch!=(int)'q')
|
|
{
|
|
/*Inner border*/
|
|
box(inwin, '|', '-');
|
|
/*Inner window title*/
|
|
wmove(inwin, 0, COL/4);
|
|
wattron(inwin, COLOR_PAIR(2));
|
|
wprintw(inwin, "Push 'q' to quit");
|
|
wmove(inwin, 0, COL-11);
|
|
timespec_get(&ts, TIME_UTC);
|
|
strftime(buff, sizeof buff, "%H:%M\'%S\"", gmtime(&ts.tv_sec));
|
|
wprintw(inwin, "%s", buff);
|
|
wattroff(inwin, COLOR_PAIR(2));
|
|
wrefresh(inwin);
|
|
ch = wgetch(inwin);
|
|
wclear(inwin);
|
|
}
|
|
endwin();
|
|
pthread_join(bracket_th, NULL);
|
|
pthread_join(ball_th, NULL);
|
|
pthread_exit(NULL);
|
|
return EXIT_SUCCESS;
|
|
}
|