Skip to content

Conversation

@ej-sanmartin
Copy link
Contributor

Description

Implements SDL_SetWindowIcon for Emscripten by converting the icon surface to a PNG data URL using the Canvas API. Chose PNG for universal browser support and smaller file sizes, and used Canvas for the conversion since it's the standard browser API for handling pixel data with built-in format conversion and encoding.

Existing Issue(s)

Fixes #14478

Testing

  • Builds successfully on MacOS 12.7.6
  • Test with a simple script, which uses this method and updates the favicon on the browser to a red square.

Screenshot:

Screen Shot 2025-11-20 at 2 05 51 AM

Script:

#include <SDL3/SDL.h>
#include <emscripten.h>

int main(int argc, char* argv[])
{
    if (!SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("SDL_Init failed: %s", SDL_GetError());
        return 1;
    }

    SDL_Window* window = SDL_CreateWindow("Icon Test", 640, 480, 0);
    if (!window) {
        SDL_Log("SDL_CreateWindow failed: %s", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    // Create a 32x32 red surface
    SDL_Surface* icon = SDL_CreateSurface(32, 32, SDL_PIXELFORMAT_RGBA32);
    if (!icon) {
        SDL_Log("SDL_CreateSurface failed: %s", SDL_GetError());
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    // Fill with bright red
    SDL_FillSurfaceRect(icon, NULL, SDL_MapSurfaceRGBA(icon, 255, 0, 0, 255));

    // Test SDL_SetWindowIcon - should change favicon to red square
    SDL_Log("Setting window icon...");
    if (SDL_SetWindowIcon(window, icon)) {
        SDL_Log("SUCCESS!");
    } else {
        SDL_Log("FAILED!",
                SDL_GetError());
    }

    SDL_DestroySurface(icon);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

@ej-sanmartin
Copy link
Contributor Author

OpenBSD Build Failure seems unrelated to my change:

Error Log shows this:

Can't install python-3.12.11 because of libraries
|library c.102.0 not found
| /usr/lib/libc.so.100.3 (system): bad major

@slouken
Copy link
Collaborator

slouken commented Nov 20, 2025

Does it help that SDL supports PNG natively now? You can use SDL_SavePNG_IO() with a dynamic memory stream to encode it if you want.

@ej-sanmartin
Copy link
Contributor Author

I'll check out that approach, I'll send out another commit later today.

@ej-sanmartin
Copy link
Contributor Author

Looking into this, yes SDL_SavePNG_IO() is a lot more robust and I see I can use SDL_IOFromDynamicMem() as dynamic memory stream for PNG encoding. I'll work on this and test/build locally.

@ej-sanmartin
Copy link
Contributor Author

Updated, builds successfully! SDL_SavePNG_IO() is a lot better.

@slouken slouken added this to the 3.4.0 milestone Nov 21, 2025
@slouken slouken merged commit be15de6 into libsdl-org:main Nov 21, 2025
43 checks passed
@slouken
Copy link
Collaborator

slouken commented Nov 21, 2025

Looks good! Merged, thanks!

@icculus
Copy link
Collaborator

icculus commented Nov 21, 2025

Thanks for jumping on this one, @ej-sanmartin! Looks great!

@ej-sanmartin
Copy link
Contributor Author

You're welcome, happy to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SDL_SetWindowIcon for Emscripten

3 participants