src/video/cocoa/SDL_cocoashape.m
author egottlieb
Thu, 05 Aug 2010 21:27:32 -0400
changeset 4835 40b5293d0598
parent 4834 fd5bb97ec05d
child 4839 05d172e92b52
permissions -rw-r--r--
Finally figured out how to use autorelease pools.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4809
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
     1
/*
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
     3
    Copyright (C) 2010 Eli Gottlieb
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
     4
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
     9
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    13
    Lesser General Public License for more details.
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    14
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    16
    License along with this library; if not, write to the Free Software
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    18
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    19
    Eli Gottlieb
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    20
    eligottlieb@gmail.com
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    21
*/
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    22
4833
34fb492cefe3 Shaped windows for Cocoa build and testshape runs, but the actual shaping doesn't work. Something, however, is definitely happening, because we get the backgrounds drawn in two different colors.
egottlieb
parents: 4832
diff changeset
    23
#include "SDL_assert.h"
4827
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
    24
#include "SDL_stdinc.h"
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
    25
#include "SDL_cocoavideo.h"
4809
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    26
#include "SDL_shape.h"
4810
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    27
#include "SDL_cocoashape.h"
4831
8dabd625079f Building under OS X with command-line tools now works.
egottlieb
parents: 4830
diff changeset
    28
#include "../SDL_sysvideo.h"
4809
329708ffe2a7 Rejiggering the way shaped windows are created as preparation for OS X implementation. Fixed overdrive bug in test program that appears to have been introduced by someone other than myself.
Eli Gottlieb <eligottlieb@gmail.com>
parents:
diff changeset
    29
4810
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    30
SDL_WindowShaper* Cocoa_CreateShaper(SDL_Window* window) {
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    31
	SDL_WindowData* data = (SDL_WindowData*)window->driverdata;
4834
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    32
	[data->nswindow setOpaque:NO];
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    33
	[data->nswindow setBackgroundColor:[NSColor clearColor]];
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
    34
	[data->nswindow setStyleMask:NSBorderlessWindowMask];
4827
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
    35
	SDL_WindowShaper* result = SDL_malloc(sizeof(SDL_WindowShaper));
4810
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    36
	result->window = window;
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    37
	result->mode.mode = ShapeModeDefault;
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    38
	result->mode.parameters.binarizationCutoff = 1;
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    39
	result->usershownflag = 0;
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    40
	window->shaper = result;
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
    41
	
4827
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
    42
	SDL_ShapeData* shape_data = SDL_malloc(sizeof(SDL_ShapeData));
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
    43
	result->driverdata = shape_data;
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
    44
	shape_data->context = [data->nswindow graphicsContext];
4834
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    45
	SDL_assert(shape_data->context != NULL);
4827
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
    46
	shape_data->saved = SDL_FALSE;
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
    47
	shape_data->shape = NULL;
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
    48
	
4810
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    49
	int resized_properly = Cocoa_ResizeWindowShape(window);
4832
0c82f20327ec Correcting minor bugs and adding assertions to help me track down a NULL pointer bug in Cocoa code.
egottlieb
parents: 4831
diff changeset
    50
	SDL_assert(resized_properly == 0);
4810
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    51
	return result;
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    52
}
7a602fd2121f OK, it appears that dramatic hacks are not necessary to make Cocoa work...
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4809
diff changeset
    53
4829
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    54
typedef struct {
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    55
	NSBezierPath* clipPath;
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    56
	SDL_Window* window;
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    57
} SDL_PathConglomeration;
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    58
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    59
NSRect convert_rect(SDL_Rect rect,SDL_Window* window) {
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    60
	NSRect nsrect = NSMakeRect(rect.x,window->h-(rect.y+rect.h),rect.w,rect.h);
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    61
	return [[((SDL_WindowData*)window->driverdata)->nswindow contentView] convertRectFromBase:nsrect];
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    62
}
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    63
4830
3dfe779900f5 Compiling for cocoa now works. Does it run? We'll see.
egottlieb
parents: 4829
diff changeset
    64
void ConglomerateShapeTree(SDL_ShapeTree* tree,SDL_PathConglomeration* cong) {
4832
0c82f20327ec Correcting minor bugs and adding assertions to help me track down a NULL pointer bug in Cocoa code.
egottlieb
parents: 4831
diff changeset
    65
	SDL_assert(tree != NULL);
4829
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    66
	if(tree->kind == OpaqueShape) {
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    67
		NSRect rect = convert_rect(tree->data.shape,cong->window);
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    68
		[cong->clipPath appendBezierPathWithRect:rect];
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    69
	}
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    70
}
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    71
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
    72
int Cocoa_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
4827
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
    73
	SDL_ShapeData* data = (SDL_ShapeData*)shaper->driverdata;
4834
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    74
	/* if(data->saved == SDL_TRUE) {
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
    75
		[data->context restoreGraphicsState];
4827
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
    76
		data->saved = SDL_FALSE;
4834
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    77
	}*/
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
    78
		
4834
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    79
	/*[data->context saveGraphicsState];
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    80
	data->saved = SDL_TRUE;*/
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    81
	
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    82
	[NSGraphicsContext saveGraphicsState];
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
    83
	
4834
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    84
	[NSGraphicsContext setCurrentContext:data->context];
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    85
	SDL_WindowData* window_data = (SDL_WindowData*)shaper->window->driverdata;
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    86
	
4833
34fb492cefe3 Shaped windows for Cocoa build and testshape runs, but the actual shaping doesn't work. Something, however, is definitely happening, because we get the backgrounds drawn in two different colors.
egottlieb
parents: 4832
diff changeset
    87
	data->shape = SDL_CalculateShapeTree(*shapeMode,shape,SDL_FALSE);
4834
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    88
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
4835
40b5293d0598 Finally figured out how to use autorelease pools.
egottlieb
parents: 4834
diff changeset
    89
	NSBezierPath* clipPath = [NSBezierPath bezierPath];
4829
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    90
	
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    91
	SDL_PathConglomeration cong = {clipPath,shaper->window};
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    92
	
4830
3dfe779900f5 Compiling for cocoa now works. Does it run? We'll see.
egottlieb
parents: 4829
diff changeset
    93
	SDL_TraverseShapeTree(data->shape,(SDL_TraversalFunction)&ConglomerateShapeTree,(void*)&cong);
4829
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    94
	
4835
40b5293d0598 Finally figured out how to use autorelease pools.
egottlieb
parents: 4834
diff changeset
    95
	SDL_assert([NSGraphicsContext currentContext] != Nil);
4829
7160e833c4ac Implemented shaped windows for Cocoa. Still need to see if they actually work.
egottlieb
parents: 4827
diff changeset
    96
	[clipPath addClip];
4834
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    97
	
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    98
	[NSGraphicsContext restoreGraphicsState];
fd5bb97ec05d Improving Cocoa implementation and fixed a small bug in SDL_shape.c.
egottlieb
parents: 4833
diff changeset
    99
	[pool release];
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
   100
}
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
   101
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
   102
int Cocoa_ResizeWindowShape(SDL_Window *window) {
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
   103
	SDL_ShapeData* data = window->shaper->driverdata;
4832
0c82f20327ec Correcting minor bugs and adding assertions to help me track down a NULL pointer bug in Cocoa code.
egottlieb
parents: 4831
diff changeset
   104
	SDL_assert(data != NULL);
4827
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
   105
	
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
   106
	if(data->shape != NULL)
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
   107
		SDL_FreeShapeTree(&data->shape);
5660aac926e9 Got basic, pre-actually-writing-anything Cocoa to build.
egottlieb
parents: 4811
diff changeset
   108
	
4811
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
   109
	return 0;
d79939f20c45 Working on Cocoa implementation.
Eli Gottlieb <eligottlieb@gmail.com>
parents: 4810
diff changeset
   110
}