-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject1.cpp
More file actions
79 lines (75 loc) · 1.66 KB
/
project1.cpp
File metadata and controls
79 lines (75 loc) · 1.66 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "window.h"
#include "shared.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <thread>
#include <unistd.h>
Shared *shared;
Window *window;
AppConfig *config;
bool running = true;
void exitListener()
{
while (running)
{
char c = getchar();
if(c == 'x')
running = false;
}
}
void ballFunction(int arg)
{
int id = arg;
while (running && shared->balls[id]->bounceCounter < 5)
{
usleep(config->speed);
shared->balls[id]->updateBall();
}
shared->balls[id]->currentX = -1;
shared->balls[id]->currentY = -1;
}
void updateWindowFct()
{
while (running)
{
usleep(50000);
window->updateWindow(shared);
}
delete window;
}
int main(int argc, char *argv[])
{
srand(time(NULL));
window = new Window();
window->drawSquare();
shared = new Shared();
//config init
config = new AppConfig();
config->balls = argc >= 2 ? atoi(argv[1]) : 10;
config->delay = argc >= 3 ? atoi(argv[2]) : 5;
config->speed = argc >= 4 ? atoi(argv[3]) : 50000;
//
std::vector<std::thread> threads;
//create control threads
std::thread windowThread(updateWindowFct);
std::thread exitThread(exitListener);
//create ball threads
for (int i = 0; i < config->balls; i++)
{
shared->AddBall();
threads.push_back(std::thread(ballFunction,i));
sleep(config->delay);
if(!running)
break;
}
windowThread.join();
exitThread.join();
for (int i = 2; i < config->balls+2; i++)
{
threads[i].join();
}
delete shared;
return 0;
}