summaryrefslogtreecommitdiff
path: root/src/image.cpp
blob: 177968a7b8d13d166eb7385e8e555681c2aca006 (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
#include "FLD.hpp"

static FLD::Image&
invalid_image()
{
	static FLD::Image image = { .valid = false };
	return image;
}

FLD::Image&
FLD::image(const std::string &path)
{
	if (images.contains(path))
		return images[path];

	const auto surf = SDL_LoadBMP(path.c_str());
	if (surf == nullptr) {
		sdlError(path);
		return invalid_image();
	}

	const auto tex = SDL_CreateTextureFromSurface(_renderer, surf);
	SDL_FreeSurface(surf);
	if (tex == nullptr) {
		sdlError(path);
		return invalid_image();
	}

	if (SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND) < 0) {
		SDL_DestroyTexture(tex);
		sdlError(path);
		return invalid_image();
	}

	int width, height;
	if (SDL_QueryTexture(tex, nullptr, nullptr, &width, &height) < 0) {
		SDL_DestroyTexture(tex);
		sdlError(path);
		return invalid_image();
	}

	images[path] = {
		.path = path,
		.ptr = tex,
		.width = width,
		.height = height
	};

	return images[path];
}