We hereby grant permission for SDL to support X11 PNG icons

I am too lazy to learn how to commit to the official SDL2 repository, as I am used to github.

SDL2 devs: I have contacted the other two contributors to this code and they agreed to relicense it under zlib for X11 icon support to be added to SDL2. Feel free to use and alter the contents of this github gist, but do not remove the copyright holders from the code comment at the top: https://gist.github.com/time-killer-games/6a69ae54ef8a58beb056021ec6c5544e

This could potentially be used as a stepping stone to getting SDL’s Wayland support to allow for the same. I’m not certain, but I believe it would rely on the same basic code logic and libpng.

Whether you actually, officially, add it to SDL2, is up to you SDL2 devs, but I believe it is in your best interest, as it is just a matter of copy/paste into your repository and maybe write 2 lines.


SDL2 can already set the window icon in X11, with SDL_SetWindowIcon - or does this do anything else?
What’s the advantage over what SDL already supports (on all platforms that have window icons)?

lol I don’t know how we missed that. Nevermind, I guess.

What is different about my code is that it loads the PNG file and adds it to the window all in one simple function call. What SDL does is a bit overcomplicated as you have to create a surface from an array of pixel data that already exists, which also means an image has to be converted to that array manually, and data has to be freed and a bunch of stuff that in my opinion should all be handled internally (and optionally done manually how it is for people who want to load from non-png formats or have the array hardcoded).

But none of this is truly necessary. It’s just a question of whether SDL does all this stuff for the user in one line, which limits how powerful the library can be, or allowing the user to write more on their own which increases the possibilities, or allowing an option for both which might be considered unnecessary bloat.

IMG_Load (part of the SDL2_Image library) can load a PNG filepath into a SDL_Surface with one function call.


Yep, and (shameless self-plug incoming) so does STBIMG_Load() of SDL_stbimage.h if you want something a bit leaner with no external dependencies (except for stb_image.h).

Either way I think adding png support (and a dependency to libpng) to SDL itself would be a bit overkill, as many SDL users probably don’t need it - and those who do can use SDL_Image/SDL_stbimage.

BTW, I personally find it useful to integrate the window icon in the executable - Gimp allows exporting images as C source code which makes this easy. I wrote an article about that a while ago: https://blog.gibson.sh/2015/04/13/how-to-integrate-your-sdl2-window-icon-or-any-image-into-your-executable/

1 Like