Add alpha channel to preview
authorunc0rr
Wed, 26 Feb 2014 23:43:42 +0400
changeset 10162 38dbf26475d8
parent 10161 c092f92aee23
child 10163 b994afa40326
Add alpha channel to preview
QTfrontend/net/hwmap.cpp
hedgewars/hwengine.pas
hedgewars/uLand.pas
hedgewars/uTypes.pas
--- a/QTfrontend/net/hwmap.cpp	Wed Feb 26 23:07:55 2014 +0400
+++ b/QTfrontend/net/hwmap.cpp	Wed Feb 26 23:43:42 2014 +0400
@@ -86,6 +86,26 @@
 
         emit HHLimitReceived(buf[128 * 32]);
         emit ImageReceived(px);
+    } else if (readbuffer.size() == 128 * 256 + 1)
+    {
+        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*) readbuffer.constData();
+        QImage im(buf, 256, 128, QImage::Format_Indexed8);
+        im.setColorTable(colorTable);
+
+        QPixmap px = QPixmap::fromImage(im, Qt::ColorOnly);
+        QPixmap pxres(px.size());
+        QPainter p(&pxres);
+
+        p.fillRect(pxres.rect(), linearGrad);
+        p.drawPixmap(0, 0, px);
+
+        emit HHLimitReceived(buf[128 * 256]);
+        emit ImageReceived(px);
     }
 }
 
--- a/hedgewars/hwengine.pas	Wed Feb 26 23:07:55 2014 +0400
+++ b/hedgewars/hwengine.pas	Wed Feb 26 23:43:42 2014 +0400
@@ -531,7 +531,7 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
-var Preview: TPreview;
+var Preview: TPreviewAlpha;
 begin
     initEverything(false);
 {$IFDEF HWLIBRARY}
@@ -544,7 +544,7 @@
     TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
 
     ScriptOnPreviewInit;
-    GenPreview(Preview);
+    GenPreviewAlpha(Preview);
     WriteLnToConsole('Sending preview...');
     SendIPCRaw(@Preview, sizeof(Preview));
     SendIPCRaw(@MaxHedgehogs, sizeof(byte));
--- a/hedgewars/uLand.pas	Wed Feb 26 23:07:55 2014 +0400
+++ b/hedgewars/uLand.pas	Wed Feb 26 23:43:42 2014 +0400
@@ -27,6 +27,7 @@
 procedure DrawBottomBorder;
 procedure GenMap;
 procedure GenPreview(out Preview: TPreview);
+procedure GenPreviewAlpha(out Preview: TPreviewAlpha);
 
 implementation
 uses uConsole, uStore, uRandom, uLandObjects, uIO, uLandTexture, SysUtils,
@@ -869,6 +870,49 @@
 end;
 
 
+procedure GenPreviewAlpha(out Preview: TPreviewAlpha);
+var rh, rw, ox, oy, x, y, xx, yy, t, lh, lw: LongInt;
+begin
+    WriteLnToConsole('Generating preview...');
+    case cMapGen of
+        0: GenBlank(EdgeTemplates[SelectTemplate]);
+        1: begin ResizeLand(4096,2048); GenMaze; end;
+        2: GenDrawnMap;
+    else
+        OutError('Unknown mapgen', true);
+    end;
+
+    // strict scaling needed here since preview assumes a rectangle
+    rh:= max(LAND_HEIGHT, 2048);
+    rw:= max(LAND_WIDTH, 4096);
+    ox:= 0;
+    if rw < rh*2 then
+        begin
+        rw:= rh*2;
+        end;
+    if rh < rw div 2 then rh:= rw * 2;
+
+    ox:= (rw-LAND_WIDTH) div 2;
+    oy:= rh-LAND_HEIGHT;
+
+    lh:= rh div 128;
+    lw:= rw div 256;
+    for y:= 0 to 127 do
+        for x:= 0 to 255 do
+            begin
+            t:= 0;
+
+            for yy:= y * lh - oy to y * lh + 7 - oy do
+                for xx:= x * lw - ox to x * lw + 7 - ox do
+                    if (yy and LAND_HEIGHT_MASK = 0) and (xx and LAND_WIDTH_MASK = 0)
+                        and (Land[yy, xx] <> 0) then
+                        inc(t);
+
+            Preview[y, x]:= t * 1023 div 256;
+            if t > 8 then Preview[y, x]:= 255 else Preview[y, x]:= 0
+            end;
+end;
+
 procedure chLandCheck(var s: shortstring);
 begin
     AddFileLog('CheckLandDigest: ' + s + ' digest : ' + digest);
--- a/hedgewars/uTypes.pas	Wed Feb 26 23:07:55 2014 +0400
+++ b/hedgewars/uTypes.pas	Wed Feb 26 23:43:42 2014 +0400
@@ -466,6 +466,7 @@
     TDirtyTag = packed array of array of byte;
 
     TPreview  = packed array[0..127, 0..31] of byte;
+    TPreviewAlpha  = packed array[0..127, 0..255] of byte;
 
     PWidgetMovement = ^TWidgetMovement;
     TWidgetMovement = record