Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Icons

This thread is locked; no one can reply to it. rss feed Print
Icons
SkaxCo
Member #8,323
February 2007

Can you store an icon inside a datafile and use it as the program icon?

BAF
Member #2,981
December 2002
avatar

Not to my knowledge. You should be able to use the fixicon program offered by Allegro and link with the generated object file to gain an icon.

James Stanley
Member #7,275
May 2006
avatar

You can but it's difficult and I haven't tried it.
Firstly, you can't save an icon file. You must save the text that is stored in the icon header file that fixicon generates, then you could perhaps load it at run time. But there's not really a lot of point. My method isn't thought through very well, and it probably won't work, but hey...

Ron Ofir
Member #2,357
May 2002
avatar

What? I don't think you can set the icon using code, I think you must add it to the header of the .exe file (using resources or whatever).

BAF
Member #2,981
December 2002
avatar

What? Wfixicon gives you a resource file, and if you tell it to it will compile it for you. Then you just link with the object file.

Ron Ofir
Member #2,357
May 2002
avatar

Exactly, so it has nothing to do with datafiles, that, and you can save an icon file and then associate it later using wfixicon or simply the proprties option.

BAF
Member #2,981
December 2002
avatar

Wfixicon will work with datafiles. But the icon has to be compiled in as a resource, you can't just have it reference the datafile for the icon. Unless you make a shortcut, but if you want the exe itself to bear the icon, using a resource file is your only option, and wfixicon makes it easier.

Timorg
Member #2,028
March 2002

Not really the solution you want, but I have code from an old project that can generate icons on the fly, allowing you to convert a allegro bitmap to an icon, then use that. I need to clean it up, as the project is a mess, I am going to clean it up now, which will take a little while. When its together I will post it here. :)

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

James Stanley
Member #7,275
May 2006
avatar

I'm sure that if you look in the header file that fixicon gives you, it just has some text that is the icon. Load the text out of a datafile and you've pretty much done the same thing.

Timorg
Member #2,028
March 2002

Here is a working of example of a bitmap that is converted to a HBITMAP, which is converted to a HICON which is then sent as a message to the window. (I am pretty sure when you SendMessage, it makes it own copy of the icon)

There is a code::blocks project file, and a makefile in the archive to build the example.

To use this all you have to do is:

HBITMAP HBImage = convert_bitmap_to_hbitmap(image);
HICON TEST = CreateIcon(HBImage, 1, 1);
DeleteObject(HBImage);
SendMessage(  win_get_window(), (UINT) WM_SETICON, (WPARAM) ICON_SMALL, (LPARAM) TEST);

The code works, but I am sure it could be improved. :)

One of the attached files is broken. I'm not sure which one, and you can't delete attachements, Just grab it from http://www.timorg.net/create_icon.zip :)

It just occurred to me you might not have meant windows, but unix has "extern void *allegro_icon;" to do it

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

BAF
Member #2,981
December 2002
avatar

Quote:

I'm sure that if you look in the header file that fixicon gives you, it just has some text that is the icon. Load the text out of a datafile and you've pretty much done the same thing.

That is just the definition of the stuff in the resource file. You likely don't even need to touch it.

Also, Timorg's method changes the icon of the running program, wfixicon will keep that icon when the program is not running.

Timorg
Member #2,028
March 2002

I guess it depends on what they mean by 'program icon', I assumed they meant window icon. You know what they say when you assume something, makes and ass out of you and me. :P

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

James Stanley
Member #7,275
May 2006
avatar

Quote:

That is just the definition of the stuff in the resource file

Wanna bet?

1#include <allegro.h>
2/* XPM */
3static const char *allegico_xpm[] = {
4/* columns rows colors chars-per-pixel */
5"48 48 4 1",
6" c magenta",
7". c navy",
8"X c #808000000000",
9"o c None",
10/* pixels */
11"oooooooooooooooooooooooooooo...ooooooooooooooooo",
12"ooooooooooooooooooooooo.....oooooooXXXXXXXoooooo",
13"oooo.......oooooo......ooooooooooXXXXXXXXXXXoooo",
14"oo...........o...ooooooooo...oooXXXXXXXXXXXXXooo",
15"o.............oooooooo....ooooooXXXXXXXXXXXXXooo",
16"o.............oooo....oooooooooXXXXXXXXXXXXXXXoo",
17"...............o..ooooo....ooooXXXXXXXXXXXXXXXoo",
18"...............oooo....ooooooooXXXXXXXXXXXXXXXoo",
19"...............o...ooooooooooooXXXXXXXXXXXXXXXoo",
20"...............ooooooooo..oooooXXXXXXXXXXXXXXXoo",
21"...............oooooo...oooooooXXXXXXXXXXXXXXXoo",
22"...............ooo...ooooooooooXXXXXXXXXXXXXXXoo",
23"...............o..ooooo..oooooooXXXXXXXXXXXXXooo",
24"o.............oooooooo..ooooooooXXXXXXXXXXXXXooo",
25"o.............ooooo...oooooooooooXXXXXXXXXXXoooo",
26"oo...........oooo..oooo.ooXXXooooooXXXXXXXoooooo",
27"oooo.......ooo....oooo..ooXXXXXoooooooXXXooooooo",
28"oooooo...ooo..ooooooooooooXXXXXXXXooooXXXoooXXXX",
29"oooooo...o..ooooo....ooooooXXXXXXXXXoXXXXooooooo",
30"oooooo...ooooooo.....ooooXXXXXXXXXXXXXXXXooooooo",
31"oooooo...ooooo.......ooooXXXXXXXXXXXXXXXoooooooo",
32"oooooo...ooo........oooooXXXXXXXXXXXXXXXoooXXXXo",
33"oooooo............oooooooooooooXXXXXXXXXoooooooX",
34"oooooo..........ooooooXXXooooooooooXXXXooooooooo",
35"oooooo.........oooooooXXXXoooooooooXXXXoXXoooooo",
36"ooooooo.......ooooooooXXXXXoooooooXXXXooooXXXooo",
37"ooooooo.............ooXXXXXooooooXXXXXoooooooXXX",
38"ooooooo...............oXXXXXoooooXXXXooooooooooo",
39"ooooooo...oo..........ooXXXXoooooXXXXooooooooooo",
40"ooooooo...oooooooo....ooXXXXXoooXXXXooooXooooooo",
41"ooooooo...o....ooooooooooXXXXXoXXXXXoooooXXooooo",
42"ooooooo...oooooooooooooooXXXXXXXXXXooooooooXXXoo",
43"ooooooo...oooo.ooXXXXXooooXXXXXXXXXoooooooooooXX",
44"ooooooo......ooooXXXXXXXXXoXXXXXXXooooXXXXoooooo",
45"ooooooo.......oooXXXXXXXXXXXXXXXXXoooXooooXXXXoo",
46"oooooooo.........ooXXXXXXXXXXXXXXoooooooooooooXX",
47"oooooooo..........oooooXXXXXXXXXXooooooooooooooo",
48"oooooooo.............oooooooXXXXXooXXooooooooooo",
49"ooooooooo.............oooooooooooooooXXXXXoooooo",
50"oooooooooo.............ooo..ooooooooooooooXXXXoo",
51"oooooooooo......oo.......ooo....oooooooooooooXXX",
52"oooooooooooo.....oo.......oooooo....oooooooooooo",
53"ooooooooooooo....oooo......ooooooooooooooooooooo",
54"oooooooooooooo...ooooo.....ooooooooooooooooooooo",
55"oooooooooooooo...oooooo....ooo................oo",
56"oooooooooooooo...ooooooo...ooooooooooooooooooooo",
57"oooooooooooooo...oo...oooooooooooooooooooooooooo",
58"oooooooooooooo...oooo.oooooooooooooooooooooooooo"
59};
60#if defined ALLEGRO_WITH_XWINDOWS && defined ALLEGRO_USE_CONSTRUCTOR
61extern void *allegro_icon;
62CONSTRUCTOR_FUNCTION(static void _set_allegro_icon(void));
63static void _set_allegro_icon(void)
64{
65 allegro_icon = allegico_xpm;
66}
67#endif

That doesn't look much like a definition of a resource file...

Timorg
Member #2,028
March 2002

Cute icon, had to write a program to view it (which is attached), from the text version it almost looked like the figures were having sex.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

James Stanley
Member #7,275
May 2006
avatar

You don't need to write a program to view it...
Just use that as icon.h in your project. That's how it works.

Timorg
Member #2,028
March 2002

That doesn't work on windows, hence the program, if I ran linux, I could have done that.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

James Stanley
Member #7,275
May 2006
avatar

Oh, sorry. I forgot. Windows must be deliberately complicated.

BAF
Member #2,981
December 2002
avatar

Quote:

Wanna bet?

Yes. Now look in the resource file. I bet you it references the icon as well.

Timorg
Member #2,028
March 2002

Assuming you have a icon, to set it as the window icon

SendMessage(  win_get_window(), (UINT) WM_SETICON, (WPARAM) ICON_SMALL, (LPARAM) icon);

doesn't seem all that complicated to me.

I guess it might seem complicated that you need to ask the application to set the icon to something, rather than just setting it, but it adds thread safety.

Windows uses a different format for icons, that what allegro uses for bitmaps, so they need converting. I don't see that microsoft was being "deliberately complicated" when they don't use bitmaps for icons. Icons and cursors are in the same format, how do you propose they store the x and y for the hotspot of the cursor in a bitmap. Or should they have 2 different formats, one for icons and one for cursors, and how can you store multiple sized versions of an icon in a bitmap?

(yes I am aware I am feeding a troll :P)

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

BAF
Member #2,981
December 2002
avatar

Also, Timorg, if you simply use it in the resource file, the exe will then have that icon, and Windows will show it for the window by default.

James Stanley
Member #7,275
May 2006
avatar

Quote:

Yes. Now look in the resource file. I bet you it references the icon as well.

There isn't a resource file. That header is all that there is. I had forgotten that Windows is more complicated than it need be.

Timorg
Member #2,028
March 2002

I use a default rc for for all my programs, so they all have the same icon. But there are other cases where you might like to use a custom icon for the window. If your game is modded by providing a datafile that has the levels and art and everything in it, when you load the datafile, you might like to load a mods icon to the window icon (Which is possibly what the OP wanted). Or taking gimps example, a copy of the current bitmap is used for the editors window icon. Or you want to use allegro to generate tray icons, that change to show what is happening while your program is minimized.

As long as your not expecting it to change your programs executable icon, this can be really useful.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

BAF
Member #2,981
December 2002
avatar

Well, wfixicon doesn't work on Linux. But it most definitely gives you a resource file, and if you tell it to it will even compile it for you.

Go to: