Re: Weird Threads (repost)
Re: Weird Threads (repost)
- Subject: Re: Weird Threads (repost)
- From: Shawn Erickson <email@hidden>
- Date: Thu, 12 Feb 2004 19:37:18 -0800
On Feb 12, 2004, at 6:01 PM, Sam McCandlish wrote:
Let me make sure I know what you're saying...
Do you mean I should just have a thread that runs whenever the image
needs to be rendered, then when drawRect is called draw that image?If
so, how would I display the image as it was drawing? (Is this even
possible?) Thanks for your help!
A quick little hack to show a possible path to take...
#include <stdlib.h>
@interface MyView : NSView {
NSImage *cachedImage;
NSLock *cachedImageLock;
}
- (void)updateCachedImage;
- (NSPoint)randomPoint;
@end
@implementation MyView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
cachedImageLock = [[NSLock alloc] init];
cachedImage = [[NSImage alloc] initWithSize:[self bounds].size];
[NSThread detachNewThreadSelector:@selector(updateCachedImage)
toTarget:self
withObject:nil];
}
return self;
}
- (void)dealloc
{
[cachedImage release];
}
- (void)drawRect:(NSRect)rect {
const NSRect *rects;
int rectIndex, rectCount;
[self getRectsBeingDrawn:&rects count:&rectCount];
//quick hack for live resize...
if ([self inLiveResize]) {
[[NSColor windowBackgroundColor] setFill];
NSRectFillList(rects, rectCount);
} else {
for (rectIndex = 0; rectIndex < rectCount; rectIndex++) {
NSRect rect = rects[rectIndex];
[cachedImage compositeToPoint:rect.origin fromRect:rect
operation:NSCompositeCopy];
}
}
}
- (void)viewDidEndLiveResize
{
[cachedImageLock lock];
[cachedImage release];
cachedImage = [[NSImage alloc] initWithSize:[self bounds].size];
[cachedImageLock unlock];
}
- (void)updateCachedImage
{
while (1) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRect bounds;
bounds.origin = NSMakePoint(0,0);
bounds.size = [cachedImage size];
[cachedImageLock lock];
[cachedImage lockFocus];
[[NSColor windowBackgroundColor] setFill];
[[NSColor blueColor] setStroke];
NSRectFill(bounds);
NSBezierPath *path = [NSBezierPath bezierPath];
[path setLineWidth:2.0];
[path setLineJoinStyle:NSRoundLineJoinStyle];
[path setLineCapStyle:NSSquareLineCapStyle];
[path moveToPoint:[self randomPoint]];
int i;
for (i = 0; i < 10; i++) {
[path curveToPoint:[self randomPoint]
controlPoint1:[self randomPoint]
controlPoint2:[self randomPoint]];
}
[path closePath];
[path stroke];
[cachedImage unlockFocus];
[cachedImageLock unlock];
[self setNeedsDisplay:YES];
[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:0.5]];
[pool release];
}
}
- (BOOL)isOpaque
{
return YES;
}
- (BOOL)wantsDefaultClipping
{
return NO;
}
- (NSPoint)randomPoint
{
float x, y;
NSSize size = [cachedImage size];
x = size.width * ((double)rand() / (double)RAND_MAX);
y = size.height * ((double)rand() / (double)RAND_MAX);
return NSMakePoint(x, y);
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.