Skip to content

Commit

Permalink
Add an additional abstraction between pixman_op_t and the Blend Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Sep 1, 2021
1 parent fa78227 commit 7e5872f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 43 deletions.
59 changes: 46 additions & 13 deletions src/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ namespace {
}
} // anonymous namespace

void Bitmap::Blit(int x, int y, Bitmap const& src, Rect const& src_rect, Opacity const& opacity, int blend_mode) {
void Bitmap::Blit(int x, int y, Bitmap const& src, Rect const& src_rect, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
Expand Down Expand Up @@ -605,11 +605,11 @@ PixmanImagePtr Bitmap::GetSubimage(Bitmap const& src, const Rect& src_rect) {
(uint32_t*) pixels, src.pitch()) };
}

void Bitmap::TiledBlit(Rect const& src_rect, Bitmap const& src, Rect const& dst_rect, Opacity const& opacity, int blend_mode) {
void Bitmap::TiledBlit(Rect const& src_rect, Bitmap const& src, Rect const& dst_rect, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
TiledBlit(0, 0, src_rect, src, dst_rect, opacity, blend_mode);
}

void Bitmap::TiledBlit(int ox, int oy, Rect const& src_rect, Bitmap const& src, Rect const& dst_rect, Opacity const& opacity, int blend_mode) {
void Bitmap::TiledBlit(int ox, int oy, Rect const& src_rect, Bitmap const& src, Rect const& dst_rect, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
Expand All @@ -633,11 +633,11 @@ void Bitmap::TiledBlit(int ox, int oy, Rect const& src_rect, Bitmap const& src,
dst_rect.width, dst_rect.height);
}

void Bitmap::StretchBlit(Bitmap const& src, Rect const& src_rect, Opacity const& opacity, int blend_mode) {
void Bitmap::StretchBlit(Bitmap const& src, Rect const& src_rect, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
StretchBlit(GetRect(), src, src_rect, opacity, blend_mode);
}

void Bitmap::StretchBlit(Rect const& dst_rect, Bitmap const& src, Rect const& src_rect, Opacity const& opacity, int blend_mode) {
void Bitmap::StretchBlit(Rect const& dst_rect, Bitmap const& src, Rect const& src_rect, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
Expand All @@ -661,7 +661,7 @@ void Bitmap::StretchBlit(Rect const& dst_rect, Bitmap const& src, Rect const& sr
pixman_image_set_transform(src.bitmap.get(), nullptr);
}

void Bitmap::WaverBlit(int x, int y, double zoom_x, double zoom_y, Bitmap const& src, Rect const& src_rect, int depth, double phase, Opacity const& opacity, int blend_mode) {
void Bitmap::WaverBlit(int x, int y, double zoom_x, double zoom_y, Bitmap const& src, Rect const& src_rect, int depth, double phase, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
Expand Down Expand Up @@ -946,7 +946,7 @@ void Bitmap::BlendBlit(int x, int y, Bitmap const& src, Rect const& src_rect, co
src_rect.width, src_rect.height);
}

void Bitmap::FlipBlit(int x, int y, Bitmap const& src, Rect const& src_rect, bool horizontal, bool vertical, Opacity const& opacity, int blend_mode) {
void Bitmap::FlipBlit(int x, int y, Bitmap const& src, Rect const& src_rect, bool horizontal, bool vertical, Opacity const& opacity, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
Expand Down Expand Up @@ -1043,7 +1043,7 @@ void Bitmap::EffectsBlit(int x, int y, int ox, int oy,
Bitmap const& src, Rect const& src_rect,
Opacity const& opacity,
double zoom_x, double zoom_y, double angle,
int waver_depth, double waver_phase, int blend_mode) {
int waver_depth, double waver_phase, Bitmap::BlendMode blend_mode) {
if (opacity.IsTransparent()) {
return;
}
Expand All @@ -1069,7 +1069,7 @@ void Bitmap::EffectsBlit(int x, int y, int ox, int oy,

void Bitmap::RotateZoomOpacityBlit(int x, int y, int ox, int oy,
Bitmap const& src, Rect const& src_rect,
double angle, double zoom_x, double zoom_y, Opacity const& opacity, int blend_mode)
double angle, double zoom_x, double zoom_y, Opacity const& opacity, Bitmap::BlendMode blend_mode)
{
if (opacity.IsTransparent()) {
return;
Expand Down Expand Up @@ -1114,7 +1114,7 @@ void Bitmap::RotateZoomOpacityBlit(int x, int y, int ox, int oy,
void Bitmap::ZoomOpacityBlit(int x, int y, int ox, int oy,
Bitmap const& src, Rect const& src_rect,
double zoom_x, double zoom_y,
Opacity const& opacity, int blend_mode)
Opacity const& opacity, Bitmap::BlendMode blend_mode)
{
if (opacity.IsTransparent()) {
return;
Expand All @@ -1128,9 +1128,42 @@ void Bitmap::ZoomOpacityBlit(int x, int y, int ox, int oy,
StretchBlit(dst_rect, src, src_rect, opacity, blend_mode);
}

pixman_op_t Bitmap::GetOperator(pixman_image_t* mask, int blend_mode) const {
if (blend_mode >= 0) {
return (pixman_op_t)blend_mode;
pixman_op_t Bitmap::GetOperator(pixman_image_t* mask, Bitmap::BlendMode blend_mode) const {
if (blend_mode != BlendMode::Default) {
switch (blend_mode) {
case BlendMode::Normal:
return PIXMAN_OP_OVER;
case BlendMode::NormalWithoutAlpha:
return PIXMAN_OP_SRC;
case BlendMode::XOR:
return PIXMAN_OP_XOR;
case BlendMode::Additive:
return PIXMAN_OP_ADD;
case BlendMode::Multiply:
return PIXMAN_OP_MULTIPLY;
case BlendMode::Overlay:
return PIXMAN_OP_OVERLAY;
case BlendMode::Saturate:
return PIXMAN_OP_SATURATE;
case BlendMode::Darken:
return PIXMAN_OP_DARKEN;
case BlendMode::Lighten:
return PIXMAN_OP_LIGHTEN;
case BlendMode::ColorDodge:
return PIXMAN_OP_COLOR_DODGE;
case BlendMode::ColorBurn:
return PIXMAN_OP_COLOR_BURN;
case BlendMode::Difference:
return PIXMAN_OP_DIFFERENCE;
case BlendMode::Exclusion:
return PIXMAN_OP_EXCLUSION;
case BlendMode::SoftLight:
return PIXMAN_OP_SOFT_LIGHT;
case BlendMode::HardLight:
return PIXMAN_OP_HARD_LIGHT;
default:
return PIXMAN_OP_CLEAR;
}
}

if (!mask && (!GetTransparent() || GetImageOpacity() == ImageOpacity::Opaque)) {
Expand Down
60 changes: 39 additions & 21 deletions src/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ class Bitmap {
Flag_ReadOnly = 1 << 16
};

enum class BlendMode {
Default, // SRC or OVER depending on the image
Normal, // OP_OVER
NormalWithoutAlpha, // OP_SRC
XOR,
Additive,
Multiply,
Overlay,
Saturate,
Darken,
Lighten,
ColorDodge,
ColorBurn,
Difference,
Exclusion,
SoftLight,
HardLight
};

/**
* Provides opacity information about the image.
Expand Down Expand Up @@ -248,10 +266,10 @@ class Bitmap {
* @param src source bitmap.
* @param src_rect source bitmap rect.
* @param opacity opacity for blending with bitmap.
* @param blend_mode Blend mode to use. default: OP_SRC/OP_OVER; depends on the image
* @param blend_mode Blend mode to use.
*/
void Blit(int x, int y, Bitmap const& src, Rect const& src_rect,
Opacity const& opacity, int blend_mode = -1);
Opacity const& opacity, BlendMode blend_mode = BlendMode::Default);

/**
* Blits source bitmap to this one ignoring alpha (faster)
Expand All @@ -272,10 +290,10 @@ class Bitmap {
* @param src source bitmap.
* @param dst_rect destination rect.
* @param opacity opacity for blending with bitmap.
* @param blend_mode Blend mode to use. default: OP_SRC/OP_OVER; depends on the image
* @param blend_mode Blend mode to use.
*/
void TiledBlit(Rect const& src_rect, Bitmap const& src, Rect const& dst_rect,
Opacity const& opacity, int blend_mode = -1);
Opacity const& opacity, BlendMode blend_mode = BlendMode::Default);

/**
* Blits source bitmap in tiles to this one.
Expand All @@ -286,10 +304,10 @@ class Bitmap {
* @param src source bitmap.
* @param dst_rect destination rect.
* @param opacity opacity for blending with bitmap.
* @param blend_mode Blend mode to use. default: OP_SRC/OP_OVER; depends on the image
* @param blend_mode Blend mode to use.
*/
void TiledBlit(int ox, int oy, Rect const& src_rect, Bitmap const& src, Rect const& dst_rect,
Opacity const& opacity, int blend_mode = -1);
Opacity const& opacity, BlendMode blend_mode = BlendMode::Default);

/**
* Blits source bitmap to this one, making clones across the edges if src crossed a boundary of this.
Expand All @@ -310,10 +328,10 @@ class Bitmap {
* @param src source bitmap.
* @param src_rect source bitmap rect.
* @param opacity opacity for blending with bitmap.
* @param blend_mode Blend mode to use. default: OP_SRC/OP_OVER; depends on the image
* @param blend_mode Blend mode to use.
*/
void StretchBlit(Bitmap const& src, Rect const& src_rect,
Opacity const& opacity, int blend_mode = -1);
Opacity const& opacity, BlendMode blend_mode = BlendMode::Default);

/**
* Blits source bitmap stretched to this one.
Expand All @@ -322,10 +340,10 @@ class Bitmap {
* @param src source bitmap.
* @param src_rect source bitmap rect.
* @param opacity opacity for blending with bitmap.
* @param blend_mode Blend mode to use. default: OP_SRC/OP_OVER; depends on the image
* @param blend_mode Blend mode to use.
*/
void StretchBlit(Rect const& dst_rect, Bitmap const& src, Rect const& src_rect,
Opacity const& opacity, int blend_mode = -1);
Opacity const& opacity, BlendMode blend_mode = BlendMode::Default);

/**
* Blit source bitmap flipped.
Expand All @@ -337,10 +355,10 @@ class Bitmap {
* @param horizontal flip horizontally.
* @param vertical flip vertically.
* @param opacity opacity to apply.
* @param blend_mode Blend mode to use. default: OP_SRC/OP_OVER; depends on the image
* @param blend_mode Blend mode to use.
*/
void FlipBlit(int x, int y, Bitmap const& src, Rect const& src_rect, bool horizontal, bool vertical,
Opacity const& opacity, int blend_mode = -1);
Opacity const& opacity, BlendMode blend_mode = BlendMode::Default);

/**
* Blits source bitmap with waver, zoom, and opacity effects.
Expand All @@ -354,10 +372,10 @@ class Bitmap {
* @param depth wave magnitude.
* @param phase wave phase.
* @param opacity opacity.
* @param blend_mode Blend mode to use. default: OP_SRC/OP_OVER; depends on the image
* @param blend_mode Blend mode to use.
*/
void WaverBlit(int x, int y, double zoom_x, double zoom_y, Bitmap const& src, Rect const& src_rect, int depth, double phase,
Opacity const& opacity, int blend_mode = -1);
Opacity const& opacity, BlendMode blend_mode = BlendMode::Default);

/**
* Blits source bitmap with rotation, zoom, and opacity effects.
Expand All @@ -372,12 +390,12 @@ class Bitmap {
* @param zoom_x x scale factor.
* @param zoom_y y scale factor.
* @param opacity opacity.
* @param blend_mode Blend mode to use. default: OP_OVER
* @param blend_mode Blend mode to use.
*/
void RotateZoomOpacityBlit(int x, int y, int ox, int oy,
Bitmap const& src, Rect const& src_rect,
double angle, double zoom_x, double zoom_y,
Opacity const& opacity, int blend_mode = PIXMAN_OP_OVER);
Opacity const& opacity, BlendMode blend_mode = BlendMode::Normal);

/**
* Blits source bitmap with zoom and opacity scaling.
Expand All @@ -391,12 +409,12 @@ class Bitmap {
* @param zoom_x x scale factor.
* @param zoom_y y scale factor.
* @param opacity opacity.
* @param blend_mode Blend mode to use. default: OP_SRC/OP_OVER; depends on the image
* @param blend_mode Blend mode to use.
*/
void ZoomOpacityBlit(int x, int y, int ox, int oy,
Bitmap const& src, Rect const& src_rect,
double zoom_x, double zoom_y,
Opacity const& opacity, int blend_mode = -1);
Opacity const& opacity, BlendMode blend_mode = BlendMode::Default);

/**
* Fills entire bitmap with color.
Expand Down Expand Up @@ -526,14 +544,14 @@ class Bitmap {
* @param angle rotation angle.
* @param waver_depth wave magnitude.
* @param waver_phase wave phase.
* @param blend_mode Blend mode to use. default: OP_SRC/OP_OVER; depends on the image
* @param blend_mode Blend mode to use.
*/
void EffectsBlit(int x, int y, int ox, int oy,
Bitmap const& src, Rect const& src_rect,
Opacity const& opacity,
double zoom_x, double zoom_y, double angle,
int waver_depth, double waver_phase,
int blend_mode = -1);
BlendMode blend_mode = BlendMode::Default);

static DynamicFormat ChooseFormat(const DynamicFormat& format);
static void SetFormat(const DynamicFormat& format);
Expand Down Expand Up @@ -593,7 +611,7 @@ class Bitmap {
* @param blend_mode When >= 0: Force this blend mode as operator
* @return blend mode
*/
pixman_op_t GetOperator(pixman_image_t* mask = nullptr, int blend_mode = -1) const;
pixman_op_t GetOperator(pixman_image_t* mask = nullptr, BlendMode blend_mode = BlendMode::Default) const;
bool read_only = false;
};

Expand Down
12 changes: 6 additions & 6 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2616,11 +2616,11 @@ bool Game_Interpreter::CommandShowPicture(lcf::rpg::EventCommand const& com) { /
int flags = com.parameters[14] >> 8;
int blend_mode = flags & 3;
if (blend_mode == 1) {
params.blend_mode = PIXMAN_OP_MULTIPLY;
params.blend_mode = (int)Bitmap::BlendMode::Multiply;
} else if (blend_mode == 2) {
params.blend_mode = PIXMAN_OP_ADD;
params.blend_mode = (int)Bitmap::BlendMode::Additive;
} else if (blend_mode == 3) {
params.blend_mode = PIXMAN_OP_OVERLAY;
params.blend_mode = (int)Bitmap::BlendMode::Overlay;
}
params.flip_horizontal = (flags & 16) == 16;
params.flip_vertical = (flags & 32) == 32;
Expand Down Expand Up @@ -2690,11 +2690,11 @@ bool Game_Interpreter::CommandMovePicture(lcf::rpg::EventCommand const& com) { /
int flags = com.parameters[16] >> 8;
int blend_mode = flags & 3;
if (blend_mode == 1) {
params.blend_mode = PIXMAN_OP_MULTIPLY;
params.blend_mode = (int)Bitmap::BlendMode::Multiply;
} else if (blend_mode == 2) {
params.blend_mode = PIXMAN_OP_ADD;
params.blend_mode = (int)Bitmap::BlendMode::Additive;
} else if (blend_mode == 3) {
params.blend_mode = PIXMAN_OP_OVERLAY;
params.blend_mode = (int)Bitmap::BlendMode::Overlay;
}
params.flip_horizontal = (flags & 16) == 16;
params.flip_vertical = (flags & 32) == 32;
Expand Down
2 changes: 1 addition & 1 deletion src/game_pictures.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Game_Pictures {
// Extensions
bool flip_horizontal = false;
bool flip_vertical = false;
int blend_mode = -1;
int blend_mode = 0;
};
struct ShowParams : Params {
std::string name;
Expand Down
2 changes: 1 addition & 1 deletion src/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void Sprite::BlitScreenIntern(Bitmap& dst, Bitmap const& draw_bitmap, Rect const
dst.EffectsBlit(x, y, ox, oy, draw_bitmap, src_rect,
Opacity(opacity_top_effect, opacity_bottom_effect, bush_effect),
zoom_x, zoom_y, angle_effect,
waver_effect_depth, waver_effect_phase, blend_type_effect);
waver_effect_depth, waver_effect_phase, static_cast<Bitmap::BlendMode>(blend_type_effect));
}

BitmapRef Sprite::Refresh(Rect& rect) {
Expand Down
2 changes: 1 addition & 1 deletion src/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Sprite : public Drawable {
double zoom_x_effect = 1.0;
double zoom_y_effect = 1.0;
double angle_effect = 0.0;
int blend_type_effect = -1;
int blend_type_effect = 0;
Color blend_color_effect;
int waver_effect_depth = 0;
double waver_effect_phase = 0.0;
Expand Down

0 comments on commit 7e5872f

Please sign in to comment.