This repository was archived by the owner on Mar 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsomething_texture.cpp
More file actions
54 lines (48 loc) · 1.86 KB
/
something_texture.cpp
File metadata and controls
54 lines (48 loc) · 1.86 KB
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
54
#include "./something_texture.hpp"
SDL_Texture *load_texture_from_bmp_file(SDL_Renderer *renderer,
const char *image_filepath,
SDL_Color color_key)
{
SDL_Surface *image_surface = sec(SDL_LoadBMP(image_filepath));
sec(SDL_SetColorKey(
image_surface,
SDL_TRUE,
SDL_MapRGB(
image_surface->format,
color_key.r,
color_key.g,
color_key.b)));
SDL_Texture *image_texture = sec(SDL_CreateTextureFromSurface(renderer, image_surface));
SDL_FreeSurface(image_surface);
return image_texture;
}
SDL_Surface *load_png_file_as_surface(const char *image_filename)
{
int width, height;
uint32_t *image_pixels = (uint32_t *) stbi_load(image_filename, &width, &height, NULL, 4);
if (image_pixels == NULL) {
println(stderr, "[ERROR] Could not load `", image_filename, "` as PNG");
abort();
}
SDL_Surface* image_surface =
sec(SDL_CreateRGBSurfaceFrom(image_pixels,
(int) width,
(int) height,
32,
(int) width * 4,
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000));
return image_surface;
}
SDL_Surface *load_png_file_as_surface(String_View image_filename)
{
char *filepath_cstr = (char*) malloc(image_filename.count + 1);
assert(filepath_cstr != NULL);
memcpy(filepath_cstr, image_filename.data, image_filename.count);
filepath_cstr[image_filename.count] = '\0';
auto result = load_png_file_as_surface(filepath_cstr);
free(filepath_cstr);
return result;
}