@@ -279,9 +279,15 @@ pg_get_init(PyObject *self, PyObject *_null)
279
279
static PyObject *
280
280
pg_get_active (PyObject * self , PyObject * _null )
281
281
{
282
- Uint32 flags = SDL_GetWindowFlags (pg_GetDefaultWindow ());
282
+ SDL_WindowFlags flags = SDL_GetWindowFlags (pg_GetDefaultWindow ());
283
+
284
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
285
+ return PyBool_FromLong (!(flags & SDL_WINDOW_HIDDEN ) &&
286
+ !(flags & SDL_WINDOW_MINIMIZED ));
287
+ #else
283
288
return PyBool_FromLong ((flags & SDL_WINDOW_SHOWN ) &&
284
289
!(flags & SDL_WINDOW_MINIMIZED ));
290
+ #endif
285
291
}
286
292
287
293
/* vidinfo object */
@@ -1077,6 +1083,82 @@ _get_display(SDL_Window *win)
1077
1083
return display ;
1078
1084
}
1079
1085
1086
+ /* Based on code from sdl2-compat */
1087
+ static SDL_Window *
1088
+ PG_CreateWindowCompat (const char * title , int x , int y , int w , int h ,
1089
+ SDL_WindowFlags flags )
1090
+ {
1091
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1092
+ SDL_Window * window = NULL ;
1093
+
1094
+ SDL_PropertiesID props = SDL_CreateProperties ();
1095
+ if (!props ) {
1096
+ return NULL ;
1097
+ }
1098
+
1099
+ if (title && * title ) {
1100
+ SDL_SetStringProperty (props , SDL_PROP_WINDOW_CREATE_TITLE_STRING ,
1101
+ title );
1102
+ }
1103
+ SDL_SetNumberProperty (props , SDL_PROP_WINDOW_CREATE_X_NUMBER , x );
1104
+ SDL_SetNumberProperty (props , SDL_PROP_WINDOW_CREATE_Y_NUMBER , y );
1105
+ SDL_SetNumberProperty (props , SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER , w );
1106
+ SDL_SetNumberProperty (props , SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER , h );
1107
+ SDL_SetNumberProperty (props , SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER , flags );
1108
+ SDL_SetBooleanProperty (
1109
+ props , SDL_PROP_WINDOW_CREATE_EXTERNAL_GRAPHICS_CONTEXT_BOOLEAN ,
1110
+ SDL_GetHintBoolean ("SDL_VIDEO_EXTERNAL_CONTEXT" , false));
1111
+
1112
+ window = SDL_CreateWindowWithProperties (props );
1113
+ SDL_DestroyProperties (props );
1114
+ return window ;
1115
+ #else
1116
+ return SDL_CreateWindow (title , x , y , w , h , flags );
1117
+ #endif
1118
+ }
1119
+
1120
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1121
+ /* Returns 0 on success, negative on failure. */
1122
+ static int
1123
+ PG_SetWindowFullscreen (SDL_Window * window , bool fullscreen ,
1124
+ bool non_desktop_fullscreen )
1125
+ {
1126
+ int ret = -1 ;
1127
+ SDL_DisplayMode * * modes = NULL ;
1128
+ SDL_DisplayMode * chosen_mode = NULL ;
1129
+ if (!SDL_SetWindowFullscreen (window , fullscreen )) {
1130
+ goto end ;
1131
+ }
1132
+ if (fullscreen ) {
1133
+ if (non_desktop_fullscreen ) {
1134
+ /* if not desktop fullscreen, get the first display mode available
1135
+ */
1136
+ SDL_DisplayID disp = SDL_GetDisplayForWindow (window );
1137
+ if (!disp ) {
1138
+ goto end ;
1139
+ }
1140
+ modes = SDL_GetFullscreenDisplayModes (disp , NULL );
1141
+ if (!modes ) {
1142
+ goto end ;
1143
+ }
1144
+ chosen_mode = modes [0 ];
1145
+ if (!chosen_mode ) {
1146
+ SDL_SetError ("Could not get fullscreen display mode" );
1147
+ goto end ;
1148
+ }
1149
+ }
1150
+ if (!SDL_SetWindowFullscreenMode (window , chosen_mode )) {
1151
+ goto end ;
1152
+ }
1153
+ }
1154
+
1155
+ ret = 0 ;
1156
+ end :
1157
+ SDL_free (modes );
1158
+ return ret ;
1159
+ }
1160
+ #endif
1161
+
1080
1162
static PyObject *
1081
1163
pg_set_mode (PyObject * self , PyObject * arg , PyObject * kwds )
1082
1164
{
@@ -1100,6 +1182,9 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
1100
1182
int display = _get_display (win );
1101
1183
char * title = state -> title ;
1102
1184
const char * scale_env , * winid_env ;
1185
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1186
+ int non_desktop_fullscreen = 0 ;
1187
+ #endif
1103
1188
1104
1189
char * keywords [] = {"size" , "flags" , "depth" , "display" , "vsync" , NULL };
1105
1190
@@ -1221,12 +1306,22 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
1221
1306
1222
1307
if (flags & PGS_FULLSCREEN ) {
1223
1308
if (flags & PGS_SCALED ) {
1309
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1310
+ sdl_flags |= SDL_WINDOW_FULLSCREEN ;
1311
+ non_desktop_fullscreen = 1 ;
1312
+ #else
1224
1313
sdl_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP ;
1314
+ #endif
1225
1315
}
1226
1316
else if (w == display_mode -> w && h == display_mode -> h ) {
1227
1317
/* No need to change physical resolution.
1228
1318
Borderless fullscreen is preferred when possible */
1319
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1320
+ sdl_flags |= SDL_WINDOW_FULLSCREEN ;
1321
+ non_desktop_fullscreen = 1 ;
1322
+ #else
1229
1323
sdl_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP ;
1324
+ #endif
1230
1325
}
1231
1326
else {
1232
1327
sdl_flags |= SDL_WINDOW_FULLSCREEN ;
@@ -1252,15 +1347,19 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
1252
1347
SDL_AddEventWatch (pg_ResizeEventWatch , self );
1253
1348
}
1254
1349
}
1350
+ #if !SDL_VERSION_ATLEAST (3 , 0 , 0 )
1255
1351
if (flags & PGS_SHOWN ) {
1256
1352
sdl_flags |= SDL_WINDOW_SHOWN ;
1257
1353
}
1354
+ #endif
1258
1355
if (flags & PGS_HIDDEN ) {
1259
1356
sdl_flags |= SDL_WINDOW_HIDDEN ;
1260
1357
}
1358
+ #if !SDL_VERSION_ATLEAST (3 , 0 , 0 )
1261
1359
if (!(sdl_flags & SDL_WINDOW_HIDDEN )) {
1262
1360
sdl_flags |= SDL_WINDOW_SHOWN ;
1263
1361
}
1362
+ #endif
1264
1363
if (flags & PGS_OPENGL ) {
1265
1364
/* Must be called before creating context */
1266
1365
if (flags & PGS_DOUBLEBUF ) {
@@ -1289,9 +1388,14 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
1289
1388
if (win ) {
1290
1389
if (SDL_GetWindowDisplayIndex (win ) == display ) {
1291
1390
// fullscreen windows don't hold window x and y as needed
1391
+
1392
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1393
+ if (SDL_GetWindowFlags (win ) & SDL_WINDOW_FULLSCREEN ) {
1394
+ #else
1292
1395
if (SDL_GetWindowFlags (win ) &
1293
1396
(SDL_WINDOW_FULLSCREEN |
1294
1397
SDL_WINDOW_FULLSCREEN_DESKTOP )) {
1398
+ #endif
1295
1399
x = state -> fullscreen_backup_x ;
1296
1400
y = state -> fullscreen_backup_y ;
1297
1401
@@ -1380,19 +1484,27 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
1380
1484
// SDL doesn't preserve window position in fullscreen mode
1381
1485
// However, windows coming out of fullscreen need these to go back
1382
1486
// into the correct position
1487
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1488
+ if (sdl_flags & SDL_WINDOW_FULLSCREEN ) {
1489
+ #else
1383
1490
if (sdl_flags &
1384
1491
(SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP )) {
1492
+ #endif
1385
1493
state -> fullscreen_backup_x = x ;
1386
1494
state -> fullscreen_backup_y = y ;
1387
1495
}
1388
1496
1389
1497
if (!win ) {
1390
1498
/*open window*/
1391
1499
if (hwnd != 0 ) {
1500
+ /* TODO: figure SDL3 equivalent */
1501
+ #if !SDL_VERSION_ATLEAST (3 , 0 , 0 )
1392
1502
win = SDL_CreateWindowFrom ((void * )hwnd );
1503
+ #endif
1393
1504
}
1394
1505
else {
1395
- win = SDL_CreateWindow (title , x , y , w_1 , h_1 , sdl_flags );
1506
+ win = PG_CreateWindowCompat (title , x , y , w_1 , h_1 ,
1507
+ sdl_flags );
1396
1508
w_actual = w_1 ;
1397
1509
h_actual = h_1 ;
1398
1510
}
@@ -1417,12 +1529,20 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
1417
1529
* resize/bordered/hidden changes due to SDL ignoring those
1418
1530
* changes if the window is fullscreen
1419
1531
* See https://github.com/pygame/pygame/issues/2711 */
1532
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1533
+ if (0 != PG_SetWindowFullscreen (
1534
+ win , sdl_flags & SDL_WINDOW_FULLSCREEN ,
1535
+ non_desktop_fullscreen )) {
1536
+ return RAISE (pgExc_SDLError , SDL_GetError ());
1537
+ }
1538
+ #else
1420
1539
if (0 !=
1421
1540
SDL_SetWindowFullscreen (
1422
1541
win , sdl_flags & (SDL_WINDOW_FULLSCREEN |
1423
1542
SDL_WINDOW_FULLSCREEN_DESKTOP ))) {
1424
1543
return RAISE (pgExc_SDLError , SDL_GetError ());
1425
1544
}
1545
+ #endif
1426
1546
1427
1547
SDL_SetWindowResizable (win , flags & PGS_RESIZABLE );
1428
1548
SDL_SetWindowBordered (win , (flags & PGS_NOFRAME ) == 0 );
@@ -1713,7 +1833,11 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
1713
1833
!vsync && (((flags & PGS_RESIZABLE ) == 0 ) || !zero_size )) {
1714
1834
if (((surface -> surf -> w != w_actual ) ||
1715
1835
(surface -> surf -> h != h_actual )) &&
1836
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1837
+ ((surface -> surf -> flags & SDL_WINDOW_FULLSCREEN ) != 0 )) {
1838
+ #else
1716
1839
((surface -> surf -> flags & SDL_WINDOW_FULLSCREEN_DESKTOP ) != 0 )) {
1840
+ #endif
1717
1841
char buffer [150 ];
1718
1842
char * format_string =
1719
1843
"Requested window was forcibly resized by the OS.\n\t"
@@ -2675,14 +2799,18 @@ static PyObject *
2675
2799
pg_is_fullscreen (PyObject * self , PyObject * _null )
2676
2800
{
2677
2801
SDL_Window * win = pg_GetDefaultWindow ();
2678
- int flags ;
2802
+ SDL_WindowFlags flags ;
2679
2803
2680
2804
VIDEO_INIT_CHECK ();
2681
2805
if (!win ) {
2682
2806
return RAISE (pgExc_SDLError , "No open window" );
2683
2807
}
2684
2808
2809
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
2810
+ flags = SDL_GetWindowFlags (win );
2811
+ #else
2685
2812
flags = SDL_GetWindowFlags (win ) & SDL_WINDOW_FULLSCREEN_DESKTOP ;
2813
+ #endif
2686
2814
2687
2815
if (flags & SDL_WINDOW_FULLSCREEN ) {
2688
2816
Py_RETURN_TRUE ;
@@ -2907,7 +3035,8 @@ static PyObject *
2907
3035
pg_toggle_fullscreen (PyObject * self , PyObject * _null )
2908
3036
{
2909
3037
SDL_Window * win = pg_GetDefaultWindow ();
2910
- int result , flags ;
3038
+ int result ;
3039
+ SDL_WindowFlags flags ;
2911
3040
int window_w , window_h , w , h , window_display , x , y ;
2912
3041
pgSurfaceObject * display_surface ;
2913
3042
_DisplayState * state = DISPLAY_MOD_STATE (self );
@@ -3122,8 +3251,12 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
3122
3251
}
3123
3252
}
3124
3253
}
3254
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
3255
+ else if ((flags & SDL_WINDOW_FULLSCREEN ) == SDL_WINDOW_FULLSCREEN ) {
3256
+ #else
3125
3257
else if ((flags & SDL_WINDOW_FULLSCREEN_DESKTOP ) ==
3126
3258
SDL_WINDOW_FULLSCREEN_DESKTOP ) {
3259
+ #endif
3127
3260
result = SDL_SetWindowFullscreen (win , 0 );
3128
3261
if (result != 0 ) {
3129
3262
return RAISE (pgExc_SDLError , SDL_GetError ());
@@ -3144,9 +3277,13 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
3144
3277
1 ) != 0 ) {
3145
3278
return NULL ;
3146
3279
}
3280
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
3281
+ flags &= ~SDL_WINDOW_FULLSCREEN ;
3282
+ #else
3147
3283
flags &= ~SDL_WINDOW_FULLSCREEN_DESKTOP ;
3284
+ #endif
3148
3285
/* SDL_WINDOW_FULLSCREEN_DESKTOP includes SDL_WINDOW_FULLSCREEN */
3149
- win = SDL_CreateWindow (state -> title , wx , wy , w , h , flags );
3286
+ win = PG_CreateWindowCompat (state -> title , wx , wy , w , h , flags );
3150
3287
if (win == NULL ) {
3151
3288
return RAISE (pgExc_SDLError , SDL_GetError ());
3152
3289
}
@@ -3193,15 +3330,23 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
3193
3330
state -> fullscreen_backup_y = y ;
3194
3331
3195
3332
if (state -> unscaled_render ) {
3333
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
3334
+ result = PG_SetWindowFullscreen (win , 1 , 0 );
3335
+ #else
3196
3336
result =
3197
3337
SDL_SetWindowFullscreen (win , SDL_WINDOW_FULLSCREEN_DESKTOP );
3338
+ #endif
3198
3339
if (result != 0 ) {
3199
3340
return RAISE (pgExc_SDLError , SDL_GetError ());
3200
3341
}
3201
3342
}
3202
3343
else if (pg_renderer != NULL ) {
3344
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
3345
+ result = PG_SetWindowFullscreen (win , 1 , 0 );
3346
+ #else
3203
3347
result =
3204
3348
SDL_SetWindowFullscreen (win , SDL_WINDOW_FULLSCREEN_DESKTOP );
3349
+ #endif
3205
3350
if (result != 0 ) {
3206
3351
return RAISE (pgExc_SDLError , SDL_GetError ());
3207
3352
}
@@ -3235,8 +3380,12 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
3235
3380
#endif
3236
3381
}
3237
3382
else if (state -> using_gl ) {
3383
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
3384
+ result = PG_SetWindowFullscreen (win , 1 , 0 );
3385
+ #else
3238
3386
result =
3239
3387
SDL_SetWindowFullscreen (win , SDL_WINDOW_FULLSCREEN_DESKTOP );
3388
+ #endif
3240
3389
if (result != 0 ) {
3241
3390
return RAISE (pgExc_SDLError , SDL_GetError ());
3242
3391
}
@@ -3262,8 +3411,12 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
3262
3411
}
3263
3412
}
3264
3413
else if (w == display_mode -> w && h == display_mode -> h ) {
3414
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
3415
+ result = PG_SetWindowFullscreen (win , 1 , 0 );
3416
+ #else
3265
3417
result =
3266
3418
SDL_SetWindowFullscreen (win , SDL_WINDOW_FULLSCREEN_DESKTOP );
3419
+ #endif
3267
3420
if (result != 0 ) {
3268
3421
return RAISE (pgExc_SDLError , SDL_GetError ());
3269
3422
}
@@ -3290,7 +3443,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
3290
3443
h != display_surface -> surf -> h ) {
3291
3444
int wx = SDL_WINDOWPOS_UNDEFINED_DISPLAY (window_display );
3292
3445
int wy = SDL_WINDOWPOS_UNDEFINED_DISPLAY (window_display );
3293
- win = SDL_CreateWindow (state -> title , wx , wy , w , h , flags );
3446
+ win = PG_CreateWindowCompat (state -> title , wx , wy , w , h , flags );
3294
3447
if (win == NULL ) {
3295
3448
return RAISE (pgExc_SDLError , SDL_GetError ());
3296
3449
}
@@ -3323,7 +3476,7 @@ pg_display_resize_event(PyObject *self, PyObject *event)
3323
3476
int wnew = PyLong_AsLong (PyObject_GetAttrString (event , "w" ));
3324
3477
int hnew = PyLong_AsLong (PyObject_GetAttrString (event , "h" ));
3325
3478
SDL_Window * win = pg_GetDefaultWindow ();
3326
- int flags ;
3479
+ SDL_WindowFlags flags ;
3327
3480
int w , h , result ;
3328
3481
_DisplayState * state = DISPLAY_MOD_STATE (self );
3329
3482
GL_glViewport_Func p_glViewport = NULL ;
@@ -3332,10 +3485,14 @@ pg_display_resize_event(PyObject *self, PyObject *event)
3332
3485
if (!win ) {
3333
3486
return RAISE (pgExc_SDLError , "No open window" );
3334
3487
}
3488
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
3489
+ flags = SDL_GetWindowFlags (win ) & SDL_WINDOW_FULLSCREEN ;
3335
3490
3491
+ #else
3336
3492
flags = SDL_GetWindowFlags (win ) &
3337
3493
(SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP );
3338
3494
3495
+ #endif
3339
3496
if (flags ) {
3340
3497
return PyLong_FromLong (-1 );
3341
3498
}
0 commit comments