diff options
Diffstat (limited to 'src/libjin/graphics/je_bitmap.cpp')
-rw-r--r-- | src/libjin/graphics/je_bitmap.cpp | 115 |
1 files changed, 58 insertions, 57 deletions
diff --git a/src/libjin/graphics/je_bitmap.cpp b/src/libjin/graphics/je_bitmap.cpp index a6db55e..18e15a8 100644 --- a/src/libjin/graphics/je_bitmap.cpp +++ b/src/libjin/graphics/je_bitmap.cpp @@ -15,85 +15,86 @@ namespace JinEngine namespace Graphics { - Bitmap* Bitmap::createBitmap(const char* path) + Bitmap* Bitmap::clone() + { + Bitmap* b = new Bitmap(this); + return b; + } + + Bitmap::Bitmap() + : width(0) + , height(0) + , pixels(nullptr) + { + } + + Bitmap::Bitmap(unsigned w, unsigned h) + { + width = w; + height = h; + pixels = new Color[w*h]; + if (pixels == nullptr) + throw Exception("No enough memory."); + } + + Bitmap::Bitmap(const char* path) { AssetDatabase* ad = AssetDatabase::get(); - Buffer buffer; + Buffer buffer; ad->read(path, buffer); - return createBitmap(&buffer, buffer.size()); + new (this) Bitmap(&buffer, buffer.size()); } - Bitmap* Bitmap::createBitmap(const void* pixel, unsigned width, unsigned height) - { - Bitmap* bitmap = new Bitmap(width, height); - memcpy(bitmap->pixels, pixel, width*height * sizeof(Color)); - return bitmap; - } + Bitmap::Bitmap(const void* pix, unsigned w, unsigned h) + { + new (this) Bitmap(w, h); + memcpy(pixels, pix, w * h * sizeof(Color)); + } - Bitmap* Bitmap::createBitmap(const void* imgData, size_t size) - { - if (imgData == nullptr) - return nullptr; - int w, h; - void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha); + Bitmap::Bitmap(const void* imgData, size_t size) + { + if (imgData == nullptr) + return; + int w, h; + void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha); if (data == nullptr) { throw Exception("Could not create bitmap from image data."); - return nullptr; + return; } - Bitmap* bitmap = new Bitmap(); - bitmap->pixels = (Color*)data; - bitmap->width = w; - bitmap->height = h; - return bitmap; - } + new (this) Bitmap(); + pixels = (Color*)data; + width = w; + height = h; + } - Bitmap* Bitmap::createBitmap(int w, int h, Color color) - { - Bitmap* bitmap = new Bitmap(w, h); - if (color != Color::BLACK) - bitmap->setPixels(color); - return bitmap; - } + Bitmap::Bitmap(int w, int h, Color color) + { + new (this) Bitmap(w, h); + if (color != Color::BLACK) + setPixels(color); + } - Bitmap* Bitmap::createBitmap(int width, int height, std::function<Color(int, int, int, int)> drawer) + Bitmap::Bitmap(int w, int h, std::function<Color(int, int, int, int)> drawer) { - Bitmap* bitmap = new Bitmap(width, height); + new (this) Bitmap(w, h); 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); + setPixel(c, x, y); } } - 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) - , pixels(nullptr) - { - } - - Bitmap::Bitmap(unsigned w, unsigned h) - { - width = w; - height = h; - pixels = new Color[w*h]; - if (pixels == nullptr) - throw Exception("No enough memory."); - } + Bitmap::Bitmap(const Bitmap* bitmap) + { + new (this) Bitmap(); + int w = bitmap->getWidth(); + int h = bitmap->getHeight(); + resetPixels(bitmap->getPixels(), w, h); + } Bitmap::~Bitmap() { |