From cb7af3f4cac90f95926477b4001f9f80037568d5 Mon Sep 17 00:00:00 2001 From: Jun Zhang Date: Sun, 30 Jan 2022 12:30:02 +0800 Subject: refactor: adjust the project infra. (#1) * refactor: adjust the project infra. This patch adds cmake build system to the project, and adjust infrastructure stuff. Signed-off-by: Jun Zhang * fix: remove compiler flags that only exist in GCC. Signed-off-by: Jun Zhang --- src/bitmap.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/bitmap.cpp (limited to 'src/bitmap.cpp') diff --git a/src/bitmap.cpp b/src/bitmap.cpp new file mode 100644 index 0000000..612f2af --- /dev/null +++ b/src/bitmap.cpp @@ -0,0 +1,53 @@ +#include "bitmap.h" + +#define STB_IMAGE_IMPLEMENTATION +#include + +Bitmap::Bitmap(const char* image) +{ + int width, height, bitDepth; + + uint8_t* buffer = stbi_load(image, &width, &height, &bitDepth, STBI_rgb_alpha); + if (!buffer) { + return; + } + + this->width = width; + this->height = height; + data = new uint32_t[width * height]; + + for (int i = 0; i < width * height; i++) { + uint8_t r = buffer[i * 4 + 0]; + uint8_t g = buffer[i * 4 + 1]; + uint8_t b = buffer[i * 4 + 2]; + uint8_t a = buffer[i * 4 + 3]; + + data[i] = (a << 24) | (r << 16) | (g << 8) | b; + } + + stbi_image_free(buffer); +} + +void Bitmap::clear(uint32_t colour) +{ + memset(data, colour, width * height * 4); +} + +void Bitmap::blit(Bitmap const& other, int xo, int yo, int xc, int yc, int w, int h) +{ + constexpr int MaskColour = 0xffff00ff; + + for (int y = 0; y < h; ++y) { + int yp = y + yo; + if (yp < 0 || yp >= height) continue; + + for (int x = 0; x < w; ++x) { + int xp = x + xo; + if (xp < 0 || xp >= width) continue; + + int src = other.data[(x + xc) + (y + yc) * other.width]; + if (src != MaskColour) + data[xp + yp * width] = src; + } + } +} -- cgit v1.2.3