Skip to content

Commit eb8faef

Browse files
authored
Merge pull request #2483 from Starbuck5/showcursor-sdl3
Port SDL_ShowCursor to SDL3
2 parents 754c2e9 + aee1330 commit eb8faef

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src_c/_pygame.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#include <SDL.h>
4444

4545
#if SDL_VERSION_ATLEAST(3, 0, 0)
46+
#define PG_ShowCursor SDL_ShowCursor
47+
#define PG_HideCursor SDL_HideCursor
48+
#define PG_CursorVisible SDL_CursorVisible
49+
4650
#define PG_INIT_NOPARACHUTE 0
4751

4852
// UINT16 audio no longer exists in SDL3
@@ -67,6 +71,9 @@
6771
#define PG_CreateSurfaceFrom SDL_CreateSurfaceFrom
6872

6973
#else /* ~SDL_VERSION_ATLEAST(3, 0, 0)*/
74+
#define PG_ShowCursor() SDL_ShowCursor(SDL_ENABLE)
75+
#define PG_HideCursor() SDL_ShowCursor(SDL_DISABLE)
76+
#define PG_CursorVisible() SDL_ShowCursor(SDL_QUERY)
7077

7178
#define PG_INIT_NOPARACHUTE SDL_INIT_NOPARACHUTE
7279

src_c/event.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ set_grab(PyObject *self, PyObject *arg)
15621562
if (win) {
15631563
if (doit) {
15641564
SDL_SetWindowGrab(win, SDL_TRUE);
1565-
if (SDL_ShowCursor(SDL_QUERY) == SDL_DISABLE)
1565+
if (PG_CursorVisible() == SDL_DISABLE)
15661566
SDL_SetRelativeMouseMode(1);
15671567
else
15681568
SDL_SetRelativeMouseMode(0);

src_c/mouse.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ mouse_get_pressed(PyObject *self, PyObject *args, PyObject *kwargs)
165165
static PyObject *
166166
mouse_set_visible(PyObject *self, PyObject *args)
167167
{
168-
int toggle;
169-
int mode;
168+
int toggle, prevstate;
170169
SDL_Window *win = NULL;
171170
Uint32 window_flags = 0;
172171

@@ -176,7 +175,7 @@ mouse_set_visible(PyObject *self, PyObject *args)
176175

177176
win = pg_GetDefaultWindow();
178177
if (win) {
179-
mode = SDL_GetWindowGrab(win);
178+
int mode = SDL_GetWindowGrab(win);
180179
if ((mode == SDL_ENABLE) & !toggle) {
181180
SDL_SetRelativeMouseMode(1);
182181
}
@@ -193,8 +192,20 @@ mouse_set_visible(PyObject *self, PyObject *args)
193192
}
194193
}
195194

196-
toggle = SDL_ShowCursor(toggle);
197-
return PyBool_FromLong(toggle);
195+
prevstate = PG_CursorVisible();
196+
197+
// Cursor visibility API **can** raise errors through SDL, but in
198+
// practice it does not. (Ankith checked through the code)
199+
// Historically we haven't error checked this, should that change in the
200+
// future?
201+
if (toggle) {
202+
PG_ShowCursor();
203+
}
204+
else {
205+
PG_HideCursor();
206+
}
207+
208+
return PyBool_FromLong(prevstate);
198209
}
199210

200211
static PyObject *
@@ -204,7 +215,7 @@ mouse_get_visible(PyObject *self, PyObject *_null)
204215

205216
VIDEO_INIT_CHECK();
206217

207-
result = SDL_ShowCursor(SDL_QUERY);
218+
result = PG_CursorVisible();
208219

209220
if (0 > result) {
210221
return RAISE(pgExc_SDLError, SDL_GetError());

0 commit comments

Comments
 (0)