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
55
56
57
58
59
60
61
62
63
64
|
#include <SDL.h>
#define STB_IMAGE_IMPLEMENTATION
#define STB_ASSERT(x)
#define STB_ONLY_PNG
#include "stb_image.h"
#include "util.h"
#include "sprites.h"
typedef struct {
const Uint32 r,
g,
b,
a;
} mask_t;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
static const mask_t
MASK = { 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff };
#else
static const mask_t
MASK = { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 };
#endif /* SDL_BYTEORDER */
SDL_Texture *
sprites_init(
SDL_Renderer * const renderer,
const char * const png_path
) {
// load image
int x, y, n;
unsigned char *data = stbi_load(png_path, &x, &y, &n, 4);
if (!data) {
die("stbi_load()");
}
// create surface
SDL_Surface * const surface = SDL_CreateRGBSurfaceFrom(data, x, y, 32, 4 * x, MASK.r, MASK.g, MASK.b, MASK.a);
if (!surface) {
die("SDLCreateRGBSurfaceFrom(): %s", SDL_GetError());
}
// free image data
free(data);
// create texture
SDL_Texture *r = SDL_CreateTextureFromSurface(renderer, surface);
if (!r) {
die("SDLCreateTextureFromSurface(): %s", SDL_GetError());
}
// free surface
SDL_FreeSurface(surface);
// return texture
return r;
}
SDL_Rect
sprites_get_rect(
const sprite_t sprite
) {
const int x = ((sprite < SPRITE_LAST) ? sprite : SPRITE_NONE) * 32;
return (SDL_Rect) { x, 0, 32, 32 };
}
|