(root)/mainwindow.cpp - Rev 3
Rev 1 |
Rev 9 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QGLWidget(parent)
{
this->setFixedSize(800, 600);
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server->listen(QHostAddress::Any, 4567);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(50);
id = 0;
}
MainWindow::~MainWindow()
{
delete server;
}
void MainWindow::reset()
{
for (int i = 0; i < bikes.count(); i++)
{
bikes[i]->reset();
}
}
void MainWindow::paintEvent(QPaintEvent *e)
{
e->accept();
QPainter painter(this);
painter.fillRect(0, 0, 800, 600, Qt::white);
bool ready = true;
for (int i = 0; i < bikes.count(); i++)
{
if (!bikes[i]->isReady)
{
ready = false;
}
}
if (ready)
{
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->hasHadGo = false;
bikes[i]->run(bikes);
bikes[i]->draw(&painter);
}
}
Bike *bike;
foreach(bike,bikes)
{
while(!bike->hasHadGo)
{
}
}
}
checkClients();
}
void MainWindow::newConnection()
{
while (server->hasPendingConnections())
{
QTcpSocket *socket = server->nextPendingConnection();
Bike *bike = new Bike(socket, id);
id += 1;
reset();
bikes.append(bike);
cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl;
}
}
void MainWindow::checkClients()
{
for (int i = bikes.count() - 1; i >= 0; i--)
{
if (bikes[i]->isDisconnected)
{
delete bikes[i];
bikes.removeAt(i);
cout << ":: Removed Bike: " << i << endl;
}
}
}