diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index 96ff8e8..6bea6cd 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -6,6 +6,7 @@ #ifndef __MW_LOWLEVEL_H__ #define __MW_LOWLEVEL_H__ +#include "Mw/BaseTypes.h" #include typedef struct _MwLLHandler* MwLLHandler; @@ -40,9 +41,9 @@ struct _MwLLCommon { }; struct _MwLLCommonColor { - int red; - int green; - int blue; + MwU8 red; + MwU8 green; + MwU8 blue; }; struct _MwLLCommonPixmap { diff --git a/include/Mw/LowLevel/Cocoa.h b/include/Mw/LowLevel/Cocoa.h index 868d6ad..2d1d9bd 100644 --- a/include/Mw/LowLevel/Cocoa.h +++ b/include/Mw/LowLevel/Cocoa.h @@ -11,17 +11,50 @@ #include #ifdef __OBJC__ + +#import +#import +#import + +#ifdef __APPLE__ +#import +#else +#import +#endif + @interface MilskoCocoaPixmap : NSObject { + int width; + int height; + NSData* data; NSImage* image; } + + (MilskoCocoaPixmap*)newWithWidth:(int)width height:(int)height; - (void)updateWithData:(void*)data; - (void)destroy; + +/* using @property to create instance variables fucks up 10.4 gcc for some reason? */ +- (NSImage*)image; + @end + +@interface MilskoCocoaView : NSView { + CGContextRef cg; + CGColorSpaceRef space; + CGDataProviderRef provider; + MwU32* buf; + float width; + float height; +} + +- (CGContextRef)context; +@end + @interface MilskoCocoa : NSObject { - NSApplication* application; - NSWindow* window; - NSRect rect; + NSApplication* application; + NSWindow* window; + NSRect rect; + MilskoCocoaView* view; } + (MilskoCocoa*)newWithParent:(MwLL)parent diff --git a/src/backend/cocoa.m b/src/backend/cocoa.m index b5ccd40..cd45ae2 100644 --- a/src/backend/cocoa.m +++ b/src/backend/cocoa.m @@ -1,34 +1,40 @@ -#ifndef USE_COCOA -#define USE_COCOA -#include -#include -#endif - -#import -#import -#import - +#include #include +#include +#include +#include #include "../../external/stb_ds.h" +#include "Mw/BaseTypes.h" @implementation MilskoCocoaPixmap + (MilskoCocoaPixmap*)newWithWidth:(int)width height:(int)height { MilskoCocoaPixmap* p = [MilskoCocoaPixmap alloc]; - NSSize sz; - sz.width = width; - sz.height = height; - - p->image = [[NSImage alloc] initWithSize:sz]; + p->width = width; + p->height = height; + p->data = NULL; + p->image = NULL; return p; } -- (void)updateWithData:(void*)data { +- (void)updateWithData:(void*)_data { + [self destroy]; + + self->data = [NSData dataWithBytes:_data length:self->width * self->height * 4]; + self->image = [[NSImage alloc] initWithData:self->data]; } - (void)destroy { - [self->image dealloc]; + if(self->data != NULL) { + [self->data dealloc]; + } + if(self->image != NULL) { + [self->image dealloc]; + } +} +- (NSImage*)image { + return self->image; } @end @@ -68,13 +74,32 @@ if(parent != NULL) { MilskoCocoa* p = parent->cocoa.real; [p->window addChildWindow:c->window ordered:NSWindowAbove]; + } else { + [c->application activateIgnoringOtherApps:true]; } + c->view = [[MilskoCocoaView alloc] initWithFrame:c->rect]; + [c->window setContentView:c->view]; + return c; } - (void)polygonWithPoints:(MwPoint*)points points_count:(int)points_count color:(MwLLColor)color { + int i; + CGContextRef cg = [self->view context]; + + CGContextSetRGBFillColor(cg, color->common.red / 255., color->common.blue / 255., color->common.green / 255., 1); + CGContextBeginPath(cg); + for(i = 0; i < points_count; i++) { + CGContextMoveToPoint(cg, points[i].x, points[i].y); + if(i < points_count - 1) { + CGContextAddLineToPoint(cg, points[i + 1].x, points[i + 1].y); + } + } + CGContextFillPath(cg); + + // [self->view setNeedsDisplay:true]; }; - (void)lineWithPoints:(MwPoint*)points color:(MwLLColor)color { }; @@ -105,15 +130,22 @@ return 1; }; - (void)getNextEvent { - NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; - NSEvent* event = [self->application nextEventMatchingMask:NSAnyEventMask - untilDate:nil - inMode:NSDefaultRunLoopMode - dequeue:YES]; + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + + NSEvent* event = [self->application nextEventMatchingMask:NSAnyEventMask + untilDate:nil + inMode:NSDefaultRunLoopMode + dequeue:YES]; if(event != nil) { printf("got event: %p\n", event); } + + [self->application sendEvent:event]; + + /* this should be in the draw functions but it's here for now for testing */ + [self->view setNeedsDisplay:true]; + [pool release]; }; - (void)setTitle:(const char*)title { @@ -122,7 +154,9 @@ setTitleWithRepresentedFilename:[NSString stringWithUTF8String:title]]; [pool release]; }; -- (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect*)rect { +- (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect*)_rect { + MilskoCocoaPixmap* p = pixmap->cocoa.real; + [[p image] drawAtPoint:NSMakePoint(_rect->x, _rect->y) fromRect:NSMakeRect(_rect->x, _rect->y, _rect->width, _rect->height) operation:NSCompositeClear fraction:1.0]; }; - (void)setIcon:(MwLLPixmap)pixmap { }; @@ -183,6 +217,53 @@ - (void)destroy { [self->window dealloc]; } + +@end + +@implementation MilskoCocoaView +- (id)initWithFrame:(NSRect)frame { + self = [super initWithFrame:frame]; + self->width = frame.size.width; + self->height = frame.size.height; + self->buf = malloc(self->width * self->height * 4); + self->space = CGColorSpaceCreateDeviceRGB(); + self->cg = CGBitmapContextCreate(self->buf, + self->width, + self->height, + CHAR_BIT, + self->width * sizeof(uint32_t), + self->space, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedLast); + assert(self->cg); + printf("%p\n", self->cg); + return self; +} + +- (CGContextRef)context { + return self->cg; +} + +- (void)drawRect:(NSRect)dirtyRect { + [super drawRect:dirtyRect]; + if(self) { + + CGImageRef img = CGBitmapContextCreateImage(self->cg); + if(!img) { + return; + } + printf("printed image\n"); + + CGContextDrawImage([[NSGraphicsContext currentContext] graphicsPort], CGRectMake(0, 0, self->width, self->height), img); + CGImageRelease(img); + } + // printf("%0.2f %0.2f %0.2f %0.2f\n", dirtyRect.origin.x, dirtyRect.origin.y, dirtyRect.size.width, dirtyRect.size.height); +} + +- (void)destroy { + free(self->buf); + CGContextRelease(self->cg); + CGColorSpaceRelease(self->space); +} + @end static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {