-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.c
60 lines (52 loc) · 1.83 KB
/
display.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @authors LOPES Marco, ISELI Cyril and RINGOT Gaëtan
* Purpose: Management of display
* Language: C
* Date : november 2016
*/
#include "display.h"
#include "gfx.h"
#include <unistd.h>
/**
* Display "Game of Life"
* @param paramsDisplay: struct which contain all informations to perform the display
* @return NULL
*/
void *display(void *paramsDisplay) {
paramsDisplaySt *params = (paramsDisplaySt *) paramsDisplay;
struct gfx_context_t *ctxt = gfx_create("gofl", params->width, params->height);
if (!ctxt) {
fprintf(stderr, "Graphic mode initialization failed!\n");
exit(1);
}
pthread_barrier_wait(params->displayInitialised);
struct timespec start, finish;
double microSecondToWait = 1000000.0 / params->frequency;
while (!*params->quit) {
clock_gettime(CLOCK_REALTIME, &start);
gfx_clear(ctxt, COLOR_BLACK);
for (uint line = 0; line < params->height; ++line) {
for (uint column = 0; column < params->width; ++column) {
if ((*params->oldState)[line][column]) {
gfx_putpixel(ctxt, column, line, COLOR_YELLOW);
}
}
}
gfx_present(ctxt);
pthread_barrier_wait(params->workerDisplayBarrier);
if (*params->end)
*params->quit = *params->end;
bool **temp = *params->oldState;
*params->oldState = *params->actualState;
*params->actualState = temp;
pthread_barrier_wait(params->workerDisplayBarrier);
clock_gettime(CLOCK_REALTIME, &finish);
double elapsed = (finish.tv_sec - start.tv_sec) * 1000000;
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000.0;
if (microSecondToWait - elapsed > 0) {
usleep(microSecondToWait - elapsed);
}
}
gfx_destroy(ctxt);
return NULL;
}