Rev 1 | Rev 13 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | muzer | 1 | #include "mainwindow.h" |
2 | |||
3 | MainWindow::MainWindow(QWidget *parent) : |
||
3 | tom | 4 | QGLWidget(parent) |
1 | muzer | 5 | { |
3 | tom | 6 | this->setFixedSize(800, 600); |
1 | muzer | 7 | |
8 | server = new QTcpServer(this); |
||
9 | connect(server, SIGNAL(newConnection()), this, SLOT(newConnection())); |
||
10 | server->listen(QHostAddress::Any, 4567); |
||
11 | |||
12 | timer = new QTimer(this); |
||
13 | connect(timer, SIGNAL(timeout()), this, SLOT(update())); |
||
14 | timer->start(50); |
||
15 | |||
16 | id = 0; |
||
17 | } |
||
18 | |||
19 | MainWindow::~MainWindow() |
||
20 | { |
||
21 | delete server; |
||
22 | } |
||
23 | |||
24 | void MainWindow::reset() |
||
25 | { |
||
26 | for (int i = 0; i < bikes.count(); i++) |
||
27 | { |
||
28 | bikes[i]->reset(); |
||
29 | } |
||
30 | } |
||
31 | |||
32 | void MainWindow::paintEvent(QPaintEvent *e) |
||
33 | { |
||
34 | e->accept(); |
||
35 | QPainter painter(this); |
||
36 | |||
3 | tom | 37 | painter.fillRect(0, 0, 800, 600, Qt::white); |
1 | muzer | 38 | |
39 | bool ready = true; |
||
40 | for (int i = 0; i < bikes.count(); i++) |
||
41 | { |
||
42 | if (!bikes[i]->isReady) |
||
43 | { |
||
44 | ready = false; |
||
45 | } |
||
46 | } |
||
47 | |||
48 | if (ready) |
||
49 | { |
||
50 | for (int i = 0; i < bikes.count(); i++) |
||
51 | { |
||
52 | if (bikes[i]->show) |
||
53 | { |
||
3 | tom | 54 | bikes[i]->hasHadGo = false; |
55 | bikes[i]->run(bikes); |
||
1 | muzer | 56 | bikes[i]->draw(&painter); |
57 | } |
||
58 | } |
||
3 | tom | 59 | Bike *bike; |
60 | foreach(bike,bikes) |
||
61 | { |
||
62 | while(!bike->hasHadGo) |
||
63 | { |
||
64 | } |
||
65 | } |
||
1 | muzer | 66 | } |
67 | |||
68 | checkClients(); |
||
69 | } |
||
70 | |||
71 | void MainWindow::newConnection() |
||
72 | { |
||
73 | while (server->hasPendingConnections()) |
||
74 | { |
||
75 | QTcpSocket *socket = server->nextPendingConnection(); |
||
76 | Bike *bike = new Bike(socket, id); |
||
77 | id += 1; |
||
78 | reset(); |
||
79 | |||
80 | bikes.append(bike); |
||
81 | cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | void MainWindow::checkClients() |
||
86 | { |
||
87 | for (int i = bikes.count() - 1; i >= 0; i--) |
||
88 | { |
||
89 | if (bikes[i]->isDisconnected) |
||
90 | { |
||
91 | delete bikes[i]; |
||
92 | bikes.removeAt(i); |
||
93 | cout << ":: Removed Bike: " << i << endl; |
||
94 | } |
||
95 | } |
||
96 | } |