Skip to content

Commit 3d395a4

Browse files
committed
set_display_icon(): add custom logic for Windows
1 parent eaa51da commit 3d395a4

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

src/core/video.c

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
#include <allegro5/allegro_memfile.h>
2727
#include <allegro5/allegro_opengl.h>
2828

29-
#ifdef ALLEGRO_UNIX
30-
#include <allegro5/allegro_x.h>
29+
#if defined(ALLEGRO_UNIX)
30+
# include <allegro5/allegro_x.h>
31+
#elif defined(ALLEGRO_WINDOWS)
32+
# include <allegro5/allegro_windows.h>
3133
#endif
3234

3335
#include <stdio.h>
@@ -1047,6 +1049,51 @@ void compute_display_transform(ALLEGRO_TRANSFORM* transform)
10471049
/* sets the icon of the display to a built-in icon */
10481050
void set_display_icon(ALLEGRO_DISPLAY* display)
10491051
{
1052+
#if defined(ALLEGRO_WINDOWS)
1053+
1054+
/*
1055+
* Read the icon from the .exe file
1056+
*/
1057+
1058+
if(display == NULL)
1059+
return;
1060+
1061+
LPCSTR name = "GAME_ICON";
1062+
HWND hwnd = al_get_win_window_handle(display);
1063+
HINSTANCE hinstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
1064+
1065+
HICON big_icon = (HICON)LoadImage(hinstance, name, IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
1066+
if(big_icon != NULL) {
1067+
1068+
/* Why call CopyIcon()?
1069+
Because we must not destroy a shared resource. Allegro calls DestroyIcon()
1070+
when destroying the display. See _al_win_destroy_display_icons() at
1071+
src/win/wgl_disp.c - verified for Allegro 5.2.11. */
1072+
1073+
HICON new_icon = CopyIcon(big_icon);
1074+
if(new_icon != NULL) {
1075+
HICON old_icon = (HICON)SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)new_icon);
1076+
if(old_icon != NULL)
1077+
DestroyIcon(old_icon);
1078+
}
1079+
}
1080+
1081+
HICON small_icon = (HICON)LoadImage(hinstance, name, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
1082+
if(small_icon != NULL) {
1083+
HICON new_icon = CopyIcon(small_icon);
1084+
if(new_icon != NULL) {
1085+
HICON old_icon = (HICON)SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)new_icon);
1086+
if(old_icon != NULL)
1087+
DestroyIcon(old_icon);
1088+
}
1089+
}
1090+
1091+
#else
1092+
1093+
/*
1094+
* Use a built-in icon of Surge
1095+
*/
1096+
10501097
extern const unsigned char ICON_PNG[];
10511098
extern const size_t ICON_SIZE;
10521099
ALLEGRO_FILE* f = al_open_memfile((void*)ICON_PNG, ICON_SIZE, "rb");
@@ -1055,13 +1102,15 @@ void set_display_icon(ALLEGRO_DISPLAY* display)
10551102
if(display != NULL)
10561103
al_set_display_icon(display, icon);
10571104

1058-
#if defined(ALLEGRO_UNIX) && !defined(ALLEGRO_RASPBERRYPI)
1105+
# if defined(ALLEGRO_UNIX) && !defined(ALLEGRO_RASPBERRYPI)
10591106
if(display == NULL)
10601107
al_x_set_initial_icon(icon);
1061-
#endif
1108+
# endif
10621109

10631110
al_destroy_bitmap(icon);
10641111
al_fclose(f);
1112+
1113+
#endif
10651114
}
10661115

10671116
/* handle a video event from Allegro */

0 commit comments

Comments
 (0)