Merged fix for bug #240 from SDL 1.2 SDL-1.3
authorSam Lantinga <slouken@libsdl.org>
Sat, 24 Jun 2006 17:31:46 +0000
branchSDL-1.3
changeset 1708cd14138a8703
parent 1707 57ce47f033a5
child 1709 2047ec9bb665
Merged fix for bug #240 from SDL 1.2
src/video/quartz/SDL_QuartzWM.m
test/Makefile.in
test/testcursor.c
     1.1 --- a/src/video/quartz/SDL_QuartzWM.m	Sat Jun 24 17:01:29 2006 +0000
     1.2 +++ b/src/video/quartz/SDL_QuartzWM.m	Sat Jun 24 17:31:46 2006 +0000
     1.3 @@ -26,57 +26,82 @@
     1.4  
     1.5  struct WMcursor
     1.6  {
     1.7 -    Cursor curs;
     1.8 +    NSCursor *nscursor;
     1.9  };
    1.10  
    1.11  void
    1.12 -QZ_FreeWMCursor (_THIS, WMcursor * cursor)
    1.13 +QZ_FreeWMCursor(_THIS, WMcursor * cursor)
    1.14  {
    1.15  
    1.16 -    if (cursor != NULL)
    1.17 -        free (cursor);
    1.18 +    if (cursor != NULL) {
    1.19 +        [cursor->nscursor release];
    1.20 +        free(cursor);
    1.21 +    }
    1.22  }
    1.23  
    1.24 -/* Use the Carbon cursor routines for now */
    1.25  WMcursor *
    1.26 -QZ_CreateWMCursor (_THIS, Uint8 * data, Uint8 * mask,
    1.27 -                   int w, int h, int hot_x, int hot_y)
    1.28 +QZ_CreateWMCursor(_THIS, Uint8 * data, Uint8 * mask,
    1.29 +                  int w, int h, int hot_x, int hot_y)
    1.30  {
    1.31      WMcursor *cursor;
    1.32 -    int row, bytes;
    1.33 +    NSBitmapImageRep *imgrep;
    1.34 +    NSImage *img;
    1.35 +    unsigned char *planes[5];
    1.36 +    int i;
    1.37 +    NSAutoreleasePool *pool;
    1.38 +
    1.39 +    pool =[[NSAutoreleasePool alloc] init];
    1.40  
    1.41      /* Allocate the cursor memory */
    1.42 -    cursor = (WMcursor *) SDL_malloc (sizeof (WMcursor));
    1.43 -    if (cursor == NULL) {
    1.44 -        SDL_OutOfMemory ();
    1.45 -        return (NULL);
    1.46 +    cursor = (WMcursor *) SDL_malloc(sizeof(WMcursor));
    1.47 +    if (cursor == NULL)
    1.48 +        goto outOfMemory;
    1.49 +
    1.50 +    /* create the image representation and get the pointers to its storage */
    1.51 +  imgrep =[[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL pixelsWide: w pixelsHigh: h bitsPerSample: 1 samplesPerPixel: 2 hasAlpha: YES isPlanar: YES colorSpaceName: NSDeviceBlackColorSpace bytesPerRow: (w + 7) / 8 bitsPerPixel:0] autorelease];
    1.52 +    if (imgrep == nil)
    1.53 +        goto outOfMemory;
    1.54 +  [imgrep getBitmapDataPlanes:planes];
    1.55 +
    1.56 +    /* copy data and mask, extending the mask to all black pixels because the inversion effect doesn't work with Cocoa's alpha-blended cursors */
    1.57 +    for (i = 0; i < (w + 7) / 8 * h; i++) {
    1.58 +        planes[0][i] = data[i];
    1.59 +        planes[1][i] = mask[i] | data[i];
    1.60      }
    1.61 -    SDL_memset (cursor, 0, sizeof (*cursor));
    1.62  
    1.63 -    if (w > 16)
    1.64 -        w = 16;
    1.65 +    /* create image and cursor */
    1.66 +  img =[[[NSImage alloc] initWithSize:NSMakeSize(w, h)] autorelease];
    1.67 +    if (img == nil)
    1.68 +        goto outOfMemory;
    1.69 +  [img addRepresentation:imgrep];
    1.70 +    if (system_version < 0x1030) {      /* on 10.2, cursors must be 16*16 */
    1.71 +        if (w > 16 || h > 16) { /* too big: scale it down */
    1.72 +          [img setScalesWhenResized:YES];
    1.73 +            hot_x = hot_x * 16 / w;
    1.74 +            hot_y = hot_y * 16 / h;
    1.75 +        } else {                /* too small (or just right): extend it (from the bottom left corner, so hot_y must be adjusted) */
    1.76 +            hot_y += 16 - h;
    1.77 +        }
    1.78 +      [img setSize:NSMakeSize(16, 16)];
    1.79 +    }
    1.80 +  cursor->nscursor =[[NSCursor alloc] initWithImage: img hotSpot:NSMakePoint(hot_x,
    1.81 +                hot_y)];
    1.82 +    if (cursor->nscursor == nil)
    1.83 +        goto outOfMemory;
    1.84  
    1.85 -    if (h > 16)
    1.86 -        h = 16;
    1.87 +    [pool release];
    1.88 +    return (cursor);
    1.89  
    1.90 -    bytes = (w + 7) / 8;
    1.91 -
    1.92 -    for (row = 0; row < h; ++row) {
    1.93 -        SDL_memcpy (&cursor->curs.data[row], data, bytes);
    1.94 -        data += bytes;
    1.95 -    }
    1.96 -    for (row = 0; row < h; ++row) {
    1.97 -        SDL_memcpy (&cursor->curs.mask[row], mask, bytes);
    1.98 -        mask += bytes;
    1.99 -    }
   1.100 -    cursor->curs.hotSpot.h = hot_x;
   1.101 -    cursor->curs.hotSpot.v = hot_y;
   1.102 -
   1.103 -    return (cursor);
   1.104 +  outOfMemory:
   1.105 +    [pool release];
   1.106 +    if (cursor != NULL)
   1.107 +        SDL_free(cursor);
   1.108 +    SDL_OutOfMemory();
   1.109 +    return (NULL);
   1.110  }
   1.111  
   1.112  void
   1.113 -QZ_ShowMouse (_THIS)
   1.114 +QZ_ShowMouse(_THIS)
   1.115  {
   1.116      if (!cursor_visible) {
   1.117          [NSCursor unhide];
   1.118 @@ -85,42 +110,42 @@
   1.119  }
   1.120  
   1.121  void
   1.122 -QZ_HideMouse (_THIS)
   1.123 +QZ_HideMouse(_THIS)
   1.124  {
   1.125 -    if ((SDL_GetAppState () & SDL_APPMOUSEFOCUS) && cursor_visible) {
   1.126 +    if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS) && cursor_visible) {
   1.127          [NSCursor hide];
   1.128          cursor_visible = NO;
   1.129      }
   1.130  }
   1.131  
   1.132  BOOL
   1.133 -QZ_IsMouseInWindow (_THIS)
   1.134 +QZ_IsMouseInWindow(_THIS)
   1.135  {
   1.136      if (qz_window == nil)
   1.137          return YES;             /*fullscreen */
   1.138      else {
   1.139          NSPoint p =[qz_window mouseLocationOutsideOfEventStream];
   1.140          p.y -= 1.0f;            /* Apparently y goes from 1 to h, not from 0 to h-1 (i.e. the "location of the mouse" seems to be defined as "the location of the top left corner of the mouse pointer's hot pixel" */
   1.141 -        return NSPointInRect (p,[window_view frame]);
   1.142 +        return NSPointInRect(p,[window_view frame]);
   1.143      }
   1.144  }
   1.145  
   1.146  int
   1.147 -QZ_ShowWMCursor (_THIS, WMcursor * cursor)
   1.148 +QZ_ShowWMCursor(_THIS, WMcursor * cursor)
   1.149  {
   1.150  
   1.151      if (cursor == NULL) {
   1.152          if (cursor_should_be_visible) {
   1.153 -            QZ_HideMouse (this);
   1.154 +            QZ_HideMouse(this);
   1.155              cursor_should_be_visible = NO;
   1.156 -            QZ_ChangeGrabState (this, QZ_HIDECURSOR);
   1.157 +            QZ_ChangeGrabState(this, QZ_HIDECURSOR);
   1.158          }
   1.159      } else {
   1.160 -        SetCursor (&cursor->curs);
   1.161 +        [cursor->nscursor set];
   1.162          if (!cursor_should_be_visible) {
   1.163 -            QZ_ShowMouse (this);
   1.164 +            QZ_ShowMouse(this);
   1.165              cursor_should_be_visible = YES;
   1.166 -            QZ_ChangeGrabState (this, QZ_SHOWCURSOR);
   1.167 +            QZ_ChangeGrabState(this, QZ_SHOWCURSOR);
   1.168          }
   1.169      }
   1.170  
   1.171 @@ -137,7 +162,7 @@
   1.172  
   1.173  /* Convert Cocoa screen coordinate to Cocoa window coordinate */
   1.174  void
   1.175 -QZ_PrivateGlobalToLocal (_THIS, NSPoint * p)
   1.176 +QZ_PrivateGlobalToLocal(_THIS, NSPoint * p)
   1.177  {
   1.178  
   1.179    *p =[qz_window convertScreenToBase:*p];
   1.180 @@ -146,7 +171,7 @@
   1.181  
   1.182  /* Convert Cocoa window coordinate to Cocoa screen coordinate */
   1.183  void
   1.184 -QZ_PrivateLocalToGlobal (_THIS, NSPoint * p)
   1.185 +QZ_PrivateLocalToGlobal(_THIS, NSPoint * p)
   1.186  {
   1.187  
   1.188    *p =[qz_window convertBaseToScreen:*p];
   1.189 @@ -154,18 +179,18 @@
   1.190  
   1.191  /* Convert SDL coordinate to Cocoa coordinate */
   1.192  void
   1.193 -QZ_PrivateSDLToCocoa (_THIS, NSPoint * p)
   1.194 +QZ_PrivateSDLToCocoa(_THIS, NSPoint * p)
   1.195  {
   1.196  
   1.197 -    if (CGDisplayIsCaptured (display_id)) {     /* capture signals fullscreen */
   1.198 +    if (CGDisplayIsCaptured(display_id)) {      /* capture signals fullscreen */
   1.199  
   1.200 -        p->y = CGDisplayPixelsHigh (display_id) - p->y;
   1.201 +        p->y = CGDisplayPixelsHigh(display_id) - p->y;
   1.202      } else {
   1.203  
   1.204        *p =[window_view convertPoint: *p toView:nil];
   1.205  
   1.206          /* We need a workaround in OpenGL mode */
   1.207 -        if (SDL_VideoSurface->flags & SDL_INTERNALOPENGL) {
   1.208 +        if (SDL_VideoSurface->flags & SDL_OPENGL) {
   1.209              p->y =[window_view frame].size.height - p->y;
   1.210          }
   1.211      }
   1.212 @@ -173,19 +198,19 @@
   1.213  
   1.214  /* Convert Cocoa coordinate to SDL coordinate */
   1.215  void
   1.216 -QZ_PrivateCocoaToSDL (_THIS, NSPoint * p)
   1.217 +QZ_PrivateCocoaToSDL(_THIS, NSPoint * p)
   1.218  {
   1.219  
   1.220 -    if (CGDisplayIsCaptured (display_id)) {     /* capture signals fullscreen */
   1.221 +    if (CGDisplayIsCaptured(display_id)) {      /* capture signals fullscreen */
   1.222  
   1.223 -        p->y = CGDisplayPixelsHigh (display_id) - p->y;
   1.224 +        p->y = CGDisplayPixelsHigh(display_id) - p->y;
   1.225      } else {
   1.226  
   1.227        *p =[window_view convertPoint: *p fromView:nil];
   1.228  
   1.229          /* We need a workaround in OpenGL mode */
   1.230          if (SDL_VideoSurface != NULL
   1.231 -            && (SDL_VideoSurface->flags & SDL_INTERNALOPENGL)) {
   1.232 +            && (SDL_VideoSurface->flags & SDL_OPENGL)) {
   1.233              p->y =[window_view frame].size.height - p->y;
   1.234          }
   1.235      }
   1.236 @@ -193,19 +218,19 @@
   1.237  
   1.238  /* Convert SDL coordinate to window server (CoreGraphics) coordinate */
   1.239  CGPoint
   1.240 -QZ_PrivateSDLToCG (_THIS, NSPoint * p)
   1.241 +QZ_PrivateSDLToCG(_THIS, NSPoint * p)
   1.242  {
   1.243  
   1.244      CGPoint cgp;
   1.245  
   1.246 -    if (!CGDisplayIsCaptured (display_id)) {    /* not captured => not fullscreen => local coord */
   1.247 +    if (!CGDisplayIsCaptured(display_id)) {     /* not captured => not fullscreen => local coord */
   1.248  
   1.249          int height;
   1.250  
   1.251 -        QZ_PrivateSDLToCocoa (this, p);
   1.252 -        QZ_PrivateLocalToGlobal (this, p);
   1.253 +        QZ_PrivateSDLToCocoa(this, p);
   1.254 +        QZ_PrivateLocalToGlobal(this, p);
   1.255  
   1.256 -        height = CGDisplayPixelsHigh (display_id);
   1.257 +        height = CGDisplayPixelsHigh(display_id);
   1.258          p->y = height - p->y;
   1.259      }
   1.260  
   1.261 @@ -218,40 +243,40 @@
   1.262  #if 0                           /* Dead code */
   1.263  /* Convert window server (CoreGraphics) coordinate to SDL coordinate */
   1.264  void
   1.265 -QZ_PrivateCGToSDL (_THIS, NSPoint * p)
   1.266 +QZ_PrivateCGToSDL(_THIS, NSPoint * p)
   1.267  {
   1.268  
   1.269 -    if (!CGDisplayIsCaptured (display_id)) {    /* not captured => not fullscreen => local coord */
   1.270 +    if (!CGDisplayIsCaptured(display_id)) {     /* not captured => not fullscreen => local coord */
   1.271  
   1.272          int height;
   1.273  
   1.274          /* Convert CG Global to Cocoa Global */
   1.275 -        height = CGDisplayPixelsHigh (display_id);
   1.276 +        height = CGDisplayPixelsHigh(display_id);
   1.277          p->y = height - p->y;
   1.278  
   1.279 -        QZ_PrivateGlobalToLocal (this, p);
   1.280 -        QZ_PrivateCocoaToSDL (this, p);
   1.281 +        QZ_PrivateGlobalToLocal(this, p);
   1.282 +        QZ_PrivateCocoaToSDL(this, p);
   1.283      }
   1.284  }
   1.285  #endif /* Dead code */
   1.286  
   1.287  void
   1.288 -QZ_PrivateWarpCursor (_THIS, int x, int y)
   1.289 +QZ_PrivateWarpCursor(_THIS, int x, int y)
   1.290  {
   1.291  
   1.292      NSPoint p;
   1.293      CGPoint cgp;
   1.294  
   1.295 -    p = NSMakePoint (x, y);
   1.296 -    cgp = QZ_PrivateSDLToCG (this, &p);
   1.297 +    p = NSMakePoint(x, y);
   1.298 +    cgp = QZ_PrivateSDLToCG(this, &p);
   1.299  
   1.300      /* this is the magic call that fixes cursor "freezing" after warp */
   1.301 -    CGSetLocalEventsSuppressionInterval (0.0);
   1.302 -    CGWarpMouseCursorPosition (cgp);
   1.303 +    CGSetLocalEventsSuppressionInterval(0.0);
   1.304 +    CGWarpMouseCursorPosition(cgp);
   1.305  }
   1.306  
   1.307  void
   1.308 -QZ_WarpWMCursor (_THIS, Uint16 x, Uint16 y)
   1.309 +QZ_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
   1.310  {
   1.311  
   1.312      /* Only allow warping when in foreground */
   1.313 @@ -260,23 +285,23 @@
   1.314  
   1.315      /* Do the actual warp */
   1.316      if (grab_state != QZ_INVISIBLE_GRAB)
   1.317 -        QZ_PrivateWarpCursor (this, x, y);
   1.318 +        QZ_PrivateWarpCursor(this, x, y);
   1.319  
   1.320      /* Generate the mouse moved event */
   1.321 -    SDL_PrivateMouseMotion (0, 0, x, y);
   1.322 +    SDL_PrivateMouseMotion(0, 0, x, y);
   1.323  }
   1.324  
   1.325  void
   1.326 -QZ_MoveWMCursor (_THIS, int x, int y)
   1.327 +QZ_MoveWMCursor(_THIS, int x, int y)
   1.328  {
   1.329  }
   1.330  void
   1.331 -QZ_CheckMouseMode (_THIS)
   1.332 +QZ_CheckMouseMode(_THIS)
   1.333  {
   1.334  }
   1.335  
   1.336  void
   1.337 -QZ_SetCaption (_THIS, const char *title, const char *icon)
   1.338 +QZ_SetCaption(_THIS, const char *title, const char *icon)
   1.339  {
   1.340  
   1.341      if (qz_window != nil) {
   1.342 @@ -295,7 +320,7 @@
   1.343  }
   1.344  
   1.345  void
   1.346 -QZ_SetIcon (_THIS, SDL_Surface * icon, Uint8 * mask)
   1.347 +QZ_SetIcon(_THIS, SDL_Surface * icon, Uint8 * mask)
   1.348  {
   1.349      NSBitmapImageRep *imgrep;
   1.350      NSImage *img;
   1.351 @@ -312,7 +337,7 @@
   1.352      if (imgrep == nil)
   1.353          goto freePool;
   1.354      pixels =[imgrep bitmapData];
   1.355 -    SDL_memset (pixels, 0, 4 * icon->w * icon->h);      /* make the background, which will survive in colorkeyed areas, completely transparent */
   1.356 +    SDL_memset(pixels, 0, 4 * icon->w * icon->h);       /* make the background, which will survive in colorkeyed areas, completely transparent */
   1.357  
   1.358  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   1.359  #define BYTEORDER_DEPENDENT_RGBA_MASKS 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
   1.360 @@ -320,20 +345,20 @@
   1.361  #define BYTEORDER_DEPENDENT_RGBA_MASKS 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
   1.362  #endif
   1.363      mergedSurface =
   1.364 -        SDL_CreateRGBSurfaceFrom (pixels, icon->w, icon->h, 32, 4 * icon->w,
   1.365 -                                  BYTEORDER_DEPENDENT_RGBA_MASKS);
   1.366 +        SDL_CreateRGBSurfaceFrom(pixels, icon->w, icon->h, 32, 4 * icon->w,
   1.367 +                                 BYTEORDER_DEPENDENT_RGBA_MASKS);
   1.368      if (mergedSurface == NULL)
   1.369          goto freePool;
   1.370  
   1.371      /* blit, with temporarily cleared SRCALPHA flag because we want to copy, not alpha-blend */
   1.372      iconSrcAlpha = ((icon->flags & SDL_SRCALPHA) != 0);
   1.373      iconAlphaValue = icon->format->alpha;
   1.374 -    SDL_SetAlpha (icon, 0, 255);
   1.375 -    SDL_BlitSurface (icon, NULL, mergedSurface, NULL);
   1.376 +    SDL_SetAlpha(icon, 0, 255);
   1.377 +    SDL_BlitSurface(icon, NULL, mergedSurface, NULL);
   1.378      if (iconSrcAlpha)
   1.379 -        SDL_SetAlpha (icon, SDL_SRCALPHA, iconAlphaValue);
   1.380 +        SDL_SetAlpha(icon, SDL_SRCALPHA, iconAlphaValue);
   1.381  
   1.382 -    SDL_FreeSurface (mergedSurface);
   1.383 +    SDL_FreeSurface(mergedSurface);
   1.384  
   1.385      /* apply mask, source alpha, and premultiply color values by alpha */
   1.386      maskPitch = (icon->w + 7) / 8;
   1.387 @@ -361,8 +386,8 @@
   1.388          }
   1.389      }
   1.390  
   1.391 -  img =[[[NSImage alloc] initWithSize:NSMakeSize (icon->w,
   1.392 -                icon->h)] autorelease];
   1.393 +  img =[[[NSImage alloc] initWithSize:NSMakeSize(icon->w,
   1.394 +               icon->h)] autorelease];
   1.395      if (img == nil)
   1.396          goto freePool;
   1.397    [img addRepresentation:imgrep];
   1.398 @@ -373,14 +398,14 @@
   1.399  }
   1.400  
   1.401  int
   1.402 -QZ_IconifyWindow (_THIS)
   1.403 +QZ_IconifyWindow(_THIS)
   1.404  {
   1.405  
   1.406      if (![qz_window isMiniaturized]) {
   1.407        [qz_window miniaturize:nil];
   1.408          return 1;
   1.409      } else {
   1.410 -        SDL_SetError ("window already iconified");
   1.411 +        SDL_SetError("window already iconified");
   1.412          return 0;
   1.413      }
   1.414  }
   1.415 @@ -392,7 +417,7 @@
   1.416  }*/
   1.417  
   1.418  void
   1.419 -QZ_ChangeGrabState (_THIS, int action)
   1.420 +QZ_ChangeGrabState(_THIS, int action)
   1.421  {
   1.422  
   1.423      /* 
   1.424 @@ -412,7 +437,7 @@
   1.425          else if (action == QZ_HIDECURSOR)
   1.426              grab_state = QZ_INVISIBLE_GRAB;
   1.427      } else {
   1.428 -        assert (grab_state == QZ_INVISIBLE_GRAB);
   1.429 +        assert(grab_state == QZ_INVISIBLE_GRAB);
   1.430  
   1.431          if (action == QZ_DISABLE_GRAB)
   1.432              grab_state = QZ_UNGRABBED;
   1.433 @@ -423,28 +448,28 @@
   1.434      /* now apply the new state */
   1.435      if (grab_state == QZ_UNGRABBED) {
   1.436  
   1.437 -        CGAssociateMouseAndMouseCursorPosition (1);
   1.438 +        CGAssociateMouseAndMouseCursorPosition(1);
   1.439      } else if (grab_state == QZ_VISIBLE_GRAB) {
   1.440  
   1.441 -        CGAssociateMouseAndMouseCursorPosition (1);
   1.442 +        CGAssociateMouseAndMouseCursorPosition(1);
   1.443      } else {
   1.444 -        assert (grab_state == QZ_INVISIBLE_GRAB);
   1.445 +        assert(grab_state == QZ_INVISIBLE_GRAB);
   1.446  
   1.447 -        QZ_PrivateWarpCursor (this, SDL_VideoSurface->w / 2,
   1.448 -                              SDL_VideoSurface->h / 2);
   1.449 -        CGAssociateMouseAndMouseCursorPosition (0);
   1.450 +        QZ_PrivateWarpCursor(this, SDL_VideoSurface->w / 2,
   1.451 +                             SDL_VideoSurface->h / 2);
   1.452 +        CGAssociateMouseAndMouseCursorPosition(0);
   1.453      }
   1.454  }
   1.455  
   1.456  SDL_GrabMode
   1.457 -QZ_GrabInput (_THIS, SDL_GrabMode grab_mode)
   1.458 +QZ_GrabInput(_THIS, SDL_GrabMode grab_mode)
   1.459  {
   1.460  
   1.461      int doGrab = grab_mode & SDL_GRAB_ON;
   1.462      /*int fullscreen = grab_mode & SDL_GRAB_FULLSCREEN; */
   1.463  
   1.464      if (this->screen == NULL) {
   1.465 -        SDL_SetError ("QZ_GrabInput: screen is NULL");
   1.466 +        SDL_SetError("QZ_GrabInput: screen is NULL");
   1.467          return SDL_GRAB_OFF;
   1.468      }
   1.469  
   1.470 @@ -456,9 +481,9 @@
   1.471  
   1.472      if (grab_mode != SDL_GRAB_QUERY) {
   1.473          if (doGrab)
   1.474 -            QZ_ChangeGrabState (this, QZ_ENABLE_GRAB);
   1.475 +            QZ_ChangeGrabState(this, QZ_ENABLE_GRAB);
   1.476          else
   1.477 -            QZ_ChangeGrabState (this, QZ_DISABLE_GRAB);
   1.478 +            QZ_ChangeGrabState(this, QZ_DISABLE_GRAB);
   1.479  
   1.480          current_grab_mode = doGrab ? SDL_GRAB_ON : SDL_GRAB_OFF;
   1.481      }
     2.1 --- a/test/Makefile.in	Sat Jun 24 17:01:29 2006 +0000
     2.2 +++ b/test/Makefile.in	Sat Jun 24 17:31:46 2006 +0000
     2.3 @@ -7,7 +7,7 @@
     2.4  CFLAGS  = @CFLAGS@
     2.5  LIBS	= @LIBS@
     2.6  
     2.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE)
     2.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE)
     2.9  
    2.10  all: $(TARGETS)
    2.11  
    2.12 @@ -32,6 +32,9 @@
    2.13  testcdrom$(EXE): $(srcdir)/testcdrom.c
    2.14  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    2.15  
    2.16 +testcursor$(EXE): $(srcdir)/testcursor.c
    2.17 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    2.18 +
    2.19  testdyngl$(EXE): $(srcdir)/testdyngl.c
    2.20  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    2.21  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/testcursor.c	Sat Jun 24 17:31:46 2006 +0000
     3.3 @@ -0,0 +1,223 @@
     3.4 +#include <stdio.h>
     3.5 +#include <stdlib.h>
     3.6 +
     3.7 +#include "SDL.h"
     3.8 +
     3.9 +/* This is an example 16x16 cursor
    3.10 +	top left :	black
    3.11 +	top right : inverted color or black
    3.12 +	bottom left: white
    3.13 +	bottom right: transparent
    3.14 +	(swap left and right for different endianness)
    3.15 +*/
    3.16 +
    3.17 +Uint16 cursor_data[16] = {
    3.18 +    0xffff,
    3.19 +    0xffff,
    3.20 +    0xffff,
    3.21 +    0xffff,
    3.22 +
    3.23 +    0xffff,
    3.24 +    0xffff,
    3.25 +    0xffff,
    3.26 +    0xffff,
    3.27 +
    3.28 +    0x0000,
    3.29 +    0x0000,
    3.30 +    0x0000,
    3.31 +    0x0000,
    3.32 +
    3.33 +    0x0000,
    3.34 +    0x0000,
    3.35 +    0x0000,
    3.36 +    0x0000
    3.37 +};
    3.38 +
    3.39 +Uint16 cursor_mask[16] = {
    3.40 +    0xff00,
    3.41 +    0xff00,
    3.42 +    0xff00,
    3.43 +    0xff00,
    3.44 +
    3.45 +    0xff00,
    3.46 +    0xff00,
    3.47 +    0xff00,
    3.48 +    0xff00,
    3.49 +
    3.50 +    0xff00,
    3.51 +    0xff00,
    3.52 +    0xff00,
    3.53 +    0xff00,
    3.54 +
    3.55 +    0xff00,
    3.56 +    0xff00,
    3.57 +    0xff00,
    3.58 +    0xff00
    3.59 +};
    3.60 +
    3.61 +/* another test cursor: smaller than 16x16, and with an odd height */
    3.62 +
    3.63 +Uint8 small_cursor_data[11] =
    3.64 +    { 0x00, 0x18, 0x08, 0x38, 0x44, 0x54, 0x44, 0x38, 0x20, 0x20, 0x00 };
    3.65 +Uint8 small_cursor_mask[11] =
    3.66 +    { 0x3C, 0x3C, 0x3C, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x78, 0x70, 0x70 };
    3.67 +
    3.68 +/* XPM */
    3.69 +static const char *arrow[] = {
    3.70 +    /* width height num_colors chars_per_pixel */
    3.71 +    "    32    32        3            1",
    3.72 +    /* colors */
    3.73 +    "X c #000000",
    3.74 +    ". c #ffffff",
    3.75 +    "  c None",
    3.76 +    /* pixels */
    3.77 +    "X                               ",
    3.78 +    "XX                              ",
    3.79 +    "X.X                             ",
    3.80 +    "X..X                            ",
    3.81 +    "X...X                           ",
    3.82 +    "X....X                          ",
    3.83 +    "X.....X                         ",
    3.84 +    "X......X                        ",
    3.85 +    "X.......X                       ",
    3.86 +    "X........X                      ",
    3.87 +    "X.....XXXXX                     ",
    3.88 +    "X..X..X                         ",
    3.89 +    "X.X X..X                        ",
    3.90 +    "XX  X..X                        ",
    3.91 +    "X    X..X                       ",
    3.92 +    "     X..X                       ",
    3.93 +    "      X..X                      ",
    3.94 +    "      X..X                      ",
    3.95 +    "       XX                       ",
    3.96 +    "                                ",
    3.97 +    "                                ",
    3.98 +    "                                ",
    3.99 +    "                                ",
   3.100 +    "                                ",
   3.101 +    "                                ",
   3.102 +    "                                ",
   3.103 +    "                                ",
   3.104 +    "                                ",
   3.105 +    "                                ",
   3.106 +    "                                ",
   3.107 +    "                                ",
   3.108 +    "                                ",
   3.109 +    "0,0"
   3.110 +};
   3.111 +
   3.112 +static SDL_Cursor *
   3.113 +create_arrow_cursor()
   3.114 +{
   3.115 +    int i, row, col;
   3.116 +    Uint8 data[4 * 32];
   3.117 +    Uint8 mask[4 * 32];
   3.118 +    int hot_x, hot_y;
   3.119 +
   3.120 +    i = -1;
   3.121 +    for (row = 0; row < 32; ++row) {
   3.122 +        for (col = 0; col < 32; ++col) {
   3.123 +            if (col % 8) {
   3.124 +                data[i] <<= 1;
   3.125 +                mask[i] <<= 1;
   3.126 +            } else {
   3.127 +                ++i;
   3.128 +                data[i] = mask[i] = 0;
   3.129 +            }
   3.130 +            switch (arrow[4 + row][col]) {
   3.131 +            case 'X':
   3.132 +                data[i] |= 0x01;
   3.133 +                mask[i] |= 0x01;
   3.134 +                break;
   3.135 +            case '.':
   3.136 +                mask[i] |= 0x01;
   3.137 +                break;
   3.138 +            case ' ':
   3.139 +                break;
   3.140 +            }
   3.141 +        }
   3.142 +    }
   3.143 +    sscanf(arrow[4 + row], "%d,%d", &hot_x, &hot_y);
   3.144 +    return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
   3.145 +}
   3.146 +
   3.147 +
   3.148 +int
   3.149 +main(int argc, char *argv[])
   3.150 +{
   3.151 +    SDL_Surface *screen;
   3.152 +    SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
   3.153 +    SDL_Cursor *cursor[3];
   3.154 +    int current;
   3.155 +
   3.156 +    /* Load the SDL library */
   3.157 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   3.158 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   3.159 +        return (1);
   3.160 +    }
   3.161 +
   3.162 +    screen = SDL_SetVideoMode(320, 200, 8, SDL_ANYFORMAT);
   3.163 +    if (screen == NULL) {
   3.164 +        fprintf(stderr, "Couldn't initialize video mode: %s\n",
   3.165 +                SDL_GetError());
   3.166 +        return (1);
   3.167 +    }
   3.168 +
   3.169 +    SDL_FillRect(screen, NULL, 0x664422);
   3.170 +
   3.171 +    cursor[0] = SDL_CreateCursor((Uint8 *) cursor_data, (Uint8 *) cursor_mask,
   3.172 +                                 16, 16, 8, 8);
   3.173 +    if (cursor[0] == NULL) {
   3.174 +        fprintf(stderr, "Couldn't initialize test cursor: %s\n",
   3.175 +                SDL_GetError());
   3.176 +        SDL_Quit();
   3.177 +        return (1);
   3.178 +    }
   3.179 +    cursor[1] = create_arrow_cursor();
   3.180 +    if (cursor[1] == NULL) {
   3.181 +        fprintf(stderr, "Couldn't initialize arrow cursor: %s\n",
   3.182 +                SDL_GetError());
   3.183 +        SDL_FreeCursor(cursor[0]);
   3.184 +        SDL_Quit();
   3.185 +        return (1);
   3.186 +    }
   3.187 +    cursor[2] = SDL_CreateCursor(small_cursor_data, small_cursor_mask,
   3.188 +                                 8, 11, 3, 5);
   3.189 +    if (cursor[2] == NULL) {
   3.190 +        fprintf(stderr, "Couldn't initialize test cursor: %s\n",
   3.191 +                SDL_GetError());
   3.192 +        SDL_Quit();
   3.193 +        return (1);
   3.194 +    }
   3.195 +
   3.196 +    current = 0;
   3.197 +    SDL_SetCursor(cursor[current]);
   3.198 +
   3.199 +    while (!quit) {
   3.200 +        SDL_Event event;
   3.201 +        while (SDL_PollEvent(&event)) {
   3.202 +            switch (event.type) {
   3.203 +            case SDL_MOUSEBUTTONDOWN:
   3.204 +                current = (current + 1) % 3;
   3.205 +                SDL_SetCursor(cursor[current]);
   3.206 +                break;
   3.207 +            case SDL_KEYDOWN:
   3.208 +                if (event.key.keysym.sym == SDLK_ESCAPE) {
   3.209 +                    quit = SDL_TRUE;
   3.210 +                }
   3.211 +                break;
   3.212 +            case SDL_QUIT:
   3.213 +                quit = SDL_TRUE;
   3.214 +                break;
   3.215 +            }
   3.216 +        }
   3.217 +        SDL_Flip(screen);
   3.218 +        SDL_Delay(1);
   3.219 +    }
   3.220 +
   3.221 +    SDL_FreeCursor(cursor[0]);
   3.222 +    SDL_FreeCursor(cursor[1]);
   3.223 +
   3.224 +    SDL_Quit();
   3.225 +    return (0);
   3.226 +}