Can you store an icon inside a datafile and use it as the program icon?
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.
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...
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).
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.
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.
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.
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.
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.
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
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.
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.
That is just the definition of the stuff in the resource file
Wanna bet?
1 | #include <allegro.h> |
2 | /* XPM */ |
3 | static 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 |
61 | extern void *allegro_icon; |
62 | CONSTRUCTOR_FUNCTION(static void _set_allegro_icon(void)); |
63 | static 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...
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.
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.
That doesn't work on windows, hence the program, if I ran linux, I could have done that.
Oh, sorry. I forgot. Windows must be deliberately complicated.
Wanna bet?
Yes. Now look in the resource file. I bet you it references the icon as well.
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 )
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.
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.
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.
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.