Add an [opacity:<r> texture modifier. Makes the base image transparent according...
authorThomas--S <Thomas--S@users.noreply.github.com>
Sat, 2 Jul 2016 15:58:08 +0000 (17:58 +0200)
committerLoic Blot <loic.blot@unix-experience.fr>
Fri, 12 Aug 2016 13:20:30 +0000 (15:20 +0200)
doc/lua_api.txt
src/client/tile.cpp

index ce40e082c92c1a6ba7546207dc382d74e7f8ff10..182f5c821c2b84e4d9e915b8dc1586650ea45db9 100644 (file)
@@ -292,6 +292,16 @@ Example:
 
     default_sandstone.png^[resize:16x16
 
+#### `[opacity:<r>`
+    Makes the base image transparent according to the given ratio.
+    r must be between 0 and 255.
+    0 means totally transparent.
+    255 means totally opaque.
+
+Example:
+
+    default_sandstone.png^[opacity:127
+
 #### `[brighten`
 Brightens the texture.
 
index ec8c95f02f5fc52d8772fbdee2cdda98cad7cae7..3b5d2a3ae4cd7d4fd853f711365ed093ef5fe0cd 100644 (file)
@@ -1735,6 +1735,36 @@ bool TextureSource::generateImagePart(std::string part_of_name,
                        baseimg->drop();
                        baseimg = image;
                }
+               /*
+                       [opacity:R
+                       Makes the base image transparent according to the given ratio.
+                       R must be between 0 and 255.
+                       0 means totally transparent.
+                       255 means totally opaque.
+               */
+               else if (str_starts_with(part_of_name, "[opacity:")) {
+                       if (baseimg == NULL) {
+                               errorstream << "generateImagePart(): baseimg == NULL "
+                                               << "for part_of_name=\"" << part_of_name
+                                               << "\", cancelling." << std::endl;
+                               return false;
+                       }
+
+                       Strfnd sf(part_of_name);
+                       sf.next(":");
+
+                       u32 ratio = mystoi(sf.next(""), 0, 255);
+
+                       core::dimension2d<u32> dim = baseimg->getDimension();
+
+                       for (u32 y = 0; y < dim.Height; y++)
+                       for (u32 x = 0; x < dim.Width; x++)
+                       {
+                               video::SColor c = baseimg->getPixel(x,y);
+                               c.setAlpha(floor((c.getAlpha() * ratio) / 255 + 0.5));
+                               baseimg->setPixel(x,y,c);
+                       }
+               }
                else
                {
                        errorstream << "generateImagePart(): Invalid "