Some refactoring qmlfrontend
authorunc0rr
Sun, 28 Sep 2014 00:18:01 +0400
branchqmlfrontend
changeset 10426 727a154cf784
parent 10424 4be6cd55f1cf
child 10428 7c25297720f1
Some refactoring
hedgewars/hwLibrary.pas
hedgewars/uFLGameConfig.pas
hedgewars/uFLIPC.pas
qmlFrontend/flib.h
qmlFrontend/hwengine.cpp
qmlFrontend/hwengine.h
qmlFrontend/previewimageprovider.cpp
--- a/hedgewars/hwLibrary.pas	Sat Sep 27 12:49:08 2014 +0400
+++ b/hedgewars/hwLibrary.pas	Sun Sep 28 00:18:01 2014 +0400
@@ -144,7 +144,9 @@
 {$ELSE}
 exports
     RunEngine,
-    registerIPCCallback,
+    runQuickGame,
+    getPreview,
+    registerPreviewCallback,
     ipcToEngine,
     flibInit,
     flibFree,
--- a/hedgewars/uFLGameConfig.pas	Sat Sep 27 12:49:08 2014 +0400
+++ b/hedgewars/uFLGameConfig.pas	Sun Sep 28 00:18:01 2014 +0400
@@ -2,12 +2,88 @@
 
 interface
 
-procedure ResetGameConfig; cdecl; export;
+procedure resetGameConfig; cdecl; export;
+procedure runQuickGame; cdecl; export;
+procedure getPreview; cdecl; export;
 
 implementation
 
-procedure ResetGameConfig; cdecl;
+const
+    MAXCONFIGS = 5;
+    MAXARGS = 32;
+
+type
+    TGameType = (gtPreview, gtLocal);
+    THedgehog = record
+            name: shortstring;
+            hat: shortstring;
+            end;
+    TTeam = record
+            teamName: shortstring;
+            flag: shortstring;
+            graveName: shortstring;
+            fortName: shortstring;
+            owner: shortstring;
+            extDriven: boolean;
+            botLevel: Longword;
+            hedgehogs: array[0..7] of THedgehog;
+            hogsNumber: Longword;
+            end;
+    TGameConfig = record
+            seed: shortstring;
+            theme: shortstring;
+            script: shortstring;
+            gameType: TGameType;
+            teams: array[0..7] of TTeam;
+            arguments: array[0..Pred(MAXARGS)] of shortstring;
+            argv: array[0..Pred(MAXARGS)] of PChar;
+            argumentsNumber: Longword;
+            end;
+    PGameConfig = ^TGameConfig;
+
+var currentConfig: TGameConfig;
+
+procedure queueExecution;
+var pConfig: PGameConfig;
+    i: Longword;
+begin
+    new(pConfig);
+    pConfig^:= currentConfig;
+
+    with pConfig^ do
+        for i:= 0 to Pred(MAXARGS) do
+        begin
+            if arguments[i][0] = #255 then 
+                arguments[i][255] = #0
+            else
+                arguments[i][byte(arguments[i][0]) + 1] = #0;
+            argv[i]:= @arguments[i][1]
+        end;
+
+    RunEngine(pConfig^.argumentsNumber, @pConfig^.argv);
+end;
+
+procedure resetGameConfig; cdecl;
 begin
 end;
 
+procedure runQuickGame; cdecl; export;
+begin
+
+end;
+
+procedure getPreview; cdecl; export;
+begin
+    with currentConfig do
+    begin
+        gameType:= gtPreview;
+        arguments[0]:= '';
+        arguments[1]:= '--internal';
+        arguments[2]:= '--landpreview';
+        argumentsNumber:= 3;
+    end;
+
+    queueExecution
+end;
+
 end.
--- a/hedgewars/uFLIPC.pas	Sat Sep 27 12:49:08 2014 +0400
+++ b/hedgewars/uFLIPC.pas	Sun Sep 28 00:18:01 2014 +0400
@@ -18,7 +18,7 @@
 function ipcReadFromFrontend: shortstring;
 function ipcCheckFromFrontend: boolean;
 
-procedure registerIPCCallback(p: pointer; f: TIPCCallback); cdecl; export;
+procedure registerPreviewCallback(p: pointer; f: TIPCCallback); cdecl; export;
 
 implementation
 
@@ -133,7 +133,7 @@
     until false
 end;
 
-procedure registerIPCCallback(p: pointer; f: TIPCCallback); cdecl; export;
+procedure registerPreviewCallback(p: pointer; f: TIPCCallback); cdecl; export;
 begin
     callbackPointer:= p;
     callbackFunction:= f;
--- a/qmlFrontend/flib.h	Sat Sep 27 12:49:08 2014 +0400
+++ b/qmlFrontend/flib.h	Sun Sep 28 00:18:01 2014 +0400
@@ -19,7 +19,7 @@
     } string255;
 
 typedef void RunEngine_t(int argc, const char ** argv);
-typedef void registerIPCCallback_t(void * context, void (*)(void * context, const char * msg, uint32_t len));
+typedef void registerPreviewCallback_t(void * context, void (*)(void * context, const char * msg, uint32_t len));
 typedef void ipcToEngine_t(const char * msg, uint8_t len);
 typedef void flibInit_t(const char * localPrefix, const char * userPrefix);
 typedef void flibFree_t();
--- a/qmlFrontend/hwengine.cpp	Sat Sep 27 12:49:08 2014 +0400
+++ b/qmlFrontend/hwengine.cpp	Sun Sep 28 00:18:01 2014 +0400
@@ -9,7 +9,7 @@
 
 extern "C" {
     RunEngine_t *RunEngine;
-    registerIPCCallback_t *registerIPCCallback;
+    registerPreviewCallback_t *registerPreviewCallback;
     ipcToEngine_t *ipcToEngine;
     flibInit_t *flibInit;
     flibFree_t *flibFree;
@@ -25,13 +25,13 @@
         qWarning() << "Engine library not found" << hwlib.errorString();
 
     RunEngine = (RunEngine_t*) hwlib.resolve("RunEngine");
-    registerIPCCallback = (registerIPCCallback_t*) hwlib.resolve("registerIPCCallback");
+    registerPreviewCallback = (registerPreviewCallback_t*) hwlib.resolve("registerIPCCallback");
     ipcToEngine = (ipcToEngine_t*) hwlib.resolve("ipcToEngine");
     flibInit = (flibInit_t*) hwlib.resolve("flibInit");
     flibFree = (flibFree_t*) hwlib.resolve("flibFree");
 
     flibInit(".", "~/.hedgewars");
-    registerIPCCallback(this, &engineMessageCallback);
+    registerPreviewCallback(this, &enginePreviewCallback);
 }
 
 HWEngine::~HWEngine()
@@ -80,7 +80,7 @@
     ipcToEngine(b.constData(), len);
 }
 
-void HWEngine::engineMessageCallback(void *context, const char * msg, quint32 len)
+void HWEngine::enginePreviewCallback(void *context, const char * msg, quint32 len)
 {
     HWEngine * obj = (HWEngine *)context;
     QByteArray b = QByteArray::fromRawData(msg, len);
@@ -92,9 +92,12 @@
 
 void HWEngine::engineMessageHandler(const QByteArray &msg)
 {
-    PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview"));
-    preview->setPixmap(msg);
-    emit previewImageChanged();
+    if(msg.size() == 128 * 256)
+    {
+        PreviewImageProvider * preview = (PreviewImageProvider *)m_engine->imageProvider(QLatin1String("preview"));
+        preview->setPixmap(msg);
+        emit previewImageChanged();
+    }
 }
 
 QString HWEngine::currentSeed()
--- a/qmlFrontend/hwengine.h	Sat Sep 27 12:49:08 2014 +0400
+++ b/qmlFrontend/hwengine.h	Sun Sep 28 00:18:01 2014 +0400
@@ -32,7 +32,7 @@
     QQmlEngine * m_engine;
     QString m_seed;
 
-    static void engineMessageCallback(void *context, const char * msg, quint32 len);
+    static void enginePreviewCallback(void *context, const char * msg, quint32 len);
     void sendIPC(const QByteArray &b);
 
 private slots:
--- a/qmlFrontend/previewimageprovider.cpp	Sat Sep 27 12:49:08 2014 +0400
+++ b/qmlFrontend/previewimageprovider.cpp	Sun Sep 28 00:18:01 2014 +0400
@@ -18,22 +18,19 @@
 
 void PreviewImageProvider::setPixmap(const QByteArray &px)
 {
-    if(px.size() == 128 * 256)
-    {
-        QVector<QRgb> colorTable;
-        colorTable.resize(256);
-        for(int i = 0; i < 256; ++i)
-            colorTable[i] = qRgba(255, 255, 0, i);
+    QVector<QRgb> colorTable;
+    colorTable.resize(256);
+    for(int i = 0; i < 256; ++i)
+        colorTable[i] = qRgba(255, 255, 0, i);
 
-        const quint8 *buf = (const quint8*) px.constData();
-        QImage im(buf, 256, 128, QImage::Format_Indexed8);
-        im.setColorTable(colorTable);
+    const quint8 *buf = (const quint8*) px.constData();
+    QImage im(buf, 256, 128, QImage::Format_Indexed8);
+    im.setColorTable(colorTable);
 
-        m_px = QPixmap::fromImage(im, Qt::ColorOnly);
-        //QPixmap pxres(px.size());
-        //QPainter p(&pxres);
+    m_px = QPixmap::fromImage(im, Qt::ColorOnly);
+    //QPixmap pxres(px.size());
+    //QPainter p(&pxres);
 
-        //p.fillRect(pxres.rect(), linearGrad);
-        //p.drawPixmap(0, 0, px);
-    }
+    //p.fillRect(pxres.rect(), linearGrad);
+    //p.drawPixmap(0, 0, px);
 }