3
3
#include "pygame.h"
4
4
5
5
#include "pgcompat.h"
6
+ #include "pgopengl.h"
6
7
7
8
#include "doc/sdl2_video_doc.h"
8
9
#include "doc/window_doc.h"
@@ -85,6 +86,7 @@ pg_display_resource(char *filename)
85
86
}
86
87
87
88
static PyTypeObject pgWindow_Type ;
89
+ static GL_glViewport_Func p_glViewport = NULL ;
88
90
89
91
#define pgWindow_Check (x ) \
90
92
(PyObject_IsInstance((x), (PyObject *)&pgWindow_Type))
@@ -201,7 +203,24 @@ window_flip(pgWindowObject *self, PyObject *_null)
201
203
Py_RETURN_NONE ;
202
204
}
203
205
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
205
224
static int SDLCALL
206
225
_resize_event_watch (void * userdata , SDL_Event * event )
207
226
{
@@ -222,6 +241,15 @@ _resize_event_watch(void *userdata, SDL_Event *event)
222
241
return 0 ;
223
242
}
224
243
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
+
225
253
if (!event_window_pg -> surf )
226
254
return 0 ;
227
255
@@ -578,6 +606,13 @@ window_set_size(pgWindowObject *self, PyObject *arg, void *v)
578
606
* relying on the event callback */
579
607
self -> surf -> surf = SDL_GetWindowSurface (self -> _win );
580
608
}
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
+ }
581
616
582
617
return 0 ;
583
618
}
@@ -966,6 +1001,11 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs)
966
1001
PyErr_SetString (pgExc_SDLError , SDL_GetError ());
967
1002
return -1 ;
968
1003
}
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" );
969
1009
self -> context = context ;
970
1010
}
971
1011
else {
0 commit comments