@@ -65,7 +65,14 @@ mouse_set_pos(PyObject *self, PyObject *args)
65
65
static PyObject *
66
66
mouse_get_pos (PyObject * self , PyObject * args , PyObject * kwargs )
67
67
{
68
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
69
+ /* SDL3 changed the mouse API to deal with float coordinates, for now we
70
+ * still truncate the result to int before returning to python side.
71
+ * This can be changed in a breaking release in the future if needed. */
72
+ float x , y ;
73
+ #else
68
74
int x , y ;
75
+ #endif
69
76
int desktop = 0 ;
70
77
71
78
static char * kwids [] = {"desktop" , NULL };
@@ -90,12 +97,27 @@ mouse_get_pos(PyObject *self, PyObject *args, PyObject *kwargs)
90
97
SDL_RenderGetScale (sdlRenderer , & scalex , & scaley );
91
98
SDL_RenderGetViewport (sdlRenderer , & vprect );
92
99
100
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
101
+ x = x / scalex ;
102
+ y = y / scaley ;
103
+ #else
93
104
x = (int )(x / scalex );
94
105
y = (int )(y / scaley );
106
+ #endif
95
107
96
108
x -= vprect .x ;
97
109
y -= vprect .y ;
98
110
111
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
112
+ if (x < 0 )
113
+ x = 0 ;
114
+ if (x >= vprect .w )
115
+ x = (float )vprect .w - 1 ;
116
+ if (y < 0 )
117
+ y = 0 ;
118
+ if (y >= vprect .h )
119
+ y = (float )vprect .h - 1 ;
120
+ #else
99
121
if (x < 0 )
100
122
x = 0 ;
101
123
if (x >= vprect .w )
@@ -104,17 +126,29 @@ mouse_get_pos(PyObject *self, PyObject *args, PyObject *kwargs)
104
126
y = 0 ;
105
127
if (y >= vprect .h )
106
128
y = vprect .h - 1 ;
129
+ #endif
107
130
}
108
131
}
109
132
}
110
133
134
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
135
+ return pg_tuple_couple_from_values_int ((int )x , (int )y );
136
+ #else
111
137
return pg_tuple_couple_from_values_int (x , y );
138
+ #endif
112
139
}
113
140
114
141
static PyObject *
115
142
mouse_get_rel (PyObject * self , PyObject * _null )
116
143
{
144
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
145
+ /* SDL3 changed the mouse API to deal with float coordinates, for now we
146
+ * still truncate the result to int before returning to python side.
147
+ * This can be changed in a breaking release in the future if needed. */
148
+ float x , y ;
149
+ #else
117
150
int x , y ;
151
+ #endif
118
152
119
153
VIDEO_INIT_CHECK ();
120
154
@@ -132,7 +166,11 @@ mouse_get_rel(PyObject *self, PyObject *_null)
132
166
y/=scaley;
133
167
}
134
168
*/
169
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
170
+ return pg_tuple_couple_from_values_int ((int )x , (int )y );
171
+ #else
135
172
return pg_tuple_couple_from_values_int (x , y );
173
+ #endif
136
174
}
137
175
138
176
static PyObject *
@@ -216,21 +254,26 @@ mouse_set_visible(PyObject *self, PyObject *args)
216
254
{
217
255
int toggle , prevstate ;
218
256
SDL_Window * win = NULL ;
219
- Uint32 window_flags = 0 ;
257
+ SDL_WindowFlags window_flags = 0 ;
220
258
221
259
if (!PyArg_ParseTuple (args , "i" , & toggle ))
222
260
return NULL ;
223
261
VIDEO_INIT_CHECK ();
224
262
225
263
win = pg_GetDefaultWindow ();
226
264
if (win ) {
265
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
266
+ SDL_SetWindowRelativeMouseMode (win ,
267
+ SDL_GetWindowMouseGrab (win ) && !toggle );
268
+ #else
227
269
int mode = SDL_GetWindowGrab (win );
228
270
if ((mode == SDL_ENABLE ) & !toggle ) {
229
271
SDL_SetRelativeMouseMode (1 );
230
272
}
231
273
else {
232
274
SDL_SetRelativeMouseMode (0 );
233
275
}
276
+ #endif
234
277
window_flags = SDL_GetWindowFlags (win );
235
278
if (!toggle && (window_flags & PG_WINDOW_FULLSCREEN_INCLUSIVE )) {
236
279
SDL_SetHint (SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN , "0" );
@@ -263,7 +306,13 @@ mouse_get_visible(PyObject *self, PyObject *_null)
263
306
264
307
VIDEO_INIT_CHECK ();
265
308
309
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
310
+ SDL_Window * win = pg_GetDefaultWindow ();
311
+ result =
312
+ win ? (PG_CursorVisible () && !SDL_GetWindowRelativeMouseMode (win )) : 0 ;
313
+ #else
266
314
result = (PG_CursorVisible () && !SDL_GetRelativeMouseMode ());
315
+ #endif
267
316
268
317
if (0 > result ) {
269
318
return RAISE (pgExc_SDLError , SDL_GetError ());
@@ -527,7 +576,12 @@ mouse_get_cursor(PyObject *self, PyObject *_null)
527
576
static PyObject *
528
577
mouse_get_relative_mode (PyObject * self )
529
578
{
579
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
580
+ SDL_Window * win = pg_GetDefaultWindow ();
581
+ return PyBool_FromLong (win ? SDL_GetWindowRelativeMouseMode (win ) : 0 );
582
+ #else
530
583
return PyBool_FromLong (SDL_GetRelativeMouseMode ());
584
+ #endif
531
585
}
532
586
533
587
static PyObject *
@@ -537,9 +591,20 @@ mouse_set_relative_mode(PyObject *self, PyObject *arg)
537
591
if (mode == -1 ) {
538
592
return NULL ;
539
593
}
594
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
595
+ SDL_Window * win = pg_GetDefaultWindow ();
596
+ if (!win ) {
597
+ return RAISE (pgExc_SDLError ,
598
+ "display.set_mode has not been called yet." );
599
+ }
600
+ if (!SDL_SetWindowRelativeMouseMode (win , (bool )mode )) {
601
+ return RAISE (pgExc_SDLError , SDL_GetError ());
602
+ }
603
+ #else
540
604
if (SDL_SetRelativeMouseMode ((SDL_bool )mode )) {
541
605
return RAISE (pgExc_SDLError , SDL_GetError ());
542
606
}
607
+ #endif
543
608
Py_RETURN_NONE ;
544
609
}
545
610
0 commit comments