From 9ff304e3d41c1be1e975c67b08852e31266507cf Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 27 Feb 2011 21:36:23 -0800 Subject: [PATCH] Added a cleaner way to set the default cursor. Added a way to cycle through the default cursor in testcursor.c --- src/events/SDL_mouse.c | 29 +++++++++++++++++++++-------- src/events/SDL_mouse_c.h | 3 +++ src/video/cocoa/SDL_cocoamouse.m | 4 +--- test/testcursor.c | 7 +++++-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c index 39da3cde5..1c84f3546 100644 --- a/src/events/SDL_mouse.c +++ b/src/events/SDL_mouse.c @@ -44,6 +44,17 @@ SDL_MouseInit(void) return (0); } +void +SDL_SetDefaultCursor(SDL_Cursor * cursor) +{ + SDL_Mouse *mouse = SDL_GetMouse(); + + mouse->def_cursor = cursor; + if (!mouse->cur_cursor) { + SDL_SetCursor(cursor); + } +} + SDL_Mouse * SDL_GetMouse(void) { @@ -397,15 +408,17 @@ SDL_SetCursor(SDL_Cursor * cursor) /* Set the new cursor */ if (cursor) { /* Make sure the cursor is still valid for this mouse */ - SDL_Cursor *found; - for (found = mouse->cursors; found; found = found->next) { - if (found == cursor) { - break; + if (cursor != mouse->def_cursor) { + SDL_Cursor *found; + for (found = mouse->cursors; found; found = found->next) { + if (found == cursor) { + break; + } + } + if (!found) { + SDL_SetError("Cursor not associated with the current mouse"); + return; } - } - if (!found) { - SDL_SetError("Cursor not associated with the current mouse"); - return; } mouse->cur_cursor = cursor; } else { diff --git a/src/events/SDL_mouse_c.h b/src/events/SDL_mouse_c.h index a72a569b3..f3a851cca 100644 --- a/src/events/SDL_mouse_c.h +++ b/src/events/SDL_mouse_c.h @@ -72,6 +72,9 @@ extern int SDL_MouseInit(void); /* Get the mouse state structure */ SDL_Mouse *SDL_GetMouse(void); +/* Set the default mouse cursor */ +extern void SDL_SetDefaultCursor(SDL_Cursor * cursor); + /* Set the mouse focus window */ extern void SDL_SetMouseFocus(SDL_Window * window); diff --git a/src/video/cocoa/SDL_cocoamouse.m b/src/video/cocoa/SDL_cocoamouse.m index e1bb02578..cbab74896 100644 --- a/src/video/cocoa/SDL_cocoamouse.m +++ b/src/video/cocoa/SDL_cocoamouse.m @@ -120,15 +120,13 @@ Cocoa_InitMouse(_THIS) { SDL_Mouse *mouse = SDL_GetMouse(); - SDL_Cursor *cursor; mouse->CreateCursor = Cocoa_CreateCursor; mouse->ShowCursor = Cocoa_ShowCursor; mouse->WarpMouse = Cocoa_WarpMouse; mouse->FreeCursor = Cocoa_FreeCursor; - cursor = Cocoa_CreateDefaultCursor(); - mouse->cursors = mouse->cur_cursor = cursor; + SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor()); } static int diff --git a/test/testcursor.c b/test/testcursor.c index dff54dc44..463dac9b6 100644 --- a/test/testcursor.c +++ b/test/testcursor.c @@ -147,7 +147,7 @@ main(int argc, char *argv[]) { SDL_Surface *screen; SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE; - SDL_Cursor *cursor[3]; + SDL_Cursor *cursor[4]; int current; /* Load the SDL library */ @@ -189,6 +189,7 @@ main(int argc, char *argv[]) SDL_Quit(); return (1); } + cursor[3] = SDL_GetCursor(); current = 0; SDL_SetCursor(cursor[current]); @@ -198,7 +199,7 @@ main(int argc, char *argv[]) while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_MOUSEBUTTONDOWN: - current = (current + 1) % 3; + current = (current + 1) % SDL_arraysize(cursor); SDL_SetCursor(cursor[current]); break; case SDL_KEYDOWN: @@ -222,3 +223,5 @@ main(int argc, char *argv[]) SDL_Quit(); return (0); } + +/* vi: set ts=4 sw=4 expandtab: */