QTfrontend/hwmap.cpp
author displacer
Sun, 24 Sep 2006 19:51:16 +0000
changeset 164 92cff18a3ab6
child 168 679e9b8912f5
permissions -rw-r--r--
first map preview added (still experimental)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     1
#include "hwmap.h"
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     2
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     3
#include "hwconsts.h"
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     4
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     5
#include <QMessageBox>
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     6
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     7
HWMap::HWMap() :
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     8
  m_isStarted(false)
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
     9
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    10
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    11
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    12
void HWMap::getImage(std::string seed) 
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    13
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    14
  m_seed=seed;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    15
  Start();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    16
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    17
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    18
void HWMap::ClientDisconnect()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    19
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    20
  QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    21
  im.setNumColors(2);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    22
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    23
  IPCSocket->close();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    24
  IPCSocket->deleteLater();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    25
  IPCSocket = 0;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    26
  IPCServer->close();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    27
  deleteLater();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    28
  
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    29
  emit ImageReceived(im);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    30
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    31
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    32
void HWMap::ClientRead()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    33
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    34
  readbuffer.append(IPCSocket->readAll());
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    35
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    36
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    37
void HWMap::SendToClientFirst()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    38
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    39
  std::string toSend=std::string("eseed ")+m_seed;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    40
  char ln=(char)toSend.length();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    41
  IPCSocket->write(&ln, 1);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    42
  IPCSocket->write(toSend.c_str(), ln);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    43
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    44
  IPCSocket->write("\x01!", 2);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    45
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    46
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    47
void HWMap::NewConnection()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    48
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    49
  QTcpSocket * client = IPCServer->nextPendingConnection();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    50
  if(!IPCSocket) {
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    51
    IPCServer->close();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    52
    IPCSocket = client;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    53
    connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    54
    connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    55
    SendToClientFirst();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    56
  } else {
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    57
    qWarning("2nd IPC client?!");
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    58
    client->disconnectFromHost();
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    59
  }
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    60
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    61
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    62
void HWMap::StartProcessError(QProcess::ProcessError error)
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    63
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    64
  QMessageBox::critical(0, tr("Error"),
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    65
			tr("Unable to run engine: %1 (")
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    66
			.arg(error) + bindir->absolutePath() + "/hwengine)");
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    67
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    68
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    69
void HWMap::Start()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    70
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    71
  IPCServer = new QTcpServer(this);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    72
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    73
  IPCServer->setMaxPendingConnections(1);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    74
  IPCSocket = 0;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    75
  if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    76
    QMessageBox::critical(0, tr("Error"),
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    77
			  tr("Unable to start the server: %1.")
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    78
			  .arg(IPCServer->errorString()));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    79
  }
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    80
  
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    81
  QProcess * process;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    82
  QStringList arguments;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    83
  process = new QProcess;
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    84
  connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    85
  arguments << "46631";
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    86
  arguments << "landpreview";
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    87
  process->start(bindir->absolutePath() + "/hwengine", arguments);
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    88
}