Commit ae9e22fe authored by Romain Bignon's avatar Romain Bignon

new method Gray2Color

parent 21952646
......@@ -151,17 +151,16 @@ SDL_Surface* CreateRGBASurface (int width, int height, Uint32 flags){
return surface;
}
void ChangePixelColor(ECImage* surf, Color last_color, Color new_color)
void ChangePixelColor(ECImage* surf, const Color& last_color, const Color& new_color)
{
SLOCK(surf->Img);
for(int x = 0; x < surf->GetWidth(); x++)
for(int y = 0; y < surf->GetHeight(); y++)
{
Uint32 col = getpixel(surf->Img, x, y);
/*Uint8 r, g, b;
SDL_GetRGB(col, surf->Img->format, &r, &g, &b);*/
if(last_color == surf->GetColor(col))
putpixel(surf->Img, x, y, surf->MapColor(new_color));
Color pixcolor = surf->GetColor(col);
if(last_color == pixcolor)
putpixel(surf->Img, x, y, surf->MapColor(new_color.WithAlpha(pixcolor.GetAlpha())));
}
SUNLOCK(surf->Img);
}
......@@ -244,6 +243,7 @@ int ECSprite::init(ECSpriteBase *base, ECImage *screen)
order = true;
repeat = true;
mX = 0; mY = 0; mOldX = 0; mOldY = 0;
autofree = false;
//mSpriteBase = new ECSpriteBase(base->path.c_str());
mSpriteBase = base;
......@@ -256,7 +256,8 @@ int ECSprite::init(ECSpriteBase *base, ECImage *screen)
ECSprite::~ECSprite()
{
if (autofree)
delete mSpriteBase;
}
bool ECSprite::Anim() const { return (mSpriteBase->animation && mAnimating); }
......@@ -330,11 +331,26 @@ int ECSprite::GetHeight()
return (mSpriteBase ? mSpriteBase->mH : 0);
}
void ECSprite::ChangeColor(Color first, Color to)
void ECSprite::ChangeColor(const Color& first, const Color& to)
{
mSpriteBase->ChangeColor(first, to);
}
void ECSprite::CopySpriteBase()
{
ECSpriteBase* spr = mSpriteBase;
mSpriteBase = spr->Copy();
mSpriteBase->animation = spr->animation;
if (autofree)
delete spr;
autofree = true;
}
void ECSprite::Gray2Color(const Color& to)
{
mSpriteBase->Gray2Color(to);
}
void ECSprite::RotoZoom(double angle, double zoomx, double zoomy, bool smooth)
{
mSpriteBase->RotoZoom(angle, zoomx, zoomy, smooth);
......@@ -367,6 +383,13 @@ ECSpriteBase::~ECSpriteBase()
}
ECSpriteBase* ECSpriteBase::Copy() const
{
ECSpriteBase* sb = new ECSpriteBase();
sb->init(path.c_str());
return sb;
}
int ECSpriteBase::init(const char *dir)
{
std::string filename;
......@@ -431,12 +454,18 @@ int ECSpriteBase::init(const char *dir)
return 0;
}
void ECSpriteBase::ChangeColor(Color from, Color to)
void ECSpriteBase::ChangeColor(const Color& from, const Color& to)
{
for(uint i=0; i < NumFrames(); ++i)
ChangePixelColor(&surfaces[i], from, to);
}
void ECSpriteBase::Gray2Color(const Color& to)
{
for(uint i=0; i < NumFrames(); ++i)
surfaces[i].Gray2Color(to);
}
void ECSpriteBase::RotoZoom(double angle, double zoomx, double zoomy, bool smooth)
{
for(uint i=0; i < NumFrames(); ++i)
......@@ -891,3 +920,26 @@ void ECImage::PutPixel(int x, int y, Uint32 pixel){
break;
}
}
#if !defined(min) || !defined(max)
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
#endif
void ECImage::Gray2Color(const Color& new_color)
{
for(int x = 0; x < GetWidth(); ++x)
for(int y = 0; y < GetHeight(); ++y)
{
Uint32 col = GetPixel(x, y);
Uint8 r, g, b, a;
SDL_GetRGBA(col, Img->format, &r, &g, &b, &a);
if (min(r, min(g, b)) > max(r, max(g, b)) - 20)
{
Color newc = Color(r * new_color.GetRed() / 255,
g * new_color.GetGreen() / 255,
b * new_color.GetBlue() / 255,
a);
PutPixel(x, y, MapColor(newc));
}
}
}
......@@ -32,7 +32,7 @@ class ECSpriteBase;
class Color;
class ECImage;
void ChangePixelColor(ECImage* surf, Color last_color, Color new_color);
void ChangePixelColor(ECImage* surf, const Color& last_color, const Color& new_color);
Uint32 getpixel(SDL_Surface * surface, int x, int y);
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
void DrawRect(SDL_Surface * screen, int x1, int y1, int x2, int y2, Uint32 color);
......@@ -112,7 +112,8 @@ public:
ECImage* First() const;
void ChangeColor(Color first, Color to);
void ChangeColor(const Color& first, const Color& to);
void Gray2Color(const Color& to);
void RotoZoom(double angle, double zoomx, double zoomy, bool smooth);
void Zoom(double zoomx, double zoomy, bool smooth);
......@@ -120,6 +121,8 @@ public:
ECImage* Window() const { return mScreen; }
ECSpriteBase* SpriteBase() const { return mSpriteBase; }
void CopySpriteBase();
/* Variables privées */
private:
uint mFrame;
......@@ -130,6 +133,7 @@ private:
long mLastupdate;
bool order, repeat;
ECSpriteBase *mSpriteBase;
bool autofree;
ECImage *mScreen;
};
......@@ -144,7 +148,8 @@ public:
*/
int init(const char *dir);
void ChangeColor(Color from, Color to);
void ChangeColor(const Color& from, const Color& to);
void Gray2Color(const Color& to);
void RotoZoom(double angle, double zoomx, double zoomy, bool smooth);
void Zoom(double zoomx, double zoomy, bool smooth);
......@@ -153,6 +158,8 @@ public:
ECSpriteBase();
~ECSpriteBase();
ECSpriteBase* Copy() const;
ECImage* First();
bool Alpha() const { return alpha; }
......@@ -257,6 +264,8 @@ public:
Uint32 GetPixel(int x, int y);
void PutPixel(int x, int y, Uint32 pixel);
void Gray2Color(const Color& new_color);
/* Attributs */
public:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment