Subversion Repositories QTron

Rev

Rev 3 | Rev 14 | 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
                bikes[i]->draw(&painter);
57
            }
58
        }
59
        Bike *bike;
60
        foreach(bike,bikes)
61
        {
62
            while(!bike->hasHadGo)
63
            {
64
            }
65
        }
66
    }
1 muzer 67
 
9 muzer 68
    checkClients();
1 muzer 69
}
70
 
71
void MainWindow::newConnection()
72
{
9 muzer 73
    while (server->hasPendingConnections())
74
    {
75
        QTcpSocket *socket = server->nextPendingConnection();
76
        Bike *bike = new Bike(socket, id);
77
        id += 1;
78
        reset();
1 muzer 79
 
9 muzer 80
        bikes.append(bike);
81
        cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl;
82
    }
1 muzer 83
}
84
 
85
void MainWindow::checkClients()
86
{
9 muzer 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
    }
1 muzer 96
}