Browse Source

color key

master
blobt 5 years ago
parent
commit
36d89a6ac2
  1. 22
      src/Raster.cc
  2. 1
      src/Raster.h
  3. 2
      src/Rgba.h
  4. 51
      src/draw_image.cc

22
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<int>(startX, 0);
int bottom = tmax<int>(startY, 0);
int right = tmin<int>(startX + image->width(), _width);
int top = tmin<int>(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) {

1
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);

2
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);

51
src/draw_image.cc

@ -6,21 +6,62 @@
#include "Raster.h"
#include "common.h"
#include "Image.h"
#include <malloc.h>
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;
}

Loading…
Cancel
Save