Subversion Repositories QTron

Rev

Rev 13 | Rev 27 | 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
{
9 muzer 6
    this->setFixedSize(800, 600);
1 muzer 7
 
9 muzer 8
    server = new QTcpServer(this);
9
    connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
10
    server->listen(QHostAddress::Any, 4567);
1 muzer 11
 
9 muzer 12
    timer = new QTimer(this);
13
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
14
    timer->start(50);
1 muzer 15
 
9 muzer 16
    id = 0;
1 muzer 17
}
18
 
19
MainWindow::~MainWindow()
20
{
9 muzer 21
    delete server;
1 muzer 22
}
23
 
24
void MainWindow::reset()
25
{
9 muzer 26
    for (int i = 0; i < bikes.count(); i++)
27
    {
28
        bikes[i]->reset();
29
    }
1 muzer 30
}
31
 
32
void MainWindow::paintEvent(QPaintEvent *e)
33
{
9 muzer 34
    e->accept();
35
    QPainter painter(this);
1 muzer 36
 
9 muzer 37
    painter.fillRect(0, 0, 800, 600, Qt::white);
1 muzer 38
 
9 muzer 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
    }
1 muzer 47
 
9 muzer 48
    if (ready)
49
    {
50
        for (int i = 0; i < bikes.count(); i++)
51
        {
52
            if (bikes[i]->show)
53
            {
54
                bikes[i]->hasHadGo = false;
55
                bikes[i]->run(bikes);
56
            }
57
        }
58
        Bike *bike;
59
        foreach(bike,bikes)
60
        {
61
            while(!bike->hasHadGo)
62
            {
63
            }
64
        }
13 tom 65
 
14 muzer 66
        for (int i = 0; i < bikes.count(); i++)
67
        {
68
            if (bikes[i]->show)
69
            {
70
                bikes[i]->draw(&painter,bikes);
71
            }
72
        }
9 muzer 73
    }
1 muzer 74
 
9 muzer 75
    checkClients();
1 muzer 76
}
77
 
78
void MainWindow::newConnection()
79
{
9 muzer 80
    while (server->hasPendingConnections())
81
    {
82
        QTcpSocket *socket = server->nextPendingConnection();
83
        Bike *bike = new Bike(socket, id);
84
        id += 1;
85
        reset();
1 muzer 86
 
9 muzer 87
        bikes.append(bike);
88
        cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl;
89
    }
1 muzer 90
}
91
 
92
void MainWindow::checkClients()
93
{
9 muzer 94
    for (int i = bikes.count() - 1; i >= 0; i--)
95
    {
96
        if (bikes[i]->isDisconnected)
97
        {
98
            delete bikes[i];
99
            bikes.removeAt(i);
100
            cout << ":: Removed Bike: " << i << endl;
101
        }
102
    }
1 muzer 103
}