diff options
Diffstat (limited to 'src/libjin/Graphics/je_bitmap.cpp')
-rw-r--r-- | src/libjin/Graphics/je_bitmap.cpp | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/src/libjin/Graphics/je_bitmap.cpp b/src/libjin/Graphics/je_bitmap.cpp index 0747f0b..cdab46d 100644 --- a/src/libjin/Graphics/je_bitmap.cpp +++ b/src/libjin/Graphics/je_bitmap.cpp @@ -1,6 +1,7 @@ #define STB_IMAGE_IMPLEMENTATION -#include "../3rdparty/stb/stb_image.h" +#include "stb/stb_image.h" +#include "../common/je_exception.h" #include "../filesystem/je_asset_database.h" #include "../math/je_math.h" @@ -22,7 +23,6 @@ namespace JinEngine return createBitmap(&buffer, buffer.size()); } - /* pixelbitmap */ Bitmap* Bitmap::createBitmap(const void* pixel, unsigned width, unsigned height) { Bitmap* bitmap = new Bitmap(width, height); @@ -36,8 +36,11 @@ namespace JinEngine return nullptr; int w, h; void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha); - if (data == nullptr) - return nullptr; + if (data == nullptr) + { + throw Exception("Could not create bitmap from image data."); + return nullptr; + } Bitmap* bitmap = new Bitmap(); bitmap->pixels = (Color*)data; bitmap->width = w; @@ -53,6 +56,20 @@ namespace JinEngine return bitmap; } + /*static*/ Bitmap* Bitmap::createBitmap(int width, int height, std::function<Color(int, int, int, int)> drawer) + { + Bitmap* bitmap = new Bitmap(width, height); + for (int y = 0; y < height; ++y) + { + for (int x = 0; x < width; ++x) + { + Color c = drawer(width, height, x, y); + bitmap->setPixel(c, x, y); + } + } + return bitmap; + } + /*static */ Bitmap* Bitmap::clone(const Bitmap* bitmap) { Bitmap* b = new Bitmap(); @@ -74,6 +91,8 @@ namespace JinEngine width = w; height = h; pixels = new Color[w*h]; + if (pixels == nullptr) + throw Exception("No enough memory."); } Bitmap::~Bitmap() @@ -95,6 +114,8 @@ namespace JinEngine if (pixels != nullptr) delete[] pixels; pixels = new Color[w*h]; + if (pixels == nullptr) + throw Exception("Not enough memory."); size_t s = w * h * sizeof(Color); memcpy(pixels, p, s); width = w; @@ -103,10 +124,12 @@ namespace JinEngine void Bitmap::setPixel(const Color& c, int x, int y) { - if (pixels == nullptr) - return; + if (pixels == nullptr) + throw Exception("Bitmap don't have pixel space."); if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1)) return; + if (x + y * width >= width * height) + throw Exception("Pixel <%d, %d> of bitmap is out of range.", x, y); pixels[x + y * width] = c; } @@ -115,7 +138,8 @@ namespace JinEngine if (pixels != nullptr) delete[] pixels; pixels = new Color[w*h]; - size_t s = w * h * sizeof(Color); + if (pixels == nullptr) + throw Exception("Not enough memory."); width = w; height = h; for (int x = 0; x < w; ++x) @@ -127,8 +151,10 @@ namespace JinEngine } } - void Bitmap::setPixels(Color* p) + void Bitmap::setPixels(Color* p, int count) { + if (count > width * height) + throw Exception("Pixels are out of range."); size_t s = width * height * sizeof(Color); memcpy(pixels, p, s); } |