From 63215211350215c2971090cc0e3807e8941a0e37 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 16 Jan 2019 08:02:30 -0500 Subject: add window icon --- src/sdl/main.c | 7 +++++-- src/sdl/sprites.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/sdl/sprites.h | 1 + 3 files changed, 52 insertions(+), 2 deletions(-) (limited to 'src/sdl') diff --git a/src/sdl/main.c b/src/sdl/main.c index 8e96fa5..82d6682 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -14,6 +14,8 @@ #include "assets.h" #include "solve.h" +#define WINDOW_TITLE "Pablotron Sokoban" + static TTF_Font * load_font( const asset_id_t id @@ -134,7 +136,7 @@ int main(int argc, char *argv[]) { // create window SDL_Window *win = SDL_CreateWindow( - "Sokoban", + WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, @@ -168,8 +170,9 @@ int main(int argc, char *argv[]) { // set level set_level(&draw_ctx, &ctx, level_num); - // init sprites, load font + // init sprites, set window icon, load font sprites_init(renderer, draw_ctx.sprites); + sprites_set_window_icon(win, SPRITE_HOME); draw_ctx.font = load_font(ASSET_ROBOTO_TTF); // register solve event diff --git a/src/sdl/sprites.c b/src/sdl/sprites.c index 51824d8..a64d011 100644 --- a/src/sdl/sprites.c +++ b/src/sdl/sprites.c @@ -67,3 +67,49 @@ sprites_get_rect( 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 icon_sprite +) { + // 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()); + } + + // 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()); + } + + // set window icon + SDL_SetWindowIcon(win, icon_surface); + + // free icon surface + SDL_FreeSurface(icon_surface); + + // free main surface + SDL_FreeSurface(src); + + // free image data + stbi_image_free(im_data); +} diff --git a/src/sdl/sprites.h b/src/sdl/sprites.h index cd71e7c..d0f025f 100644 --- a/src/sdl/sprites.h +++ b/src/sdl/sprites.h @@ -16,5 +16,6 @@ typedef enum { void sprites_init(SDL_Renderer * const, SDL_Texture **); SDL_Rect sprites_get_rect(const sprite_t); +void sprites_set_window_icon(SDL_Window * const, const sprite_t); #endif /* SPRITES_H */ -- cgit v1.2.3