From 36d89a6ac257e28357620d27d3c32970a71e7c35 Mon Sep 17 00:00:00 2001 From: blobt <380255922@qq.com> Date: Tue, 11 Feb 2020 17:14:38 +0800 Subject: [PATCH] color key --- src/Raster.cc | 22 ++++++++++++++++++-- src/Raster.h | 1 + src/Rgba.h | 2 +- src/draw_image.cc | 51 ++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/Raster.cc b/src/Raster.cc index 200bd49..d942af9 100644 --- a/src/Raster.cc +++ b/src/Raster.cc @@ -354,7 +354,25 @@ void Raster::drawImage(int startX, int startY, const Image* image) { for (int x = left; x < right; x++) { for (int y = bottom; y < top; y++) { Rgba color = image->pixelAt(x - left, y - bottom); - drawPoint(x, y, color); + setPixel(x, y, color); + } + } +} + +void Raster::drawImageWithKey(int startX, int startY, const Image* image, Rgba key) { + /*越界部分不描画*/ + 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 != key) { + setPixel(x, y, color); + } } } } @@ -372,7 +390,7 @@ bool Raster::setPixel(int x, int y, Rgba color) { return false; } //行列是反的 - buffer[ (_height - y) * _width + x] = color; + buffer[ ((_height - 1) - y) * _width + x] = color; } bool Raster::isIn(int2 point) { diff --git a/src/Raster.h b/src/Raster.h index 45d5212..559e54b 100644 --- a/src/Raster.h +++ b/src/Raster.h @@ -96,6 +96,7 @@ public: void drawFilledRect(int startX, int startY, int width, int height); 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); int size(); void clean(); bool setPixel(int x, int y, Rgba color); diff --git a/src/Rgba.h b/src/Rgba.h index b7ae512..3550265 100644 --- a/src/Rgba.h +++ b/src/Rgba.h @@ -18,7 +18,7 @@ class Rgba { public: - Rgba(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0, unsigned char a = 0); + Rgba(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0, unsigned char a = 255); Rgba(unsigned int rgba); friend bool operator==(Rgba &left, Rgba &right); friend bool operator!=(Rgba &left, Rgba &righe); diff --git a/src/draw_image.cc b/src/draw_image.cc index a7dfd08..9361e02 100644 --- a/src/draw_image.cc +++ b/src/draw_image.cc @@ -6,21 +6,62 @@ #include "Raster.h" #include "common.h" #include "Image.h" +#include using namespace std; -gint height = 500; -gint width = 500; +gint height = 600; +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; + +} + +void example2() { + int left = 0; + int bottom = 0; + + int right = 300; + int top = 100; + + Rgba* t = new Rgba[800 * 600]; + + + for (int i = 0; i < 800 * 600; i++) { + t[i] = Rgba(255, 255, 255); + } + + int ys = 0; + for (int y = bottom; y < top; y++) { + ys++; + for (int x = left; x < right; x++) { + //raster.setPixel(x, y, Rgba(255, 0, 0)); + t[(height - ys) * width + x] = Rgba(255, 0, 0); + } + } + + memcpy(raster.buffer, t, 800 * 600 * 4); + + + delete t; +} + unsigned char* makeBitmap() { raster.clean(); - Image* image = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/build/bin/12.jpg"); - - raster.drawImage(25, 25, image); + example1(); return (unsigned char*) raster.buffer; }