diff --git a/src/Raster.cc b/src/Raster.cc index d942af9..b5e2da2 100644 --- a/src/Raster.cc +++ b/src/Raster.cc @@ -377,6 +377,42 @@ void Raster::drawImageWithKey(int startX, int startY, const Image* image, Rgba k } } +void Raster::drawImageAlphaTest(int startX, int startY, const Image* image, byte alpha) { + /*越界部分不描画*/ + int left = tmax(startX, 0); + int bottom = tmax(startY, 0); + + int right = tmin(startX + image->width(), _width); + int top = tmin(startY + image->height(), _height); + + for (int x = left; x < right; x++) { + for (int y = bottom; y < top; y++) { + Rgba color = image->pixelAt(x - left, y - bottom); + if (color._a > alpha) { + setPixel(x, y, color); + } + } + } +} + +void Raster::drawImageAlphaBlend(int startX, int startY, const Image* image, float alpha) { + /*越界部分不描画*/ + int left = tmax(startX, 0); + int bottom = tmax(startY, 0); + + int right = tmin(startX + image->width(), _width); + int top = tmin(startY + image->height(), _height); + + for (int x = left; x < right; x++) { + for (int y = bottom; y < top; y++) { + Rgba dstColor = image->pixelAt(x - left, y - bottom); + Rgba srcColor = getPixel(x, y); + Rgba color = colorLerp(srcColor, dstColor, dstColor._a / 255.0f * alpha); + setPixel(x, y, color); + } + } +} + int Raster::size() { return _width * _height * sizeof (Rgba); } @@ -393,6 +429,10 @@ bool Raster::setPixel(int x, int y, Rgba color) { buffer[ ((_height - 1) - y) * _width + x] = color; } +Rgba Raster::getPixel(int x, int y) { + return buffer[ ((_height - 1) - y) * _width + x]; +} + bool Raster::isIn(int2 point) { if (point.x >= 0 && point.x <= _width && point.y >= 0 && point.y < _height) { return true; diff --git a/src/Raster.h b/src/Raster.h index 559e54b..a0ef514 100644 --- a/src/Raster.h +++ b/src/Raster.h @@ -97,9 +97,12 @@ public: void drawRect(const int2* points, const Rgba* colors); void drawImage(int startX, int startY, const Image* image); void drawImageWithKey(int startX, int startY, const Image* image, Rgba key); + void drawImageAlphaTest(int startX, int startY, const Image* image, byte key); + void drawImageAlphaBlend(int startX, int startY, const Image* image, float alpha = 1); int size(); void clean(); bool setPixel(int x, int y, Rgba color); + Rgba getPixel(int x, int y); bool isIn(int2 point); private: int _width; diff --git a/src/common.h b/src/common.h index 52dedc2..3ef0e40 100644 --- a/src/common.h +++ b/src/common.h @@ -65,6 +65,7 @@ struct tvec2 { typedef tvec2 float2; typedef tvec2 int2; +typedef unsigned char byte; #endif /* COMMON_H */ diff --git a/src/draw_image.cc b/src/draw_image.cc index 9361e02..e0c574e 100644 --- a/src/draw_image.cc +++ b/src/draw_image.cc @@ -17,15 +17,21 @@ gint width = 800; Raster raster(width, height); void example1() { - //画背景 - Image* image1 = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/image/bg.png"); - raster.drawImage(0, 0, image1); - delete image1; - - //画前景 - Image* image2 = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/image/colorKey.bmp"); - raster.drawImageWithKey(200, 250, image2, Rgba(255, 0, 0)); - delete image2; + //画背景 + Image* image1 = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/image/bg.png"); + raster.drawImage(0, 0, image1); + delete image1; + + //画前景 + Image* image2 = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/image/colorKey.bmp"); + raster.drawImageWithKey(200, 250, image2, Rgba(255, 0, 0)); + delete image2; + + //画草 + Image* image3 = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/image/grass.png"); + //raster.drawImageAlphaTest(400, 250, image3, 100); + raster.drawImageAlphaBlend(400, 250, image3,0.5); + delete image3; }