Skip to content

Commit f21dae6

Browse files
Thomas--Snerzhul
authored andcommittedAug 12, 2016
Add an [opacity:<r> texture modifier. 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. Useful for texture overlaying.
1 parent c4e77b4 commit f21dae6

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
 

‎doc/lua_api.txt

+10
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ Example:
292292

293293
default_sandstone.png^[resize:16x16
294294

295+
#### `[opacity:<r>`
296+
Makes the base image transparent according to the given ratio.
297+
r must be between 0 and 255.
298+
0 means totally transparent.
299+
255 means totally opaque.
300+
301+
Example:
302+
303+
default_sandstone.png^[opacity:127
304+
295305
#### `[brighten`
296306
Brightens the texture.
297307

‎src/client/tile.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,36 @@ bool TextureSource::generateImagePart(std::string part_of_name,
17351735
baseimg->drop();
17361736
baseimg = image;
17371737
}
1738+
/*
1739+
[opacity:R
1740+
Makes the base image transparent according to the given ratio.
1741+
R must be between 0 and 255.
1742+
0 means totally transparent.
1743+
255 means totally opaque.
1744+
*/
1745+
else if (str_starts_with(part_of_name, "[opacity:")) {
1746+
if (baseimg == NULL) {
1747+
errorstream << "generateImagePart(): baseimg == NULL "
1748+
<< "for part_of_name=\"" << part_of_name
1749+
<< "\", cancelling." << std::endl;
1750+
return false;
1751+
}
1752+
1753+
Strfnd sf(part_of_name);
1754+
sf.next(":");
1755+
1756+
u32 ratio = mystoi(sf.next(""), 0, 255);
1757+
1758+
core::dimension2d<u32> dim = baseimg->getDimension();
1759+
1760+
for (u32 y = 0; y < dim.Height; y++)
1761+
for (u32 x = 0; x < dim.Width; x++)
1762+
{
1763+
video::SColor c = baseimg->getPixel(x,y);
1764+
c.setAlpha(floor((c.getAlpha() * ratio) / 255 + 0.5));
1765+
baseimg->setPixel(x,y,c);
1766+
}
1767+
}
17381768
else
17391769
{
17401770
errorstream << "generateImagePart(): Invalid "

0 commit comments

Comments
 (0)