summaryrefslogtreecommitdiff
path: root/src/bitmap.cpp
blob: 612f2af2bc9d8a49e036bf447836bc341c34c3d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
		}
	}
}