Skip to content

Update OpenGL Viewport when Window resizes #2915

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

Merged
merged 5 commits into from
Jul 25, 2024
Merged
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
42 changes: 41 additions & 1 deletion src_c/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "pygame.h"

#include "pgcompat.h"
#include "pgopengl.h"

#include "doc/sdl2_video_doc.h"
#include "doc/window_doc.h"
Expand Down Expand Up @@ -95,6 +96,7 @@ pg_display_resource(char *filename)
}

static PyTypeObject pgWindow_Type;
static GL_glViewport_Func p_glViewport = NULL;

#define pgWindow_Check(x) \
(PyObject_IsInstance((x), (PyObject *)&pgWindow_Type))
Expand Down Expand Up @@ -211,7 +213,24 @@ window_flip(pgWindowObject *self, PyObject *_null)
Py_RETURN_NONE;
}

// Callback function for surface auto resize
/* Exception already set */
static int
_window_opengl_set_viewport(SDL_Window *window, SDL_GLContext context,
int wnew, int hnew)
{
if (SDL_GL_MakeCurrent(window, context) < 0) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}
if (p_glViewport == NULL) {
PyErr_SetString(pgExc_SDLError, "glViewport function is unavailable");
return -1;
}
p_glViewport(0, 0, wnew, hnew);
return 0;
}

// Callback function for surface auto resize or OpenGL viewport update
static int SDLCALL
_resize_event_watch(void *userdata, SDL_Event *event)
{
Expand All @@ -232,6 +251,15 @@ _resize_event_watch(void *userdata, SDL_Event *event)
return 0;
}

if (event_window_pg->context != NULL) {
if (_window_opengl_set_viewport(event_window, event_window_pg->context,
event->window.data1,
event->window.data2) < 0) {
return PyErr_WarnEx(PyExc_RuntimeWarning,
"Failed to set OpenGL viewport", 0);
}
}

if (!event_window_pg->surf)
return 0;

Expand Down Expand Up @@ -588,6 +616,13 @@ window_set_size(pgWindowObject *self, PyObject *arg, void *v)
* relying on the event callback */
self->surf->surf = SDL_GetWindowSurface(self->_win);
}
if (self->context != NULL) {
/* Update the OpenGL viewport immediately instead of relying on the
* event callback */
if (_window_opengl_set_viewport(self->_win, self->context, w, h) < 0) {
return -1;
}
}

return 0;
}
Expand Down Expand Up @@ -976,6 +1011,11 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs)
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}
/* As stated in the 'Remarks' of the docs
* (https://wiki.libsdl.org/SDL2/SDL_GL_GetProcAddress) on Windows
* SDL_GL_GetProcAddress is only valid after an OpenGL context has been
* created */
p_glViewport = (GL_glViewport_Func)SDL_GL_GetProcAddress("glViewport");
self->context = context;
}
else {
Expand Down
Loading