diff options
Diffstat (limited to 'src/sdl')
-rw-r--r-- | src/sdl/sprites.c | 126 |
1 files changed, 69 insertions, 57 deletions
diff --git a/src/sdl/sprites.c b/src/sdl/sprites.c index a64d011..4f3360d 100644 --- a/src/sdl/sprites.c +++ b/src/sdl/sprites.c @@ -8,40 +8,80 @@ #include "sprites.h" #include "assets.h" -void -sprites_init( - SDL_Renderer * const renderer, - SDL_Texture **sprites +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_SPRITES_PNG); + const asset_t * const asset = asset_get(asset_id); // load image int im_w, im_h; - unsigned char *im_data = stbi_load_from_memory(asset->buf, asset->len, &im_w, &im_h, NULL, 4); - if (!im_data) { + 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 - SDL_Surface * const src = SDL_CreateRGBSurfaceWithFormatFrom(im_data, im_w, im_h, 32, 4 * im_w, SDL_PIXELFORMAT_RGBA32); - if (!src) { + 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 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 = SDL_CreateRGBSurfaceWithFormat(0, 32, 32, 32, SDL_PIXELFORMAT_RGBA32); - if (!s) { - die("SDL_CreateRGBSurfaceWithFormat(): %s", SDL_GetError()); - } - - // copy from main surface - const SDL_Rect src_rect = { i * 32, 0, 32, 32 }; - if (SDL_BlitScaled(src, &src_rect, s, NULL)) { - die("SDL_BlitScaled(): %s", SDL_GetError()); - } + SDL_Surface *s = sprite_set_get_sprite_surface(&set, i); // create texture sprites[i] = SDL_CreateTextureFromSurface(renderer, s); @@ -53,11 +93,8 @@ sprites_init( SDL_FreeSurface(s); } - // free main surface - SDL_FreeSurface(src); - - // free image data - stbi_image_free(im_data); + // fini sprite set + sprite_set_fini(&set); } SDL_Rect @@ -71,45 +108,20 @@ sprites_get_rect( void sprites_set_window_icon( SDL_Window * const win, - const sprite_t icon_sprite + const sprite_t sprite_id ) { - // get asset - const asset_t * const asset = asset_get(ASSET_SPRITES_PNG); - - // load image - int im_w, im_h; - unsigned char *im_data = stbi_load_from_memory(asset->buf, asset->len, &im_w, &im_h, NULL, 4); - if (!im_data) { - die("stbi_load(): %s", stbi_failure_reason()); - } - - // create surface - SDL_Surface * const src = SDL_CreateRGBSurfaceWithFormatFrom(im_data, im_w, im_h, 32, 4 * im_w, SDL_PIXELFORMAT_RGBA32); - if (!src) { - die("SDLCreateRGBSurfaceFrom(): %s", SDL_GetError()); - } + // init sprite set from asset + sprite_set_t set = sprite_set_init(ASSET_SPRITES_PNG); // create icon surface - SDL_Surface *icon_surface = SDL_CreateRGBSurfaceWithFormat(0, 32, 32, 32, SDL_PIXELFORMAT_RGBA32); - if (!icon_surface) { - die("SDL_CreateRGBSurfaceWithFormat(): %s", SDL_GetError()); - } - - // copy from main surface - const SDL_Rect src_rect = { icon_sprite * 32, 0, 32, 32 }; - if (SDL_BlitScaled(src, &src_rect, icon_surface, NULL)) { - die("SDL_BlitScaled(): %s", SDL_GetError()); - } + SDL_Surface *surface = sprite_set_get_sprite_surface(&set, sprite_id); // set window icon - SDL_SetWindowIcon(win, icon_surface); + SDL_SetWindowIcon(win, surface); // free icon surface - SDL_FreeSurface(icon_surface); + SDL_FreeSurface(surface); - // free main surface - SDL_FreeSurface(src); - - // free image data - stbi_image_free(im_data); + // fini sprite set + sprite_set_fini(&set); } |