Search code examples
c++windowsexesdl-2

How to give an icon to a .exe in SDL2/C++?


Problem :

I am trying to give an icon to my SDL2 application. SDL2 have a function named SDL_SetWindowIcon(), but the problem is the function only set the icon in the window app not in the .exe file this mean that when I pin it in my Taskbar, I see this:

this icon

What I tried:

SDL2 function

SDL_Surface *iconSurface = IMG_Load("./src/Assets/img/Logo/icon.ico");
SDL_SetWindowIcon(window, iconSurface);
SDL_FreeSurface(iconSurface);

.rc file

MAIN ICON "File"

Important Details :

Makefile Content :

all:
    cls
    g++ -I src/include -L src/lib -o TKM main.cpp src/include/manmade/Class/* src/include/manmade/Components/* src/include/manmade/Part/* -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf
    ./TKM

Coding Environment:

OS : Windows
Text Editor : VScode 
Project build with a Makefile

Solution

  • Credits :

    It is @HolyBlackCat you respond to this question in the comment. I am just putting it in the answer section with more details.

    I add see some video and website to get knowledge in C++ and SDL2.

    Step 1

    Create a .rc file.

    Step 2

    Inside of the .rc file write this line of code:

    MAIN ICON "./thePath/of/yourImage.ico"
    

    Step 3(for simple Makefile)

    In the terminal launch this command:

    windres -i yourRCFileName.rc -o yourRCFileName.o
    

    Step 4(for simple Makefile)

    Put the path of your .o file(ex: yourRCFileName.o) after main.cpp in the Makefile to assign your icon to the .exe file:

    all:
        cls
        g++ -I src/include -L src/lib -o test main.cpp yourRCFileName.o -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf
        ./test
    

    Step 3(for big Makefile)

    It is like when you make it with .cpp file. When you make change in your .rc file it will launch the command(@windress ...) otherwise nothing happen.

    yourRCFileName.o: yourRCFileName.rc
        @windres -i $^ -o $@
    

    Step 4(for big Makefile)

    You put the path of your .o file in the right section like always:

    stackoverflowApp.exe: main.o yourRCFileName.o
        g++ -std=c++20 -I src/include -L src/lib $^ -o $@ -lmingw32 -otherLibraires
        ./stackoverflowApp
    

    Important Details :

    The image need to be a .ico or you are going to get the following error message:

    C:\mingw64\bin\windres.exe: icon file `./src/Assets/img/apple.png' does not contain icon data
    

    Tips :

    You can use a website such as https://icoconvert.com/ to convert your image but they accept only these image :

    PNG, JPG or BMP image( maximum size: 50 MB )