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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#include <SDL.h>
#define STB_NO_STDIO
#define STB_IMAGE_IMPLEMENTATION
#define STB_ASSERT(x)
#define STB_ONLY_PNG
#include "stb_image.h"
#include "util.h"
#include "sprites.h"
#include "assets.h"
typedef struct {
unsigned char *im_data;
SDL_Surface *surface;
} sprite_set_t;
static sprite_set_t
sprite_set_init(
const asset_id_t asset_id
) {
sprite_set_t set;
// get asset
const asset_t * const asset = asset_get(asset_id);
// load backing image data
int im_w, im_h;
set.im_data = stbi_load_from_memory(asset->buf, asset->len, &im_w, &im_h, NULL, 4);
if (!set.im_data) {
die("stbi_load(): %s", stbi_failure_reason());
}
// create surface from backing image data
set.surface = SDL_CreateRGBSurfaceWithFormatFrom(set.im_data, im_w, im_h, 32, 4 * im_w, SDL_PIXELFORMAT_RGBA32);
if (!set.surface) {
die("SDLCreateRGBSurfaceFrom(): %s", SDL_GetError());
}
// return sprite set
return set;
}
static void
sprite_set_fini(
const sprite_set_t * const set
) {
// free main surface
SDL_FreeSurface(set->surface);
// free backing image data
stbi_image_free(set->im_data);
}
static SDL_Surface *
sprite_set_get_sprite_surface(
const sprite_set_t * const set,
const sprite_t icon_sprite
) {
// create surface
SDL_Surface *r = SDL_CreateRGBSurfaceWithFormat(0, 32, 32, 32, SDL_PIXELFORMAT_RGBA32);
if (!r) {
die("SDL_CreateRGBSurfaceWithFormat(): %s", SDL_GetError());
}
// copy from main surface
const SDL_Rect src_rect = { icon_sprite * 32, 0, 32, 32 };
if (SDL_BlitScaled(set->surface, &src_rect, r, NULL)) {
die("SDL_BlitScaled(): %s", SDL_GetError());
}
// return surface
return r;
}
void
sprites_init(
SDL_Renderer * const renderer,
SDL_Texture **sprites
) {
// init sprite set from asset
sprite_set_t set = sprite_set_init(ASSET_SPRITES_PNG);
// create sprite textures
for (size_t i = 0; i < SPRITE_LAST; i++) {
// create surface
SDL_Surface *s = sprite_set_get_sprite_surface(&set, i);
// create texture
sprites[i] = SDL_CreateTextureFromSurface(renderer, s);
if (!sprites[i]) {
die("SDLCreateTextureFromSurface(): %s", SDL_GetError());
}
// free surface
SDL_FreeSurface(s);
}
// fini sprite set
sprite_set_fini(&set);
}
SDL_Rect
sprites_get_rect(
const sprite_t sprite
) {
const int x = ((sprite < SPRITE_LAST) ? sprite : SPRITE_NONE) * 32;
return (SDL_Rect) { .x = x, .y = 0, .w = 32, .h = 32 };
}
void
sprites_set_window_icon(
SDL_Window * const win,
const sprite_t sprite_id
) {
// init sprite set from asset
sprite_set_t set = sprite_set_init(ASSET_SPRITES_PNG);
// create icon surface
SDL_Surface *surface = sprite_set_get_sprite_surface(&set, sprite_id);
// set window icon
SDL_SetWindowIcon(win, surface);
// free icon surface
SDL_FreeSurface(surface);
// fini sprite set
sprite_set_fini(&set);
}
|