summaryrefslogtreecommitdiff
path: root/src/bitmap.cpp
diff options
context:
space:
mode:
authorJun Zhang <jun@junz.org>2022-01-30 12:30:02 +0800
committerGitHub <noreply@github.com>2022-01-29 21:30:02 -0700
commitcb7af3f4cac90f95926477b4001f9f80037568d5 (patch)
tree022f4700c796a9935acd3ee0d0fd80a812a78464 /src/bitmap.cpp
parent99b4763f1028e72bb06d8db6c7e8ace469f8989c (diff)
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 <jun@junz.org> * fix: remove compiler flags that only exist in GCC. Signed-off-by: Jun Zhang <jun@junz.org>
Diffstat (limited to 'src/bitmap.cpp')
-rw-r--r--src/bitmap.cpp53
1 files changed, 53 insertions, 0 deletions
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 <stb/stb_image.h>
+
+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;
+ }
+ }
+}