Skip to content

Commit

Permalink
Remove adjust_surface_alpha_formula
Browse files Browse the repository at this point in the history
This was only used in one place and made the sdl/utils.cpp file
depend on the WFL (formula) engine, which is undesirable.

Since it was only used in one place, the implementation was
moved to that one place instead.
  • Loading branch information
CelticMinstrel committed Dec 11, 2016
1 parent 9f10226 commit aa6ee42
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 96 deletions.
86 changes: 85 additions & 1 deletion src/image_modifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include "color.hpp"
#include "serialization/string_utils.hpp"

#include "formula/formula.hpp"
#include "formula/callable.hpp"

#include <map>

#define GETTEXT_DOMAIN "wesnoth-lib"
Expand Down Expand Up @@ -222,9 +225,90 @@ surface wipe_alpha_modification::operator()(const surface& src) const
return wipe_alpha(src);
}

// TODO: Is this useful enough to move into formula/callable_objects?
class pixel_callable : public game_logic::formula_callable {
public:
pixel_callable(SDL_Point p, color_t clr, Uint32 w, Uint32 h) : p(p), clr(clr), w(w), h(h) {}
void get_inputs(std::vector<game_logic::formula_input>* inputs) const override {
inputs->push_back(game_logic::formula_input("x", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("y", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("red", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("green", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("blue", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("alpha", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("height", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("width", game_logic::FORMULA_READ_ONLY));
}
variant get_value(const std::string& key) const override {
if(key == "x") {
return variant(p.x);
} else if(key == "y") {
return variant(p.y);
} else if(key == "red") {
return variant(clr.r);
} else if(key == "green") {
return variant(clr.g);
} else if(key == "blue") {
return variant(clr.b);
} else if(key == "alpha") {
return variant(clr.a);
} else if(key == "width") {
return variant(w);
} else if(key == "height") {
return variant(h);
}
return variant();
}
private:
SDL_Point p;
color_t clr;
Uint32 w, h;
};

surface adjust_alpha_modification::operator()(const surface & src) const
{
return adjust_surface_alpha_formula(src, formula_);
if(src == nullptr) {
return nullptr;
}

game_logic::formula new_alpha(formula_);

surface nsurf(make_neutral_surface(src));

if(nsurf == nullptr) {
std::cerr << "could not make neutral surface...\n";
return nullptr;
}

adjust_surface_alpha(nsurf, SDL_ALPHA_OPAQUE);

{
surface_lock lock(nsurf);
Uint32* cur = lock.pixels();
Uint32*const end = cur + nsurf->w * src->h;
Uint32*const beg = cur;

while(cur != end) {
color_t pixel;
pixel.a = (*cur) >> 24;
pixel.r = (*cur) >> 16;
pixel.g = (*cur) >> 8;
pixel.b = (*cur);

int i = cur - beg;
SDL_Point p;
p.y = i / nsurf->w;
p.x = i % nsurf->w;

pixel_callable px(p, pixel, nsurf->w, nsurf->h);
pixel.a = std::min<unsigned>(new_alpha.evaluate(px).as_int(), 255);
*cur = (pixel.a << 24) + (pixel.r << 16) + (pixel.g << 8) + pixel.b;

++cur;
}
}

return nsurf;
}

surface crop_modification::operator()(const surface& src) const
Expand Down
94 changes: 0 additions & 94 deletions src/sdl/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
#include "video.hpp"
#include "xBRZ/xbrz.hpp"

#include "formula/formula.hpp"
#include "formula/callable.hpp"

#include <algorithm>
#include <cassert>
#include <cstring>
Expand Down Expand Up @@ -1115,97 +1112,6 @@ void adjust_surface_alpha(surface& surf, fixed_t amount)
SDL_SetSurfaceAlphaMod(surf, Uint8(amount));
}

class pixel_callable : public game_logic::formula_callable {
public:
pixel_callable(SDL_Point p, color_t clr, Uint32 w, Uint32 h) : p(p), clr(clr), w(w), h(h) {}
void get_inputs(std::vector<game_logic::formula_input>* inputs) const override
{
inputs->push_back(game_logic::formula_input("x", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("y", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("red", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("green", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("blue", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("alpha", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("height", game_logic::FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("width", game_logic::FORMULA_READ_ONLY));
}
variant get_value(const std::string& key) const override
{
if(key == "x") {
return variant(p.x);
} else if(key == "y") {
return variant(p.y);
} else if(key == "red") {
return variant(clr.r);
} else if(key == "green") {
return variant(clr.g);
} else if(key == "blue") {
return variant(clr.b);
} else if(key == "alpha") {
return variant(clr.a);
} else if(key == "width") {
return variant(w);
} else if(key == "height") {
return variant(h);
}
return variant();
}
private:
SDL_Point p;
color_t clr;
Uint32 w, h;
};

surface adjust_surface_alpha_formula(const surface &surf, const std::string& formula, bool optimize)
{
if(surf== nullptr) {
return nullptr;
}

game_logic::formula new_alpha(formula);

surface nsurf(make_neutral_surface(surf));

if(nsurf == nullptr) {
std::cerr << "could not make neutral surface...\n";
return nullptr;
}

adjust_surface_alpha(nsurf, SDL_ALPHA_OPAQUE);

{
surface_lock lock(nsurf);
Uint32* cur = lock.pixels();
Uint32*const end = cur + nsurf->w*surf->h;
Uint32*const beg = cur;

while(cur != end) {
color_t pixel;
pixel.a = (*cur) >> 24;
pixel.r = (*cur) >> 16;
pixel.g = (*cur) >> 8;
pixel.b = (*cur);

int i = cur - beg;
SDL_Point p;
p.y = i / nsurf->w;
p.x = i % nsurf->w;

pixel_callable px(p, pixel, nsurf->w, nsurf->h);
pixel.a = std::min<unsigned>(new_alpha.evaluate(px).as_int(),255);
*cur = (pixel.a << 24) + (pixel.r << 16) + (pixel.g << 8) + pixel.b;

++cur;
}
}

if(optimize) {
adjust_surface_alpha(nsurf, SDL_ALPHA_OPAQUE);
}

return nsurf;
}

surface adjust_surface_alpha_add(const surface &surf, int amount, bool optimize)
{
if(surf== nullptr) {
Expand Down
1 change: 0 additions & 1 deletion src/sdl/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ surface get_surface_portion(const surface &surf, SDL_Rect &rect);

void adjust_surface_alpha(surface& surf, fixed_t amount);
surface adjust_surface_alpha_add(const surface &surf, int amount, bool optimize=true);
surface adjust_surface_alpha_formula(const surface &surf, const std::string& formula, bool optimize=true);

/** Applies a mask on a surface. */
surface mask_surface(const surface &surf, const surface &mask, bool* empty_result = nullptr, const std::string& filename = std::string());
Expand Down

0 comments on commit aa6ee42

Please sign in to comment.