diff options
author | chai <chaifix@163.com> | 2018-09-07 13:30:44 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-09-07 13:30:44 +0800 |
commit | e72188433348c270a54879da9f086f1b527b580f (patch) | |
tree | b2cb92b37579c6f9cff77f52fc591426eb2d8286 /src/libjin/Graphics/Bitmap.cpp | |
parent | 28ca1f570d417671904a25c2a9c589fdb1eb7a03 (diff) |
*update
Diffstat (limited to 'src/libjin/Graphics/Bitmap.cpp')
-rw-r--r-- | src/libjin/Graphics/Bitmap.cpp | 57 |
1 files changed, 51 insertions, 6 deletions
diff --git a/src/libjin/Graphics/Bitmap.cpp b/src/libjin/Graphics/Bitmap.cpp index 93ea1a9..c2acff4 100644 --- a/src/libjin/Graphics/Bitmap.cpp +++ b/src/libjin/Graphics/Bitmap.cpp @@ -24,12 +24,23 @@ namespace graphics return bitmap; } - /*static*/ Bitmap* Bitmap::createBitmap(int w, int h) + /*static*/ Bitmap* Bitmap::createBitmap(int w, int h, Color color) { Bitmap* bitmap = new Bitmap(w, h); + if (color != Color::BLACK) + bitmap->setPixels(color); return bitmap; } + /*static */ Bitmap* Bitmap::clone(const Bitmap* bitmap) + { + Bitmap* b = new Bitmap(); + int w = bitmap->getWidth(); + int h = bitmap->getHeight(); + b->resetPixels(bitmap->getPixels(), w, h); + return b; + } + Bitmap::Bitmap() : width(0) , height(0) @@ -58,7 +69,7 @@ namespace graphics height = h; } - void Bitmap::setPixels(Color* p, int w, int h) + void Bitmap::resetPixels(const Color* p, int w, int h) { if (pixels != nullptr) delete[] pixels; @@ -69,15 +80,49 @@ namespace graphics height = h; } - void Bitmap::setPixel(const Color& pixel, int x, int y) + void Bitmap::setPixel(const Color& c, int x, int y) { if (pixels == nullptr) return; if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1)) return; - pixels[x + y * width] = pixel; + pixels[x + y * width] = c; } - + + void Bitmap::resetPixels(const Color& c, int w, int h) + { + if (pixels != nullptr) + delete[] pixels; + size_t s = w * h * sizeof(Color); + pixels = (Color*)calloc(1, s); + width = w; + height = h; + for (int x = 0; x < w; ++x) + { + for (int y = 0; y < h; ++y) + { + pixels[x + y * w] = c; + } + } + } + + void Bitmap::setPixels(Color* p) + { + size_t s = width * height * sizeof(Color); + memcpy(pixels, p, s); + } + + void Bitmap::setPixels(Color c) + { + for (int x = 0; x < width; ++x) + { + for (int y = 0; y < height; ++y) + { + pixels[x + y * width] = c; + } + } + } + Color Bitmap::getPixel(int x, int y) { if (pixels == nullptr) @@ -87,7 +132,7 @@ namespace graphics return pixels[x + y * width]; } - const Color* Bitmap::getPixels() + const Color* Bitmap::getPixels() const { return pixels; } |