Skip to content

Commit 22d5656

Browse files
committed
display.c: fix scaled-after-non-scaled usage
1 parent e14abb1 commit 22d5656

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src_c/display.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,64 @@ _get_display(SDL_Window *win)
824824
}
825825
return display;
826826
}
827+
#if SDL_VERSION_ATLEAST(2, 28, 0)
828+
void
829+
PG_DestroyWindowSurface(SDL_Window *win)
830+
{
831+
if (SDL_HasWindowSurface(win)) {
832+
SDL_DestroyWindowSurface(win);
833+
}
834+
}
835+
#else
836+
/* SDL_Window struct, as taken from the SDL source (except some pointer types
837+
* have been replaced with `void *`) */
838+
struct SDL_Window {
839+
const void *magic;
840+
Uint32 id;
841+
char *title;
842+
int x, y;
843+
int w, h;
844+
int min_w, min_h;
845+
int max_w, max_h;
846+
Uint32 flags;
847+
848+
/* Stored position and size for windowed mode */
849+
SDL_Rect windowed;
850+
851+
SDL_DisplayMode fullscreen_mode;
852+
853+
float brightness;
854+
Uint16 *gamma;
855+
Uint16 *saved_gamma; /* (just offset into gamma) */
856+
857+
SDL_Surface *surface;
858+
SDL_bool surface_valid;
859+
860+
void *shaper;
861+
862+
void *data;
863+
864+
void *driverdata;
865+
866+
SDL_Window *prev;
867+
SDL_Window *next;
868+
};
869+
870+
void
871+
PG_DestroyWindowSurface(SDL_Window *win)
872+
{
873+
/* This is logic taken from the implementation of SDL_DestroyWindowSurface
874+
* in the versions before it was added. We can safely do this because
875+
* SDL 2.x has been ABI compatible between releases, and the same destroy
876+
* surface logic was internally used all the way back till SDL 2.0.10 */
877+
if (win->surface) {
878+
win->surface->flags &= ~SDL_DONTFREE;
879+
SDL_FreeSurface(win->surface);
880+
win->surface = NULL;
881+
win->surface_valid = SDL_FALSE;
882+
}
883+
}
884+
#endif
827885

828886
static PyObject *
829887
pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
@@ -1215,6 +1273,11 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
12151273
SDL_SetHintWithPriority(SDL_HINT_RENDER_SCALE_QUALITY,
12161274
"nearest", SDL_HINT_DEFAULT);
12171275

1276+
/* If the window has a surface associated with it already,
1277+
* we need to destroy it (if possible) because now we are
1278+
* associating a renderer with it. */
1279+
PG_DestroyWindowSurface(win);
1280+
12181281
if (vsync) {
12191282
pg_renderer = SDL_CreateRenderer(
12201283
win, -1, SDL_RENDERER_PRESENTVSYNC);

0 commit comments

Comments
 (0)