Added a cleaner way to set the default cursor.
authorSam Lantinga
Sun, 27 Feb 2011 21:36:23 -0800
changeset 540564fa8526e1ce
parent 5404 6717e01acbe0
child 5406 1517da4ab6b1
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
src/events/SDL_mouse_c.h
src/video/cocoa/SDL_cocoamouse.m
test/testcursor.c
     1.1 --- a/src/events/SDL_mouse.c	Sun Feb 27 21:17:06 2011 -0800
     1.2 +++ b/src/events/SDL_mouse.c	Sun Feb 27 21:36:23 2011 -0800
     1.3 @@ -44,6 +44,17 @@
     1.4      return (0);
     1.5  }
     1.6  
     1.7 +void
     1.8 +SDL_SetDefaultCursor(SDL_Cursor * cursor)
     1.9 +{
    1.10 +    SDL_Mouse *mouse = SDL_GetMouse();
    1.11 +
    1.12 +    mouse->def_cursor = cursor;
    1.13 +    if (!mouse->cur_cursor) {
    1.14 +        SDL_SetCursor(cursor);
    1.15 +    }
    1.16 +}
    1.17 +
    1.18  SDL_Mouse *
    1.19  SDL_GetMouse(void)
    1.20  {
    1.21 @@ -397,15 +408,17 @@
    1.22      /* Set the new cursor */
    1.23      if (cursor) {
    1.24          /* Make sure the cursor is still valid for this mouse */
    1.25 -        SDL_Cursor *found;
    1.26 -        for (found = mouse->cursors; found; found = found->next) {
    1.27 -            if (found == cursor) {
    1.28 -                break;
    1.29 +        if (cursor != mouse->def_cursor) {
    1.30 +            SDL_Cursor *found;
    1.31 +            for (found = mouse->cursors; found; found = found->next) {
    1.32 +                if (found == cursor) {
    1.33 +                    break;
    1.34 +                }
    1.35              }
    1.36 -        }
    1.37 -        if (!found) {
    1.38 -            SDL_SetError("Cursor not associated with the current mouse");
    1.39 -            return;
    1.40 +            if (!found) {
    1.41 +                SDL_SetError("Cursor not associated with the current mouse");
    1.42 +                return;
    1.43 +            }
    1.44          }
    1.45          mouse->cur_cursor = cursor;
    1.46      } else {
     2.1 --- a/src/events/SDL_mouse_c.h	Sun Feb 27 21:17:06 2011 -0800
     2.2 +++ b/src/events/SDL_mouse_c.h	Sun Feb 27 21:36:23 2011 -0800
     2.3 @@ -72,6 +72,9 @@
     2.4  /* Get the mouse state structure */
     2.5  SDL_Mouse *SDL_GetMouse(void);
     2.6  
     2.7 +/* Set the default mouse cursor */
     2.8 +extern void SDL_SetDefaultCursor(SDL_Cursor * cursor);
     2.9 +
    2.10  /* Set the mouse focus window */
    2.11  extern void SDL_SetMouseFocus(SDL_Window * window);
    2.12  
     3.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Sun Feb 27 21:17:06 2011 -0800
     3.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Sun Feb 27 21:36:23 2011 -0800
     3.3 @@ -120,15 +120,13 @@
     3.4  Cocoa_InitMouse(_THIS)
     3.5  {
     3.6      SDL_Mouse *mouse = SDL_GetMouse();
     3.7 -    SDL_Cursor *cursor;
     3.8  
     3.9      mouse->CreateCursor = Cocoa_CreateCursor;
    3.10      mouse->ShowCursor = Cocoa_ShowCursor;
    3.11      mouse->WarpMouse = Cocoa_WarpMouse;
    3.12      mouse->FreeCursor = Cocoa_FreeCursor;
    3.13  
    3.14 -    cursor = Cocoa_CreateDefaultCursor();
    3.15 -    mouse->cursors = mouse->cur_cursor = cursor;
    3.16 +    SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
    3.17  }
    3.18  
    3.19  static int
     4.1 --- a/test/testcursor.c	Sun Feb 27 21:17:06 2011 -0800
     4.2 +++ b/test/testcursor.c	Sun Feb 27 21:36:23 2011 -0800
     4.3 @@ -147,7 +147,7 @@
     4.4  {
     4.5      SDL_Surface *screen;
     4.6      SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
     4.7 -    SDL_Cursor *cursor[3];
     4.8 +    SDL_Cursor *cursor[4];
     4.9      int current;
    4.10  
    4.11      /* Load the SDL library */
    4.12 @@ -189,6 +189,7 @@
    4.13          SDL_Quit();
    4.14          return (1);
    4.15      }
    4.16 +    cursor[3] = SDL_GetCursor();
    4.17  
    4.18      current = 0;
    4.19      SDL_SetCursor(cursor[current]);
    4.20 @@ -198,7 +199,7 @@
    4.21          while (SDL_PollEvent(&event)) {
    4.22              switch (event.type) {
    4.23              case SDL_MOUSEBUTTONDOWN:
    4.24 -                current = (current + 1) % 3;
    4.25 +                current = (current + 1) % SDL_arraysize(cursor);
    4.26                  SDL_SetCursor(cursor[current]);
    4.27                  break;
    4.28              case SDL_KEYDOWN:
    4.29 @@ -222,3 +223,5 @@
    4.30      SDL_Quit();
    4.31      return (0);
    4.32  }
    4.33 +
    4.34 +/* vi: set ts=4 sw=4 expandtab: */