Skip to content

Commit ae11dfb

Browse files
committed
add icon when opening a window
1 parent 397e51f commit ae11dfb

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

src_c/window.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,92 @@
66

77
#include "doc/sdl2_video_doc.h"
88

9+
// Copied from display.c
10+
#if !defined(__APPLE__)
11+
static char *icon_defaultname = "pygame_icon.bmp";
12+
static int icon_colorkey = 0;
13+
#else
14+
static char *icon_defaultname = "pygame_icon_mac.bmp";
15+
static int icon_colorkey = -1;
16+
#endif
17+
18+
static char *pkgdatamodule_name = "pygame.pkgdata";
19+
static char *imagemodule_name = "pygame.image";
20+
static char *resourcefunc_name = "getResource";
21+
static char *load_basicfunc_name = "load_basic";
22+
23+
// Copied from display.c
24+
static void
25+
pg_close_file(PyObject *fileobj)
26+
{
27+
PyObject *result = PyObject_CallMethod(fileobj, "close", NULL);
28+
if (result) {
29+
Py_DECREF(result);
30+
}
31+
else {
32+
PyErr_Clear();
33+
}
34+
}
35+
36+
// Copied from display.c
37+
static PyObject *
38+
pg_display_resource(char *filename)
39+
{
40+
PyObject *imagemodule = NULL;
41+
PyObject *load_basicfunc = NULL;
42+
PyObject *pkgdatamodule = NULL;
43+
PyObject *resourcefunc = NULL;
44+
PyObject *fresult = NULL;
45+
PyObject *result = NULL;
46+
PyObject *name = NULL;
47+
48+
pkgdatamodule = PyImport_ImportModule(pkgdatamodule_name);
49+
if (!pkgdatamodule)
50+
goto display_resource_end;
51+
52+
resourcefunc = PyObject_GetAttrString(pkgdatamodule, resourcefunc_name);
53+
if (!resourcefunc)
54+
goto display_resource_end;
55+
56+
imagemodule = PyImport_ImportModule(imagemodule_name);
57+
if (!imagemodule)
58+
goto display_resource_end;
59+
60+
load_basicfunc = PyObject_GetAttrString(imagemodule, load_basicfunc_name);
61+
if (!load_basicfunc)
62+
goto display_resource_end;
63+
64+
fresult = PyObject_CallFunction(resourcefunc, "s", filename);
65+
if (!fresult)
66+
goto display_resource_end;
67+
68+
name = PyObject_GetAttrString(fresult, "name");
69+
if (name != NULL) {
70+
if (PyUnicode_Check(name)) {
71+
pg_close_file(fresult);
72+
Py_DECREF(fresult);
73+
fresult = name;
74+
name = NULL;
75+
}
76+
}
77+
else {
78+
PyErr_Clear();
79+
}
80+
81+
result = PyObject_CallFunction(load_basicfunc, "O", fresult);
82+
if (!result)
83+
goto display_resource_end;
84+
85+
display_resource_end:
86+
Py_XDECREF(pkgdatamodule);
87+
Py_XDECREF(resourcefunc);
88+
Py_XDECREF(imagemodule);
89+
Py_XDECREF(load_basicfunc);
90+
Py_XDECREF(fresult);
91+
Py_XDECREF(name);
92+
return result;
93+
}
94+
995
static PyTypeObject pgWindow_Type;
1096

1197
#define pgWindow_Check(x) \
@@ -553,6 +639,19 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs)
553639

554640
SDL_SetWindowData(_win, "pg_window", self);
555641

642+
PyObject *icon = pg_display_resource(icon_defaultname);
643+
if (!icon) {
644+
return -1;
645+
}
646+
if (icon_colorkey != -1) {
647+
if (SDL_SetColorKey(pgSurface_AsSurface(icon), SDL_TRUE,
648+
icon_colorkey) < 0) {
649+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
650+
return -1;
651+
}
652+
}
653+
SDL_SetWindowIcon(self->_win, pgSurface_AsSurface(icon));
654+
556655
return 0;
557656
}
558657

0 commit comments

Comments
 (0)