Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Added a cleaner way to set the default cursor.
Browse files Browse the repository at this point in the history
Added a way to cycle through the default cursor in testcursor.c
  • Loading branch information
slouken committed Feb 28, 2011
1 parent ddd6885 commit 9ff304e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
29 changes: 21 additions & 8 deletions src/events/SDL_mouse.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/events/SDL_mouse_c.h
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/video/cocoa/SDL_cocoamouse.m
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions test/testcursor.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -189,6 +189,7 @@ main(int argc, char *argv[])
SDL_Quit();
return (1);
}
cursor[3] = SDL_GetCursor();

current = 0;
SDL_SetCursor(cursor[current]);
Expand All @@ -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:
Expand All @@ -222,3 +223,5 @@ main(int argc, char *argv[])
SDL_Quit();
return (0);
}

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 9ff304e

Please sign in to comment.