33#include "pygame.h"
44
55#include "pgcompat.h"
6+ #include "pgopengl.h"
67
78#include "doc/sdl2_video_doc.h"
89#include "doc/window_doc.h"
@@ -85,6 +86,7 @@ pg_display_resource(char *filename)
8586}
8687
8788static PyTypeObject pgWindow_Type ;
89+ static GL_glViewport_Func p_glViewport = NULL ;
8890
8991#define pgWindow_Check (x ) \
9092 (PyObject_IsInstance((x), (PyObject *)&pgWindow_Type))
@@ -201,7 +203,24 @@ window_flip(pgWindowObject *self, PyObject *_null)
201203 Py_RETURN_NONE ;
202204}
203205
204- // Callback function for surface auto resize
206+ /* Exception already set */
207+ static int
208+ _window_opengl_set_viewport (SDL_Window * window , SDL_GLContext context ,
209+ int wnew , int hnew )
210+ {
211+ if (SDL_GL_MakeCurrent (window , context ) < 0 ) {
212+ PyErr_SetString (pgExc_SDLError , SDL_GetError ());
213+ return -1 ;
214+ }
215+ if (p_glViewport == NULL ) {
216+ PyErr_SetString (pgExc_SDLError , "glViewport function is unavailable" );
217+ return -1 ;
218+ }
219+ p_glViewport (0 , 0 , wnew , hnew );
220+ return 0 ;
221+ }
222+
223+ // Callback function for surface auto resize or OpenGL viewport update
205224static int SDLCALL
206225_resize_event_watch (void * userdata , SDL_Event * event )
207226{
@@ -222,6 +241,15 @@ _resize_event_watch(void *userdata, SDL_Event *event)
222241 return 0 ;
223242 }
224243
244+ if (event_window_pg -> context != NULL ) {
245+ if (_window_opengl_set_viewport (event_window , event_window_pg -> context ,
246+ event -> window .data1 ,
247+ event -> window .data2 ) < 0 ) {
248+ return PyErr_WarnEx (PyExc_RuntimeWarning ,
249+ "Failed to set OpenGL viewport" , 0 );
250+ }
251+ }
252+
225253 if (!event_window_pg -> surf )
226254 return 0 ;
227255
@@ -578,6 +606,13 @@ window_set_size(pgWindowObject *self, PyObject *arg, void *v)
578606 * relying on the event callback */
579607 self -> surf -> surf = SDL_GetWindowSurface (self -> _win );
580608 }
609+ if (self -> context != NULL ) {
610+ /* Update the OpenGL viewport immediately instead of relying on the
611+ * event callback */
612+ if (_window_opengl_set_viewport (self -> _win , self -> context , w , h ) < 0 ) {
613+ return -1 ;
614+ }
615+ }
581616
582617 return 0 ;
583618}
@@ -966,6 +1001,11 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs)
9661001 PyErr_SetString (pgExc_SDLError , SDL_GetError ());
9671002 return -1 ;
9681003 }
1004+ /* As stated in the 'Remarks' of the docs
1005+ * (https://wiki.libsdl.org/SDL2/SDL_GL_GetProcAddress) on Windows
1006+ * SDL_GL_GetProcAddress is only valid after an OpenGL context has been
1007+ * created */
1008+ p_glViewport = (GL_glViewport_Func )SDL_GL_GetProcAddress ("glViewport" );
9691009 self -> context = context ;
9701010 }
9711011 else {
0 commit comments