Skip to content

Commit

Permalink
Support specifying the Blend Mode as part of the filename
Browse files Browse the repository at this point in the history
This is supported e.g. by a DynRPG plugin but we are not feature compatible to this.

Works for everything that is processed as a Sprite which is almost everything except scrolling backgrounds (e.g. Panorama) and the Tilemap.

Fix EasyRPG#2628
  • Loading branch information
Ghabry committed Sep 1, 2021
1 parent 7e5872f commit 38ed088
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Bitmap::Bitmap(Filesystem_Stream::InputStream stream, bool transparent, uint32_t
ConvertImage(w, h, pixels, transparent);

CheckPixels(flags);

filename = ToString(stream.GetName());
}

Bitmap::Bitmap(const uint8_t* data, unsigned bytes, bool transparent, uint32_t flags) {
Expand Down
14 changes: 14 additions & 0 deletions src/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ class Bitmap {
*/
Color GetShadowColor() const;

/**
* Gets the filename this bitmap was loaded from.
* This will be empty when the origin was not a file.
*
* @return filename
*/
StringView GetFilename() const;

void CheckPixels(uint32_t flags);

/**
Expand Down Expand Up @@ -578,6 +586,8 @@ class Bitmap {
TileOpacity tile_opacity;
Color bg_color, sh_color;

std::string filename;

/** Bitmap data. */
PixmanImagePtr bitmap;
pixman_format_code_t pixman_format;
Expand Down Expand Up @@ -647,4 +657,8 @@ inline bool Bitmap::GetTransparent() const {
return format.alpha_type != PF::NoAlpha;
}

inline StringView Bitmap::GetFilename() const {
return filename;
}

#endif
32 changes: 32 additions & 0 deletions src/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,43 @@ BitmapRef Sprite::Refresh(Rect& rect) {
}

void Sprite::SetBitmap(BitmapRef const& nbitmap) {
if (nbitmap == bitmap) {
return;
}

bitmap = nbitmap;
if (!bitmap) {
src_rect = Rect();
} else {
src_rect = bitmap->GetRect();

// EasyRPG Extension: Set the Blend Mode based on the filename, e.g. ".add.png" for "Additive"
SetBlendType(0);
StringView filename = bitmap->GetFilename();
if (filename.size() >= 8 && filename[filename.size() - 4] == '.' && filename[filename.size() - 8] == '.') {
filename = filename.substr(filename.size() - 7, 3);
constexpr std::array<std::pair<StringView, Bitmap::BlendMode>, 13> exts = {{
{"add", Bitmap::BlendMode::Additive},
{"clb", Bitmap::BlendMode::ColorBurn},
{"cld", Bitmap::BlendMode::ColorDodge},
{"dif", Bitmap::BlendMode::Difference},
{"drk", Bitmap::BlendMode::Darken},
{"exc", Bitmap::BlendMode::Exclusion},
{"hlg", Bitmap::BlendMode::HardLight},
{"lgh", Bitmap::BlendMode::Lighten},
{"mul", Bitmap::BlendMode::Multiply},
{"ove", Bitmap::BlendMode::Overlay},
{"sat", Bitmap::BlendMode::Saturate},
{"slg", Bitmap::BlendMode::SoftLight},
{"xor", Bitmap::BlendMode::XOR}
}};
auto it = std::lower_bound(exts.begin(), exts.end(), filename, [](const auto& e, const auto& fname) {
return e.first < fname;
});
if (it != exts.end() && it->first == filename) {
SetBlendType((int)it->second);
}
}
}

src_rect_effect = src_rect;
Expand Down
4 changes: 3 additions & 1 deletion src/sprite_picture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ void Sprite_Picture::Draw(Bitmap& dst) {

SetFlipX(data.easyrpg_flip_horizontal);
SetFlipY(data.easyrpg_flip_vertical);
SetBlendType(data.easyrpg_blend_mode);
if (static_cast<Bitmap::BlendMode>(data.easyrpg_blend_mode) != Bitmap::BlendMode::Default) {
SetBlendType(data.easyrpg_blend_mode);
}

Sprite::Draw(dst);
}

0 comments on commit 38ed088

Please sign in to comment.