diff options
| author | totycro <totycro@unknown-horizons.org> | 2011-04-10 00:55:13 +0200 |
|---|---|---|
| committer | totycro <totycro@unknown-horizons.org> | 2011-04-10 00:55:13 +0200 |
| commit | da481762bded855ffd709114ee3a16ec4dbaaeb6 (patch) | |
| tree | 10f323140f11280f5185c19666a8412199613e0e /pacman-c++/mainwidget.cpp | |
| parent | 5b4ddc78484448886135e031bee3d423e2fd3b83 (diff) | |
| download | foop-da481762bded855ffd709114ee3a16ec4dbaaeb6.tar.gz foop-da481762bded855ffd709114ee3a16ec4dbaaeb6.tar.bz2 foop-da481762bded855ffd709114ee3a16ec4dbaaeb6.zip | |
Tell player about their color
minor cleanup
Diffstat (limited to 'pacman-c++/mainwidget.cpp')
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 420ea8f..25f554a 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -15,16 +15,20 @@ MainWidget::MainWidget(QWidget *parent) | |||
| 15 | //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); | 15 | //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); |
| 16 | //AudioPlayer::self()->play(AudioPlayer::Intro); | 16 | //AudioPlayer::self()->play(AudioPlayer::Intro); |
| 17 | 17 | ||
| 18 | bool connected = connectToServer(); | 18 | Color::Color myColor = connectToServer(); |
| 19 | if (!connected) { | 19 | if (myColor == Color::none) { |
| 20 | QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); | 20 | QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); |
| 21 | // TODO: quit application here or sth | 21 | // TODO: quit application here or sth |
| 22 | m_socket = NULL; | 22 | m_socket = NULL; |
| 23 | QTimer *timer = new QTimer(this); | 23 | QTimer *timer = new QTimer(this); |
| 24 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); | 24 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); |
| 25 | timer->start(Constants::tick); | 25 | timer->start(Constants::tick); |
| 26 | } else { | ||
| 27 | connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); | ||
| 26 | } | 28 | } |
| 27 | connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); | 29 | |
| 30 | // TODO: use mycolor | ||
| 31 | qDebug() << "myc" << myColor; | ||
| 28 | 32 | ||
| 29 | startGame(); | 33 | startGame(); |
| 30 | } | 34 | } |
| @@ -146,17 +150,8 @@ void MainWidget::tick() | |||
| 146 | } | 150 | } |
| 147 | } | 151 | } |
| 148 | } else { | 152 | } else { |
| 149 | QByteArray data = m_socket->readAll(); | ||
| 150 | //qDebug() << "read qbytes " << data.length(); | ||
| 151 | // TODO: normal conversion to std::string won't work, | ||
| 152 | // probably due to \0-bytes. both those methods will yield | ||
| 153 | // a 3 char long string | ||
| 154 | //std::string dataStr = std::string(data.constData()); | ||
| 155 | //std::string dataStr = QString(data).toStdString(); | ||
| 156 | std::string dataStr; | 153 | std::string dataStr; |
| 157 | for (int i = 0; i < data.size(); ++i) { | 154 | Util::QByteArrayToStdString(m_socket->readAll(), dataStr); |
| 158 | dataStr += data[i]; | ||
| 159 | } | ||
| 160 | 155 | ||
| 161 | //qDebug() << "read str " << dataStr.length(); | 156 | //qDebug() << "read str " << dataStr.length(); |
| 162 | ProtoBuf::MapUpdate packet; | 157 | ProtoBuf::MapUpdate packet; |
| @@ -181,7 +176,7 @@ void MainWidget::keyPressEvent(QKeyEvent* event) | |||
| 181 | return; | 176 | return; |
| 182 | 177 | ||
| 183 | QWidget::keyPressEvent(event); | 178 | QWidget::keyPressEvent(event); |
| 184 | Transmission::field_t newKey = translateKey(event->key()); | 179 | Transmission::field_t newKey = translateKey(event->key(), -1); |
| 185 | if (m_currentKey == newKey || newKey == -1) { | 180 | if (m_currentKey == newKey || newKey == -1) { |
| 186 | return; | 181 | return; |
| 187 | } | 182 | } |
| @@ -191,15 +186,7 @@ void MainWidget::keyPressEvent(QKeyEvent* event) | |||
| 191 | // send to server | 186 | // send to server |
| 192 | ProtoBuf::KeyPressUpdate packet; | 187 | ProtoBuf::KeyPressUpdate packet; |
| 193 | packet.set_newkey(m_currentKey); | 188 | packet.set_newkey(m_currentKey); |
| 194 | std::string dataStr = packet.SerializeAsString(); | 189 | Util::sendPacket(packet, m_socket); |
| 195 | unsigned int bytesWritten = m_socket->write(dataStr.c_str(), dataStr.length()); | ||
| 196 | m_socket->flush(); | ||
| 197 | if (bytesWritten != dataStr.length()) { | ||
| 198 | qDebug() << "written: " << bytesWritten; | ||
| 199 | qDebug() << "strl: " << dataStr.length(); | ||
| 200 | } | ||
| 201 | Q_ASSERT(bytesWritten == dataStr.length()); | ||
| 202 | |||
| 203 | qDebug() << "send key: " << m_currentKey; | 190 | qDebug() << "send key: " << m_currentKey; |
| 204 | } else { | 191 | } else { |
| 205 | 192 | ||
| @@ -244,10 +231,23 @@ void MainWidget::playerScoreClicked() | |||
| 244 | return; | 231 | return; |
| 245 | } | 232 | } |
| 246 | 233 | ||
| 247 | bool MainWidget::connectToServer() | 234 | Color::Color MainWidget::connectToServer() |
| 248 | { | 235 | { |
| 249 | m_socket = new QTcpSocket(this); | 236 | m_socket = new QTcpSocket(this); |
| 250 | m_socket->connectToHost("127.0.0.1", Constants::port); | 237 | m_socket->connectToHost("127.0.0.1", Constants::port); |
| 251 | return m_socket->waitForConnected(Constants::connection_timeout); | 238 | bool worked = m_socket->waitForConnected(Constants::connection_timeout); |
| 239 | if (worked) { | ||
| 240 | // additional init | ||
| 241 | // first packet is our color | ||
| 242 | worked = m_socket->waitForReadyRead(); | ||
| 243 | if (worked) { | ||
| 244 | std::string data; | ||
| 245 | Util::QByteArrayToStdString(m_socket->readAll(), data); | ||
| 246 | ProtoBuf::WhoAmI packet; | ||
| 247 | packet.ParseFromString(data); | ||
| 248 | return static_cast<Color::Color>(packet.color() & Transmission::color_mask); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | return Color::none; | ||
| 252 | } | 252 | } |
| 253 | 253 | ||
