cocoa: change Shape data to use ObjC objects instead of C structs.

Fixes #6089
This commit is contained in:
slime 2022-08-21 18:06:11 -03:00 committed by Sam Lantinga
parent 057086e389
commit b204db1e6b
2 changed files with 42 additions and 33 deletions

View File

@ -29,12 +29,11 @@
#include "SDL_shape.h" #include "SDL_shape.h"
#include "../SDL_shape_internals.h" #include "../SDL_shape_internals.h"
typedef struct { @interface SDL_ShapeData : NSObject
NSGraphicsContext* context; @property (nonatomic) NSGraphicsContext* context;
SDL_bool saved; @property (nonatomic) SDL_bool saved;
@property (nonatomic) SDL_ShapeTree* shape;
SDL_ShapeTree* shape; @end
} SDL_ShapeData;
extern SDL_WindowShaper* Cocoa_CreateShaper(SDL_Window* window); extern SDL_WindowShaper* Cocoa_CreateShaper(SDL_Window* window);
extern int Cocoa_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode); extern int Cocoa_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode);

View File

@ -27,6 +27,18 @@
#include "SDL_cocoashape.h" #include "SDL_cocoashape.h"
#include "../SDL_sysvideo.h" #include "../SDL_sysvideo.h"
@implementation SDL_ShapeData
@end
@interface SDL_CocoaClosure : NSObject
@property (nonatomic) NSView* view;
@property (nonatomic) NSBezierPath* path;
@property (nonatomic) SDL_Window* window;
@end
@implementation SDL_CocoaClosure
@end
SDL_WindowShaper* SDL_WindowShaper*
Cocoa_CreateShaper(SDL_Window* window) Cocoa_CreateShaper(SDL_Window* window)
{ @autoreleasepool { @autoreleasepool
@ -46,30 +58,26 @@ Cocoa_CreateShaper(SDL_Window* window)
result->userx = result->usery = 0; result->userx = result->usery = 0;
window->shaper = result; window->shaper = result;
data = (SDL_ShapeData *)SDL_malloc(sizeof(SDL_ShapeData)); data = [[SDL_ShapeData alloc] init];
result->driverdata = data; data.context = [windata.nswindow graphicsContext];
data->context = [windata.nswindow graphicsContext]; data.saved = SDL_FALSE;
data->saved = SDL_FALSE; data.shape = NULL;
data->shape = NULL;
/* TODO: There's no place to release this... */
result->driverdata = (void*) CFBridgingRetain(data);
resized_properly = Cocoa_ResizeWindowShape(window); resized_properly = Cocoa_ResizeWindowShape(window);
SDL_assert(resized_properly == 0); SDL_assert(resized_properly == 0);
return result; return result;
}} }}
typedef struct {
NSView* view;
NSBezierPath* path;
SDL_Window* window;
} SDL_CocoaClosure;
void void
ConvertRects(SDL_ShapeTree* tree, void* closure) ConvertRects(SDL_ShapeTree* tree, void* closure)
{ {
SDL_CocoaClosure* data = (SDL_CocoaClosure*)closure; SDL_CocoaClosure* data = (__bridge SDL_CocoaClosure*)closure;
if(tree->kind == OpaqueShape) { if(tree->kind == OpaqueShape) {
NSRect rect = NSMakeRect(tree->data.shape.x,data->window->h - tree->data.shape.y,tree->data.shape.w,tree->data.shape.h); NSRect rect = NSMakeRect(tree->data.shape.x, data.window->h - tree->data.shape.y, tree->data.shape.w, tree->data.shape.h);
[data->path appendBezierPathWithRect:[data->view convertRect:rect toView:nil]]; [data.path appendBezierPathWithRect:[data.view convertRect:rect toView:nil]];
} }
} }
@ -77,26 +85,28 @@ int
Cocoa_SetWindowShape(SDL_WindowShaper *shaper, SDL_Surface *shape, SDL_WindowShapeMode *shape_mode) Cocoa_SetWindowShape(SDL_WindowShaper *shaper, SDL_Surface *shape, SDL_WindowShapeMode *shape_mode)
{ @autoreleasepool { @autoreleasepool
{ {
SDL_ShapeData* data = (SDL_ShapeData*)shaper->driverdata; SDL_ShapeData* data = (__bridge SDL_ShapeData*)shaper->driverdata;
SDL_WindowData* windata = (__bridge SDL_WindowData*)shaper->window->driverdata; SDL_WindowData* windata = (__bridge SDL_WindowData*)shaper->window->driverdata;
SDL_CocoaClosure closure; SDL_CocoaClosure* closure;
if(data->saved == SDL_TRUE) { if(data.saved == SDL_TRUE) {
[data->context restoreGraphicsState]; [data.context restoreGraphicsState];
data->saved = SDL_FALSE; data.saved = SDL_FALSE;
} }
/*[data->context saveGraphicsState];*/ /*[data.context saveGraphicsState];*/
/*data->saved = SDL_TRUE;*/ /*data.saved = SDL_TRUE;*/
[NSGraphicsContext setCurrentContext:data->context]; [NSGraphicsContext setCurrentContext:data.context];
[[NSColor clearColor] set]; [[NSColor clearColor] set];
NSRectFill([windata.sdlContentView frame]); NSRectFill([windata.sdlContentView frame]);
data->shape = SDL_CalculateShapeTree(*shape_mode,shape); data.shape = SDL_CalculateShapeTree(*shape_mode, shape);
closure = [[SDL_CocoaClosure alloc] init];
closure.view = windata.sdlContentView; closure.view = windata.sdlContentView;
closure.path = [NSBezierPath bezierPath]; closure.path = [NSBezierPath bezierPath];
closure.window = shaper->window; closure.window = shaper->window;
SDL_TraverseShapeTree(data->shape,&ConvertRects,&closure); SDL_TraverseShapeTree(data.shape, &ConvertRects, (__bridge void*)closure);
[closure.path addClip]; [closure.path addClip];
return 0; return 0;
@ -104,11 +114,11 @@ Cocoa_SetWindowShape(SDL_WindowShaper *shaper, SDL_Surface *shape, SDL_WindowSha
int int
Cocoa_ResizeWindowShape(SDL_Window *window) Cocoa_ResizeWindowShape(SDL_Window *window)
{ { @autoreleasepool {
SDL_ShapeData* data = window->shaper->driverdata; SDL_ShapeData* data = (__bridge SDL_ShapeData*)window->shaper->driverdata;
SDL_assert(data != NULL); SDL_assert(data != NULL);
return 0; return 0;
} }}
#endif /* SDL_VIDEO_DRIVER_COCOA */ #endif /* SDL_VIDEO_DRIVER_COCOA */