Added a function to create color cursors: SDL_CreateColorCursor()
authorSam Lantinga <slouken@libsdl.org>
Fri, 11 Mar 2011 14:14:38 -0800
changeset 54735665ccbd6792
parent 5472 674d45edfaeb
child 5474 c2a1f62433e1
Added a function to create color cursors: SDL_CreateColorCursor()
include/SDL_mouse.h
src/events/SDL_mouse.c
test/testcursor.c
     1.1 --- a/include/SDL_mouse.h	Fri Mar 11 13:59:52 2011 -0800
     1.2 +++ b/include/SDL_mouse.h	Fri Mar 11 14:14:38 2011 -0800
     1.3 @@ -147,6 +147,15 @@
     1.4                                                       int hot_y);
     1.5  
     1.6  /**
     1.7 + *  \brief Create a color cursor.
     1.8 + *  
     1.9 + *  \sa SDL_FreeCursor()
    1.10 + */
    1.11 +extern DECLSPEC SDL_Cursor *SDLCALL SDL_CreateColorCursor(SDL_Surface *surface,
    1.12 +                                                          int hot_x,
    1.13 +                                                          int hot_y);
    1.14 +
    1.15 +/**
    1.16   *  \brief Set the active cursor.
    1.17   */
    1.18  extern DECLSPEC void SDLCALL SDL_SetCursor(SDL_Cursor * cursor);
     2.1 --- a/src/events/SDL_mouse.c	Fri Mar 11 13:59:52 2011 -0800
     2.2 +++ b/src/events/SDL_mouse.c	Fri Mar 11 14:14:38 2011 -0800
     2.3 @@ -360,24 +360,15 @@
     2.4      const Uint32 white = 0xFFFFFFFF;
     2.5      const Uint32 transparent = 0x00000000;
     2.6  
     2.7 -    if (!mouse->CreateCursor) {
     2.8 -        SDL_SetError("Cursors are not currently supported");
     2.9 -        return NULL;
    2.10 -    }
    2.11 -
    2.12 -    /* Sanity check the hot spot */
    2.13 -    if ((hot_x < 0) || (hot_y < 0) || (hot_x >= w) || (hot_y >= h)) {
    2.14 -        SDL_SetError("Cursor hot spot doesn't lie within cursor");
    2.15 -        return NULL;
    2.16 -    }
    2.17 -
    2.18      /* Make sure the width is a multiple of 8 */
    2.19      w = ((w + 7) & ~7);
    2.20  
    2.21      /* Create the surface from a bitmap */
    2.22 -    surface =
    2.23 -        SDL_CreateRGBSurface(0, w, h, 32, 0x00FF0000, 0x0000FF00, 0x000000FF,
    2.24 -                             0xFF000000);
    2.25 +    surface = SDL_CreateRGBSurface(0, w, h, 32,
    2.26 +                                   0x00FF0000,
    2.27 +                                   0x0000FF00,
    2.28 +                                   0x000000FF,
    2.29 +                                   0xFF000000);
    2.30      if (!surface) {
    2.31          return NULL;
    2.32      }
    2.33 @@ -398,13 +389,54 @@
    2.34          }
    2.35      }
    2.36  
    2.37 +    cursor = SDL_CreateColorCursor(surface, hot_x, hot_y);
    2.38 +
    2.39 +    SDL_FreeSurface(surface);
    2.40 +
    2.41 +    return cursor;
    2.42 +}
    2.43 +
    2.44 +SDL_Cursor *
    2.45 +SDL_CreateColorCursor(SDL_Surface *surface, int hot_x, int hot_y)
    2.46 +{
    2.47 +    SDL_Mouse *mouse = SDL_GetMouse();
    2.48 +    SDL_Surface *temp = NULL;
    2.49 +    SDL_Cursor *cursor;
    2.50 +
    2.51 +    if (!surface) {
    2.52 +        SDL_SetError("Passed NULL cursor surface");
    2.53 +        return NULL;
    2.54 +    }
    2.55 +
    2.56 +    if (!mouse->CreateCursor) {
    2.57 +        SDL_SetError("Cursors are not currently supported");
    2.58 +        return NULL;
    2.59 +    }
    2.60 +
    2.61 +    /* Sanity check the hot spot */
    2.62 +    if ((hot_x < 0) || (hot_y < 0) ||
    2.63 +        (hot_x >= surface->w) || (hot_y >= surface->h)) {
    2.64 +        SDL_SetError("Cursor hot spot doesn't lie within cursor");
    2.65 +        return NULL;
    2.66 +    }
    2.67 +
    2.68 +    if (surface->format->format != SDL_PIXELFORMAT_ARGB8888) {
    2.69 +        temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
    2.70 +        if (!temp) {
    2.71 +            return NULL;
    2.72 +        }
    2.73 +        surface = temp;
    2.74 +    }
    2.75 +
    2.76      cursor = mouse->CreateCursor(surface, hot_x, hot_y);
    2.77      if (cursor) {
    2.78          cursor->next = mouse->cursors;
    2.79          mouse->cursors = cursor;
    2.80      }
    2.81  
    2.82 -    SDL_FreeSurface(surface);
    2.83 +    if (temp) {
    2.84 +        SDL_FreeSurface(temp);
    2.85 +    }
    2.86  
    2.87      return cursor;
    2.88  }
     3.1 --- a/test/testcursor.c	Fri Mar 11 13:59:52 2011 -0800
     3.2 +++ b/test/testcursor.c	Fri Mar 11 14:14:38 2011 -0800
     3.3 @@ -141,13 +141,34 @@
     3.4      return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
     3.5  }
     3.6  
     3.7 +SDL_Surface *
     3.8 +LoadSprite(char *file)
     3.9 +{
    3.10 +    SDL_Surface *sprite;
    3.11 +
    3.12 +    /* Load the sprite image */
    3.13 +    sprite = SDL_LoadBMP(file);
    3.14 +    if (sprite == NULL) {
    3.15 +        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    3.16 +        return NULL;
    3.17 +    }
    3.18 +
    3.19 +    /* Set transparent pixel as the pixel at (0,0) */
    3.20 +    if (sprite->format->palette) {
    3.21 +        SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
    3.22 +                        *(Uint8 *) sprite->pixels);
    3.23 +    }
    3.24 +
    3.25 +    /* We're ready to roll. :) */
    3.26 +    return sprite;
    3.27 +}
    3.28  
    3.29  int
    3.30  main(int argc, char *argv[])
    3.31  {
    3.32      SDL_Surface *screen;
    3.33      SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
    3.34 -    SDL_Cursor *cursor[4];
    3.35 +    SDL_Cursor *cursor[5];
    3.36      int current;
    3.37  
    3.38      /* Load the SDL library */
    3.39 @@ -189,9 +210,10 @@
    3.40          SDL_Quit();
    3.41          return (1);
    3.42      }
    3.43 -    cursor[3] = SDL_GetCursor();
    3.44 +    cursor[3] = SDL_CreateColorCursor(LoadSprite("icon.bmp"), 0, 0);
    3.45 +    cursor[4] = SDL_GetCursor();
    3.46  
    3.47 -    current = 0;
    3.48 +    current = SDL_arraysize(cursor)-1;
    3.49      SDL_SetCursor(cursor[current]);
    3.50  
    3.51      while (!quit) {
    3.52 @@ -216,9 +238,9 @@
    3.53          SDL_Delay(1);
    3.54      }
    3.55  
    3.56 -    SDL_FreeCursor(cursor[0]);
    3.57 -    SDL_FreeCursor(cursor[1]);
    3.58 -    SDL_FreeCursor(cursor[2]);
    3.59 +    for (current = 0; current < SDL_arraysize(cursor); ++current) {
    3.60 +        SDL_FreeCursor(cursor[current]);
    3.61 +    }
    3.62  
    3.63      SDL_Quit();
    3.64      return (0);