Skip to content

Commit

Permalink
Add auto resize event watch when pygame.init()
Browse files Browse the repository at this point in the history
  • Loading branch information
yunline committed Oct 26, 2023
1 parent 018b27c commit 39e36e5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
34 changes: 34 additions & 0 deletions src_c/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,34 @@ pg_mod_autoquit(const char *modname)
Py_XDECREF(funcobj);
}

// Callback function for surface auto resize
static int SDLCALL
_pg_window_surface_auto_resize_event_watch(void *userdata, SDL_Event *event)
{
pgWindowObject *event_window_pg;
SDL_Window *event_window;
if ((event->type != SDL_WINDOWEVENT))
return 0;
if (event->window.event != SDL_WINDOWEVENT_SIZE_CHANGED)
return 0;
event_window = SDL_GetWindowFromID(event->window.windowID);
event_window_pg = SDL_GetWindowData(event_window, "pg_window");

if (!event_window_pg)
return 0;

if (event_window_pg->_is_borrowed) {
// have been handled by event watch in display.c
return 0;
}

if (!event_window_pg->surf)
return 0;

event_window_pg->surf->surf = SDL_GetWindowSurface(event_window);
return 0;
}

static PyObject *
pg_init(PyObject *self, PyObject *_null)
{
Expand Down Expand Up @@ -352,6 +380,9 @@ pg_init(PyObject *self, PyObject *_null)
}
}

// add auto resize event watch
SDL_AddEventWatch(_pg_window_surface_auto_resize_event_watch, NULL);

pg_is_init = 1;
return Py_BuildValue("(ii)", success, fail);
}
Expand Down Expand Up @@ -443,6 +474,9 @@ _pg_quit(void)
Py_DECREF(privatefuncs);
}

// remove auto resize event watch
SDL_DelEventWatch(_pg_window_surface_auto_resize_event_watch, NULL);

/* quit all pygame modules */
for (i = 0; modnames[i]; i++) {
pg_mod_autoquit(modnames[i]);
Expand Down
30 changes: 0 additions & 30 deletions src_c/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,34 +174,6 @@ window_update_from_surface(pgWindowObject *self)
Py_RETURN_NONE;
}

// Callback function for surface auto resize
static int SDLCALL
_resize_event_watch(void *userdata, SDL_Event *event)
{
pgWindowObject *event_window_pg;
SDL_Window *event_window;
if ((event->type != SDL_WINDOWEVENT))
return 0;
if (event->window.event != SDL_WINDOWEVENT_SIZE_CHANGED)
return 0;
event_window = SDL_GetWindowFromID(event->window.windowID);
event_window_pg = SDL_GetWindowData(event_window, "pg_window");

if (!event_window_pg)
return 0;

if (event_window_pg->_is_borrowed) {
// have been handled by event watch in display.c
return 0;
}

if (!event_window_pg->surf)
return 0;

event_window_pg->surf->surf = SDL_GetWindowSurface(event_window);
return 0;
}

static PyObject *
window_set_windowed(pgWindowObject *self, PyObject *_null)
{
Expand Down Expand Up @@ -1113,7 +1085,5 @@ MODINIT_DEFINE(_window)
return NULL;
}

SDL_AddEventWatch(_resize_event_watch, NULL);

return module;
}

0 comments on commit 39e36e5

Please sign in to comment.