Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run display autoinit on window API, fixes #2860

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src_c/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pg_display_quit(PyObject *self, PyObject *_null)

pg_mod_autoquit(IMPPREFIX "event");
pg_mod_autoquit(IMPPREFIX "time");
pg_mod_autoquit(IMPPREFIX "_window");
pg_mod_autoquit(IMPPREFIX "window");

if (SDL_WasInit(SDL_INIT_VIDEO)) {
SDL_QuitSubSystem(SDL_INIT_VIDEO);
Expand Down
26 changes: 22 additions & 4 deletions src_c/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "doc/sdl2_video_doc.h"
#include "doc/window_doc.h"

static int is_window_mod_init = 0;

#if !defined(__APPLE__)
static char *icon_defaultname = "pygame_icon.bmp";
static int icon_colorkey = 0;
Expand Down Expand Up @@ -759,6 +761,11 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs)
const char *_key_str;
int _value_bool;

// ensure display is init at this point, diplay init automatically calls
// the window init in this module
if (!pg_mod_autoinit(IMPPREFIX "display"))
return -1;

_kw = PyDict_New();
if (!_kw)
return -1;
Expand Down Expand Up @@ -954,6 +961,11 @@ window_from_display_module(PyTypeObject *cls, PyObject *_null)
return NULL;
}

// ensure display is init at this point, diplay init automatically calls
// the window init in this module
if (!pg_mod_autoinit(IMPPREFIX "display"))
return NULL;

SDL_Window *window = pg_GetDefaultWindow();
if (!window) {
return RAISE(pgExc_SDLError,
Expand Down Expand Up @@ -999,14 +1011,20 @@ window_repr(pgWindowObject *self)
static PyObject *
_window_internal_mod_init(PyObject *self, PyObject *_null)
{
SDL_AddEventWatch(_resize_event_watch, NULL);
if (!is_window_mod_init) {
SDL_AddEventWatch(_resize_event_watch, NULL);
is_window_mod_init = 1;
}
Py_RETURN_NONE;
}

static PyObject *
_window_internal_mod_quit(PyObject *self, PyObject *_null)
{
SDL_DelEventWatch(_resize_event_watch, NULL);
if (is_window_mod_init) {
SDL_DelEventWatch(_resize_event_watch, NULL);
is_window_mod_init = 0;
}
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -1083,9 +1101,9 @@ static PyMethodDef _window_methods[] = {
{"get_grabbed_window", (PyCFunction)get_grabbed_window, METH_NOARGS,
DOC_SDL2_VIDEO_GETGRABBEDWINDOW},
{"_internal_mod_init", (PyCFunction)_window_internal_mod_init, METH_NOARGS,
"auto initialize for _window module"},
"auto initialize for window module"},
{"_internal_mod_quit", (PyCFunction)_window_internal_mod_quit, METH_NOARGS,
"auto quit for _window module"},
"auto quit for window module"},
{NULL, NULL, 0, NULL}};

MODINIT_DEFINE(window)
Expand Down
Loading