Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Set icon from resource  (Read 5975 times)

0 Members and 1 Guest are viewing this topic.

Romz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Set icon from resource
« on: August 31, 2013, 12:28:23 am »
Visual Studio let us import an icon into the resource and when building it the icon will be used for the program's icon (the actual executable icon) but in order to set the icon on the window title bar (left side) and for when we do alt-tab, we need to set it at runtime.

I didn't find a way to set the icon with sfml with icon being in the resources (not loading an external .ico or .png file).

So I've done this for windows:

#ifdef WIN32
    HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
    if(hIcon)
    {
        SendMessage(windowgetSystemHandle(), WM_SETICON, ICON_BIG, (LPARAM)hIcon);
    }
#endif

Is there a way to do this with SFML? I don't know if it can be done with Linux, I could add a #else and do it for Linux but I'd rather not if SFML can or will do it.

Any word?

Thanks!

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Set icon from resource
« Reply #1 on: August 31, 2013, 12:34:56 am »
Well the window class does have a setIcon function, but you're right that there's no way to load it from an embedded resource.

In fact when I researched how icons work on different platforms it sounded like embedding the icon is unique to Windows, while Mac and Linux expect the icons to be entirely separate files (though on a Mac they'd still be part of the same "bundle").
« Last Edit: August 31, 2013, 12:38:28 am by Ixrec »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Set icon from resource
« Reply #2 on: August 31, 2013, 01:33:45 am »
Since resource embedding is extremely OS and compiler specific and since there are often multiple ways of doing it, SFML can't provide a general API for loading from such code, however the setIcon function only requires a pointer to an sf::Uint8 array (pixel array) and the size of said array, thus if you find a way to read the pixel information of the embedded icon, you'll be able to transform it and set it with SFML's function. Also keep in mind that sf::Image has a function loadFromMemory(), thus if you find the memory location of the icon, you might be able to simply call that function and SFML will create an image as if it would've read it from the harddisk, then you can call getPixelPtr and pass that to the setIcon function. ;)

With Linux or essentially GCC (I think), you can just add a file to the compiling line and let it get built into the binary. I'm not sure how'd get the address to read the file from that location, but if you find a way to get to that image location, you should be able to load it into an sf::Image as described above.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Set icon from resource
« Reply #3 on: August 31, 2013, 03:23:16 am »
When I tried that I quickly discovered that Windows (haven't tried on Mac or Linux) kind of expects more than one icon size to be available, so when you use setIcon the other size ends up being a badly upscaled or downscaled version version of the one you did provide.  So the icon might look fine in the window's titlebar but terrible in the taskbar, for instance.

For me this defeats the purpose of having an icon in the first place (ie, making the program look "professional") so personally I'm avoiding icons until I feel ready to write platform specific code for each OS to deal with different icon sizes.  But maybe Romz doesn't mind it as much.
« Last Edit: August 31, 2013, 03:25:18 am by Ixrec »

Romz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Set icon from resource
« Reply #4 on: August 31, 2013, 03:42:25 am »
Thanks for the answers.

I haven't researched how to embed an image in the binary with gcc yet, but I know that at least the icon can be stored as a C array and obviously compiled into the binary, then it's easier to get the address.

Searching for set icon on this forum it seems it's a largely unsolved problem, cross platform but even just windows (embedded resource).

If nothing quick and simple comes up I won't spend too much time on that for linux.

Edit: if anybody wonder, here is where and how I used it for Windows: https://github.com/shazbits/wiretap/blob/master/src/src/ProfileViewer/main.cpp

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Set icon from resource
« Reply #5 on: August 31, 2013, 04:20:36 am »
Again, I'm pretty sure that icon embedding just isn't done on Linux and Mac to begin with.  Everything I've read says those OS's look for the icons as separate files in some predefined location.

For instance, http://stackoverflow.com/questions/2126545/embedding-an-icon-in-a-linux-executable says that Linux icons are done via a "desktop entry."

 

anything