Skip to content

Commit

Permalink
Refactor Opacity
Browse files Browse the repository at this point in the history
* Move to header
* Make constexpr
  • Loading branch information
mateofio committed Dec 22, 2019
1 parent 7466e8d commit 5e6f315
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 90 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ add_library(${PROJECT_NAME}
src/message_overlay.h
src/meta.cpp
src/meta.h
src/opacity.h
src/options.h
src/output.cpp
src/output.h
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ libeasyrpg_player_a_SOURCES = \
src/message_overlay.h \
src/meta.cpp \
src/meta.h \
src/opacity.h \
src/options.h \
src/output.cpp \
src/output.h \
Expand Down
31 changes: 19 additions & 12 deletions bench/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
#include <pixel_format.h>
#include <transform.h>

constexpr auto opacity_opaque = Opacity::Opaque();
constexpr auto opacity_0 = Opacity(0);
constexpr auto opacity_50 = Opacity(128);
constexpr auto opacity_75_25 = Opacity(192, 64, 1);

auto opacity = opacity_opaque;

struct BitmapAccess : public Bitmap {
static pixman_format_code_t find_format(const DynamicFormat& format) {
return Bitmap::find_format(format);
Expand Down Expand Up @@ -48,7 +55,7 @@ static void BM_Blit(benchmark::State& state) {
auto src = Bitmap::Create(320, 240);
auto rect = src->GetRect();
for (auto _: state) {
dest->Blit(0, 0, *src, rect, Opacity::opaque);
dest->Blit(0, 0, *src, rect, opacity);
}
}

Expand All @@ -60,7 +67,7 @@ static void BM_BlitFast(benchmark::State& state) {
auto src = Bitmap::Create(320, 240);
auto rect = src->GetRect();
for (auto _: state) {
dest->BlitFast(0, 0, *src, rect, Opacity::opaque);
dest->BlitFast(0, 0, *src, rect, opacity);
}
}

Expand All @@ -72,7 +79,7 @@ static void BM_TiledBlit(benchmark::State& state) {
auto src = Bitmap::Create(320, 240);
auto rect = src->GetRect();
for (auto _: state) {
dest->TiledBlit(rect, *src, rect, Opacity::opaque);
dest->TiledBlit(rect, *src, rect, opacity);
}
}

Expand All @@ -84,7 +91,7 @@ static void BM_TiledBlitOffset(benchmark::State& state) {
auto src = Bitmap::Create(320, 240);
auto rect = src->GetRect();
for (auto _: state) {
dest->TiledBlit(0, 0, rect, *src, rect, Opacity::opaque);
dest->TiledBlit(0, 0, rect, *src, rect, opacity);
}
}

Expand All @@ -96,7 +103,7 @@ static void BM_StretchBlit(benchmark::State& state) {
auto src = Bitmap::Create(20, 20);
auto rect = src->GetRect();
for (auto _: state) {
dest->StretchBlit(*src, rect, Opacity::opaque);
dest->StretchBlit(*src, rect, opacity);
}
}

Expand All @@ -109,7 +116,7 @@ static void BM_StretchBlitRect(benchmark::State& state) {
auto src = Bitmap::Create(20, 20);
auto rect = src->GetRect();
for (auto _: state) {
dest->StretchBlit(dst_rect, *src, rect, Opacity::opaque);
dest->StretchBlit(dst_rect, *src, rect, opacity);
}
}

Expand All @@ -121,7 +128,7 @@ static void BM_FlipBlit(benchmark::State& state) {
auto src = Bitmap::Create(320, 240);
auto rect = src->GetRect();
for (auto _: state) {
dest->FlipBlit(0, 0, *src, rect, true, true, Opacity::opaque);
dest->FlipBlit(0, 0, *src, rect, true, true, opacity);
}
}

Expand All @@ -135,7 +142,7 @@ static void BM_TransformBlit(benchmark::State& state) {
auto rect = src->GetRect();
auto xform = Transform::Rotation(2 * M_PI);
for (auto _: state) {
dest->TransformBlit(dst_rect, *src, rect, xform, Opacity::opaque);
dest->TransformBlit(dst_rect, *src, rect, xform, opacity);
}
}

Expand All @@ -151,7 +158,7 @@ static void BM_WaverBlit(benchmark::State& state) {
int depth = 2;
double phase = M_PI;
for (auto _: state) {
dest->WaverBlit(0, 0, zoom_x, zoom_y, *src, rect, depth, phase, Opacity::opaque);
dest->WaverBlit(0, 0, zoom_x, zoom_y, *src, rect, depth, phase, opacity);
}
}

Expand Down Expand Up @@ -221,7 +228,7 @@ static void BM_ToneBlit(benchmark::State& state) {
auto rect = src->GetRect();
auto tone = Tone();
for (auto _: state) {
dest->ToneBlit(0, 0, *src, rect, tone, Opacity::opaque, false);
dest->ToneBlit(0, 0, *src, rect, tone, opacity, false);
}
}

Expand All @@ -234,7 +241,7 @@ static void BM_BlendBlit(benchmark::State& state) {
auto rect = src->GetRect();
auto color = Color(255, 255, 255, 255);
for (auto _: state) {
dest->BlendBlit(0, 0, *src, rect, color, Opacity::opaque);
dest->BlendBlit(0, 0, *src, rect, color, opacity);
}
}

Expand Down Expand Up @@ -317,7 +324,7 @@ static void BM_EffectsBlit(benchmark::State& state) {
for (auto _: state) {
dest->EffectsBlit(0, 0, 0, 0,
*src, rect,
Opacity::opaque,
opacity,
zoom_x, zoom_y,
angle,
waver_depth,
Expand Down
2 changes: 1 addition & 1 deletion src/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@ void Background::Draw(Bitmap& dst) {
dst.TiledBlit(-Scale(fg_x), -Scale(fg_y), fg_bitmap->GetRect(), *fg_bitmap, dst_rect, 255);

if (tone_effect != Tone()) {
dst.ToneBlit(0, 0, dst, dst.GetRect(), tone_effect, Opacity::opaque);
dst.ToneBlit(0, 0, dst, dst.GetRect(), tone_effect, Opacity::Opaque());
}
}
14 changes: 6 additions & 8 deletions src/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
#include "util_macro.h"
#include "bitmap_hslrgb.h"

const Opacity Opacity::opaque;

BitmapRef Bitmap::Create(int width, int height, const Color& color) {
BitmapRef surface = Bitmap::Create(width, height, true);
surface->Fill(color);
Expand Down Expand Up @@ -173,7 +171,7 @@ Bitmap::Bitmap(Bitmap const& source, Rect const& src_rect, bool transparent) {

Init(src_rect.width, src_rect.height, (void *) NULL);

Blit(0, 0, source, src_rect, Opacity::opaque);
Blit(0, 0, source, src_rect, Opacity::Opaque());
}

bool Bitmap::WritePNG(std::ostream& os) const {
Expand Down Expand Up @@ -221,7 +219,7 @@ Bitmap::TileOpacity Bitmap::CheckOpacity(const Rect& rect) {
std::vector<uint32_t> pixels;
pixels.resize(rect.width * rect.height);
Bitmap bmp(reinterpret_cast<void*>(&pixels.front()), rect.width, rect.height, rect.width*4, format);
bmp.Blit(0, 0, *this, rect, Opacity::opaque);
bmp.Blit(0, 0, *this, rect, Opacity::Opaque());

for (std::vector<uint32_t>::const_iterator p = pixels.begin(); p != pixels.end(); ++p) {
if ((*p & 0xFF) != 0)
Expand Down Expand Up @@ -304,7 +302,7 @@ void Bitmap::HueChangeBlit(int x, int y, Bitmap const& src, Rect const& src_rect
std::vector<uint32_t> pixels;
pixels.resize(src_rect.width * src_rect.height);
Bitmap bmp(reinterpret_cast<void*>(&pixels.front()), src_rect.width, src_rect.height, src_rect.width * 4, format);
bmp.Blit(0, 0, src, src_rect, Opacity::opaque);
bmp.Blit(0, 0, src, src_rect, Opacity::Opaque());

for (std::vector<uint32_t>::iterator p = pixels.begin(); p != pixels.end(); ++p) {
uint32_t pixel = *p;
Expand All @@ -317,7 +315,7 @@ void Bitmap::HueChangeBlit(int x, int y, Bitmap const& src, Rect const& src_rect
*p = ((uint32_t) r << 24) | ((uint32_t) g << 16) | ((uint32_t) b << 8) | (uint32_t) a;
}

Blit(dst_rect.x, dst_rect.y, bmp, bmp.GetRect(), Opacity::opaque);
Blit(dst_rect.x, dst_rect.y, bmp, bmp.GetRect(), Opacity::Opaque());
}

void Bitmap::TextDraw(Rect const& rect, int color, std::string const& text, Text::Alignment align) {
Expand Down Expand Up @@ -519,7 +517,7 @@ void Bitmap::ConvertImage(int& width, int& height, void*& pixels, bool transpare

Bitmap src(pixels, width, height, 0, img_format);
Clear();
Blit(0, 0, src, src.GetRect(), Opacity::opaque);
Blit(0, 0, src, src.GetRect(), Opacity::Opaque());
free(pixels);
}

Expand Down Expand Up @@ -994,7 +992,7 @@ void Bitmap::Flip(const Rect& dst_rect, bool horizontal, bool vertical) {

BitmapRef resampled(new Bitmap(dst_rect.width, dst_rect.height, GetTransparent()));

resampled->FlipBlit(0, 0, *this, dst_rect, horizontal, vertical, Opacity::opaque);
resampled->FlipBlit(0, 0, *this, dst_rect, horizontal, vertical, Opacity::Opaque());

pixman_image_composite32(GetOperator(),
resampled->bitmap.get(), nullptr, bitmap.get(),
Expand Down
39 changes: 1 addition & 38 deletions src/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,10 @@
#include "tone.h"
#include "text.h"
#include "pixman_image_ptr.h"
#include "opacity.h"

struct Transform;

/**
* Opacity class.
*/
class Opacity {
public:
int top;
int bottom;
int split;

static const Opacity opaque;

Opacity() :
top(255), bottom(255), split(0) {}

Opacity(int opacity) :
top(opacity), bottom(opacity), split(0) {}

Opacity(int top_opacity, int bottom_opacity, int opacity_split) :
top(top_opacity), bottom(bottom_opacity), split(opacity_split) {}

int Value() const {
assert(!IsSplit());
return top;
}

bool IsSplit() const {
return split > 0 && top != bottom;
}

bool IsTransparent() const {
return IsSplit() ? top <= 0 && bottom <= 0 : top <= 0;
}

bool IsOpaque() const {
return IsSplit() ? top >= 255 && bottom >= 255 : top >= 255;
}
};

/**
* Base Bitmap class.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,16 @@ BitmapRef Cache::SpriteEffect(const BitmapRef& src_bitmap, const Rect& rect, boo

if (tone != Tone()) {
bitmap_effects = create();
bitmap_effects->ToneBlit(0, 0, *src_bitmap, rect, tone, Opacity::opaque);
bitmap_effects->ToneBlit(0, 0, *src_bitmap, rect, tone, Opacity::Opaque());
}

if (blend != Color()) {
if (bitmap_effects) {
// Tone blit was applied
bitmap_effects->BlendBlit(0, 0, *bitmap_effects, bitmap_effects->GetRect(), blend, Opacity::opaque);
bitmap_effects->BlendBlit(0, 0, *bitmap_effects, bitmap_effects->GetRect(), blend, Opacity::Opaque());
} else {
bitmap_effects = create();
bitmap_effects->BlendBlit(0, 0, *src_bitmap, rect, blend, Opacity::opaque);
bitmap_effects->BlendBlit(0, 0, *src_bitmap, rect, blend, Opacity::Opaque());
}
}

Expand All @@ -489,7 +489,7 @@ BitmapRef Cache::SpriteEffect(const BitmapRef& src_bitmap, const Rect& rect, boo
bitmap_effects->Flip(bitmap_effects->GetRect(), flip_x, flip_y);
} else {
bitmap_effects = create();
bitmap_effects->FlipBlit(rect.x, rect.y, *src_bitmap, rect, flip_x, flip_y, Opacity::opaque);
bitmap_effects->FlipBlit(rect.x, rect.y, *src_bitmap, rect, flip_x, flip_y, Opacity::Opaque());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/game_picture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void Game_Picture::UpdateSprite() {
sheet_bitmap->Clear();

if (last_spritesheet_frame >= 0 && last_spritesheet_frame < NumSpriteSheetFrames()) {
sheet_bitmap->Blit(0, 0, *whole_bitmap, r, Opacity::opaque);
sheet_bitmap->Blit(0, 0, *whole_bitmap, r, Opacity::Opaque());
}

sprite->SetBitmap(sheet_bitmap);
Expand Down
59 changes: 59 additions & 0 deletions src/opacity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef EP_OPACITY_H
#define EP_OPACITY_H

#include <cassert>

/** Opacity class. */
struct Opacity {
static constexpr Opacity Opaque() { return Opacity(); }

int top = 255;
int bottom = 255;
int split = 0;

constexpr Opacity() = default;

constexpr Opacity(int opacity) :
Opacity(opacity, opacity, 0) {}

constexpr Opacity(int top_opacity, int bottom_opacity, int split) :
top(top_opacity), bottom(bottom_opacity), split(split) {}

int Value() const {
assert(!IsSplit());
return top;
}

constexpr bool IsSplit() const {
return split > 0 && top != bottom;
}

constexpr bool IsTransparent() const {
return IsSplit() ? top <= 0 && bottom <= 0 : top <= 0;
}

constexpr bool IsOpaque() const {
return IsSplit() ? top >= 255 && bottom >= 255 : top >= 255;
}

private:
};

#endif
2 changes: 1 addition & 1 deletion src/plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void Plane::Draw(Bitmap& dst) {
tone_bitmap = Bitmap::Create(bitmap->GetWidth(), bitmap->GetHeight());
}
tone_bitmap->Clear();
tone_bitmap->ToneBlit(0, 0, *bitmap, bitmap->GetRect(), tone_effect, Opacity::opaque);
tone_bitmap->ToneBlit(0, 0, *bitmap, bitmap->GetRect(), tone_effect, Opacity::Opaque());
}

BitmapRef source = tone_effect == Tone() ? bitmap : tone_bitmap;
Expand Down
9 changes: 4 additions & 5 deletions src/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,17 @@ void Sprite::BlitScreen(Bitmap& dst) {
rect.y %= bitmap_effects->GetHeight();
}

BlitScreenIntern(dst, *draw_bitmap, rect, bush_effect);
BlitScreenIntern(dst, *draw_bitmap, rect);
}
}

void Sprite::BlitScreenIntern(Bitmap& dst, Bitmap const& draw_bitmap,
Rect const& src_rect, int opacity_split) const {

void Sprite::BlitScreenIntern(Bitmap& dst, Bitmap const& draw_bitmap, Rect const& src_rect) const
{
double zoom_x = zoom_x_effect;
double zoom_y = zoom_y_effect;

dst.EffectsBlit(x, y, ox, oy, draw_bitmap, src_rect,
Opacity(opacity_top_effect, opacity_bottom_effect, opacity_split),
Opacity(opacity_top_effect, opacity_bottom_effect, bush_effect),
zoom_x, zoom_y, angle_effect,
waver_effect_depth, waver_effect_phase);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Sprite : public Drawable {

void BlitScreen(Bitmap& dst);
void BlitScreenIntern(Bitmap& dst, Bitmap const& draw_bitmap,
Rect const& src_rect, int opacity_split) const;
Rect const& src_rect) const;
BitmapRef Refresh(Rect& rect);
void SetFlashEffect(const Color &color);
};
Expand Down
Loading

0 comments on commit 5e6f315

Please sign in to comment.