diff --git a/src/graphics/core/ImageData.cpp b/src/graphics/core/ImageData.cpp index a2b1d524..44a1936e 100644 --- a/src/graphics/core/ImageData.cpp +++ b/src/graphics/core/ImageData.cpp @@ -1,5 +1,6 @@ #include "ImageData.hpp" +#include #include #include #include @@ -187,6 +188,41 @@ void ImageData::drawLine(int x1, int y1, int x2, int y2, const glm::ivec4& color } } +template +static void draw_rect(ImageData& image, int dstX, int dstY, int width, int height, const glm::ivec4& color) { + ubyte* data = image.getData(); + int imageWidth = image.getWidth(); + int imageHeight = image.getHeight(); + + int x1 = glm::min(glm::max(dstX, 0), imageWidth - 1); + int y1 = glm::min(glm::max(dstY, 0), imageHeight - 1); + + int x2 = glm::min(glm::max(dstX + width, 0), imageWidth - 1); + int y2 = glm::min(glm::max(dstY + height, 0), imageHeight - 1); + + for (int y = y1; y <= y2; y++) { + for (int x = x1; x <= x2; x++) { + int index = (y * imageWidth + x) * channels; + for (int i = 0; i < channels; i++) { + data[index + i] = color[i]; + } + } + } +} + +void ImageData::drawRect(int x, int y, int width, int height, const glm::ivec4& color) { + switch (format) { + case ImageFormat::rgb888: + draw_rect<3>(*this, x, y, width, height, color); + break; + case ImageFormat::rgba8888: + draw_rect<4>(*this, x, y, width, height, color); + break; + default: + break; + } +} + void ImageData::blitRGB_on_RGBA(const ImageData& image, int x, int y) { ubyte* source = image.getData(); uint srcwidth = image.getWidth(); diff --git a/src/graphics/core/ImageData.hpp b/src/graphics/core/ImageData.hpp index 213a352c..d09a0411 100644 --- a/src/graphics/core/ImageData.hpp +++ b/src/graphics/core/ImageData.hpp @@ -28,6 +28,7 @@ public: void flipY(); void drawLine(int x1, int y1, int x2, int y2, const glm::ivec4& color); + void drawRect(int x, int y, int width, int height, const glm::ivec4& color); void blit(const ImageData& image, int x, int y); void extrude(int x, int y, int w, int h); void fixAlphaColor();