Skip to content

Commit b19dd92

Browse files
committedAug 22, 2014
Add ^[mask texture modifier
1 parent e4d570e commit b19dd92

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
 

‎doc/lua_api.txt

+4
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ Advanced texture modifiers:
258258
Crops the texture to a frame of a vertical animation.
259259
Example: default_torch_animated.png^[verticalframe:16:8
260260

261+
[mask:<file>
262+
Apply a mask to the base image.
263+
The mask is applied using binary AND.
264+
261265
Sounds
262266
-------
263267
Only OGG Vorbis files are supported.

‎src/tile.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ static void blit_with_alpha(video::IImage *src, video::IImage *dst,
537537
static void blit_with_alpha_overlay(video::IImage *src, video::IImage *dst,
538538
v2s32 src_pos, v2s32 dst_pos, v2u32 size);
539539

540+
// Apply a mask to an image
541+
static void apply_mask(video::IImage *mask, video::IImage *dst,
542+
v2s32 mask_pos, v2s32 dst_pos, v2u32 size);
543+
540544
// Draw or overlay a crack
541545
static void draw_crack(video::IImage *crack, video::IImage *dst,
542546
bool use_overlay, s32 frame_count, s32 progression,
@@ -1557,6 +1561,31 @@ bool TextureSource::generateImagePart(std::string part_of_name,
15571561
baseimg->drop();
15581562
baseimg = img;
15591563
}
1564+
/*
1565+
[mask:filename
1566+
Applies a mask to an image
1567+
*/
1568+
else if(part_of_name.substr(0,6) == "[mask:")
1569+
{
1570+
if (baseimg == NULL) {
1571+
errorstream << "generateImage(): baseimg == NULL "
1572+
<< "for part_of_name=\"" << part_of_name
1573+
<< "\", cancelling." << std::endl;
1574+
return false;
1575+
}
1576+
Strfnd sf(part_of_name);
1577+
sf.next(":");
1578+
std::string filename = sf.next(":");
1579+
1580+
video::IImage *img = m_sourcecache.getOrLoad(filename, m_device);
1581+
if (img) {
1582+
apply_mask(img, baseimg, v2s32(0, 0), v2s32(0, 0),
1583+
img->getDimension());
1584+
} else {
1585+
errorstream << "generateImage(): Failed to load \""
1586+
<< filename << "\".";
1587+
}
1588+
}
15601589
else
15611590
{
15621591
errorstream<<"generateImagePart(): Invalid "
@@ -1615,6 +1644,26 @@ static void blit_with_alpha_overlay(video::IImage *src, video::IImage *dst,
16151644
}
16161645
}
16171646

1647+
/*
1648+
Apply mask to destination
1649+
*/
1650+
static void apply_mask(video::IImage *mask, video::IImage *dst,
1651+
v2s32 mask_pos, v2s32 dst_pos, v2u32 size)
1652+
{
1653+
for(u32 y0 = 0; y0 < size.Y; y0++) {
1654+
for(u32 x0 = 0; x0 < size.X; x0++) {
1655+
s32 mask_x = x0 + mask_pos.X;
1656+
s32 mask_y = y0 + mask_pos.Y;
1657+
s32 dst_x = x0 + dst_pos.X;
1658+
s32 dst_y = y0 + dst_pos.Y;
1659+
video::SColor mask_c = mask->getPixel(mask_x, mask_y);
1660+
video::SColor dst_c = dst->getPixel(dst_x, dst_y);
1661+
dst_c.color &= mask_c.color;
1662+
dst->setPixel(dst_x, dst_y, dst_c);
1663+
}
1664+
}
1665+
}
1666+
16181667
static void draw_crack(video::IImage *crack, video::IImage *dst,
16191668
bool use_overlay, s32 frame_count, s32 progression,
16201669
video::IVideoDriver *driver)

0 commit comments

Comments
 (0)
Please sign in to comment.