Fix for bug #240
authorSam Lantinga <slouken@libsdl.org>
Sat, 24 Jun 2006 17:36:55 +0000
changeset 18832780f547f5e7
parent 1882 339d733e3699
child 1884 17af3557191c
Fix for bug #240

Christian Walther contributed Cocoa cursor code.
src/video/quartz/SDL_QuartzWM.m
test/testcursor.c
     1.1 --- a/src/video/quartz/SDL_QuartzWM.m	Sat Jun 24 04:30:01 2006 +0000
     1.2 +++ b/src/video/quartz/SDL_QuartzWM.m	Sat Jun 24 17:36:55 2006 +0000
     1.3 @@ -25,49 +25,69 @@
     1.4  
     1.5  
     1.6  struct WMcursor {
     1.7 -    Cursor curs;
     1.8 +    NSCursor *nscursor;
     1.9  };
    1.10  
    1.11  void QZ_FreeWMCursor     (_THIS, WMcursor *cursor) { 
    1.12  
    1.13 -    if ( cursor != NULL )
    1.14 +    if ( cursor != NULL ) {
    1.15 +        [ cursor->nscursor release ];
    1.16          free (cursor);
    1.17 +    }
    1.18  }
    1.19  
    1.20 -/* Use the Carbon cursor routines for now */
    1.21  WMcursor*    QZ_CreateWMCursor   (_THIS, Uint8 *data, Uint8 *mask, 
    1.22                                           int w, int h, int hot_x, int hot_y) { 
    1.23      WMcursor *cursor;
    1.24 -    int row, bytes;
    1.25 -        
    1.26 +    NSBitmapImageRep *imgrep;
    1.27 +    NSImage *img;
    1.28 +    unsigned char *planes[5];
    1.29 +    int i;
    1.30 +    NSAutoreleasePool *pool;
    1.31 +    
    1.32 +    pool = [ [ NSAutoreleasePool alloc ] init ];
    1.33 +    
    1.34      /* Allocate the cursor memory */
    1.35      cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor));
    1.36 -    if ( cursor == NULL ) {
    1.37 -        SDL_OutOfMemory();
    1.38 -        return(NULL);
    1.39 +    if (cursor == NULL) goto outOfMemory;
    1.40 +    
    1.41 +    /* create the image representation and get the pointers to its storage */
    1.42 +    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.43 +    if (imgrep == nil) goto outOfMemory;
    1.44 +    [ imgrep getBitmapDataPlanes: planes ];
    1.45 +    
    1.46 +    /* 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.47 +    for (i = 0; i < (w+7)/8*h; i++) {
    1.48 +        planes[0][i] = data[i];
    1.49 +        planes[1][i] = mask[i] | data[i];
    1.50      }
    1.51 -    SDL_memset(cursor, 0, sizeof(*cursor));
    1.52      
    1.53 -    if (w > 16)
    1.54 -        w = 16;
    1.55 +    /* create image and cursor */
    1.56 +    img = [ [ [ NSImage alloc ] initWithSize: NSMakeSize(w, h) ] autorelease ];
    1.57 +    if (img == nil) goto outOfMemory;
    1.58 +    [ img addRepresentation: imgrep ];
    1.59 +    if (system_version < 0x1030) { /* on 10.2, cursors must be 16*16 */
    1.60 +        if (w > 16 || h > 16) { /* too big: scale it down */
    1.61 +            [ img setScalesWhenResized: YES ];
    1.62 +            hot_x = hot_x*16/w;
    1.63 +            hot_y = hot_y*16/h;
    1.64 +        }
    1.65 +        else { /* too small (or just right): extend it (from the bottom left corner, so hot_y must be adjusted) */
    1.66 +            hot_y += 16 - h;
    1.67 +        }
    1.68 +        [ img setSize: NSMakeSize(16, 16) ];
    1.69 +    }
    1.70 +    cursor->nscursor = [ [ NSCursor alloc ] initWithImage: img hotSpot: NSMakePoint(hot_x, hot_y) ];
    1.71 +    if (cursor->nscursor == nil) goto outOfMemory;
    1.72      
    1.73 -    if (h > 16)
    1.74 -        h = 16;
    1.75 -    
    1.76 -    bytes = (w+7)/8;
    1.77 +    [ pool release ];
    1.78 +    return(cursor);
    1.79  
    1.80 -    for ( row=0; row<h; ++row ) {
    1.81 -        SDL_memcpy(&cursor->curs.data[row], data, bytes);
    1.82 -        data += bytes;
    1.83 -    }
    1.84 -    for ( row=0; row<h; ++row ) {
    1.85 -        SDL_memcpy(&cursor->curs.mask[row], mask, bytes);
    1.86 -        mask += bytes;
    1.87 -    }
    1.88 -    cursor->curs.hotSpot.h = hot_x;
    1.89 -    cursor->curs.hotSpot.v = hot_y;
    1.90 -    
    1.91 -    return(cursor);
    1.92 +outOfMemory:
    1.93 +    [ pool release ];
    1.94 +    if (cursor != NULL) SDL_free(cursor);
    1.95 +    SDL_OutOfMemory();
    1.96 +    return(NULL);
    1.97  }
    1.98  
    1.99  void QZ_ShowMouse (_THIS) {
   1.100 @@ -103,7 +123,7 @@
   1.101          }
   1.102      }
   1.103      else {
   1.104 -        SetCursor(&cursor->curs);
   1.105 +        [ cursor->nscursor set ];
   1.106          if ( ! cursor_should_be_visible ) {
   1.107              QZ_ShowMouse (this);
   1.108              cursor_should_be_visible = YES;
     2.1 --- a/test/testcursor.c	Sat Jun 24 04:30:01 2006 +0000
     2.2 +++ b/test/testcursor.c	Sat Jun 24 17:36:55 2006 +0000
     2.3 @@ -55,6 +55,10 @@
     2.4  	0xff00
     2.5  };
     2.6  
     2.7 +/* another test cursor: smaller than 16x16, and with an odd height */
     2.8 +
     2.9 +Uint8 small_cursor_data[11] = { 0x00, 0x18, 0x08, 0x38, 0x44, 0x54, 0x44, 0x38, 0x20, 0x20, 0x00 };
    2.10 +Uint8 small_cursor_mask[11] = { 0x3C, 0x3C, 0x3C, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x78, 0x70, 0x70 };
    2.11  
    2.12  /* XPM */
    2.13  static const char *arrow[] = {
    2.14 @@ -139,7 +143,7 @@
    2.15  {
    2.16  	SDL_Surface *screen;
    2.17  	SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
    2.18 -	SDL_Cursor *cursor[2];
    2.19 +	SDL_Cursor *cursor[3];
    2.20  	int current;
    2.21  
    2.22  	/* Load the SDL library */
    2.23 @@ -170,6 +174,13 @@
    2.24  		SDL_Quit();
    2.25  		return(1);
    2.26  	}
    2.27 +	cursor[2] = SDL_CreateCursor(small_cursor_data, small_cursor_mask,
    2.28 +		8, 11, 3, 5);
    2.29 +	if (cursor[2]==NULL) {
    2.30 +		fprintf(stderr, "Couldn't initialize test cursor: %s\n",SDL_GetError());
    2.31 +		SDL_Quit();
    2.32 +		return(1);
    2.33 +	}
    2.34  
    2.35  	current = 0;
    2.36  	SDL_SetCursor(cursor[current]);
    2.37 @@ -179,7 +190,7 @@
    2.38  		while (SDL_PollEvent(&event)) {
    2.39  			switch(event.type) {
    2.40  				case SDL_MOUSEBUTTONDOWN:
    2.41 -					current = !current;
    2.42 +					current = (current + 1)%3;
    2.43  					SDL_SetCursor(cursor[current]);
    2.44  					break;
    2.45  				case SDL_KEYDOWN: