Help! Generating an offscreen image for later use in compositing..
Help! Generating an offscreen image for later use in compositing..
- Subject: Help! Generating an offscreen image for later use in compositing..
- From: Rob Koch <email@hidden>
- Date: Wed, 10 Sep 2003 16:35:09 -0500
Let me set this up:
I have and object that is the Model for a Random Map (RKGameModel) that
I want to build offscreen and not have to redraw each time (as the
board is 250 x 250 elements).
I was hoping to build the image offscreen and just composite it on as I
needed for speed. (see code below). eventually this will be the
background image for other animation on top. My problem is that I
cannot get the image to draw into _boardImage and then copied onscreen.
If I renderdirectly in the drawRect: selector all draws (slowly). Any
help pointing me in the right direction would be appreciated. The view
is in a ScrollView BTW.
Thanks,
--
Rob Koch
Sr. Software Developer
Omaha.com (
http://www.omaha.com )
mailto:email@hidden
(402) 898-2037 (8-5 M-F Central)
<code>
RKView.h as follows
/* RKView */
#import <Cocoa/Cocoa.h>
#import "RKGameModel.h"
#define CELL_SIZE (10)
@interface RKView : NSImageView
{
RKGameModel *_theModel;
NSImage *_boardImage;
}
- (void)setModel:(RKGameModel *)theModel;
@end
#import "RKInfectView.h"
@implementation RKInfectView
- (id)initWithFrame:(NSRect)frameRect
{
if (self = [super initWithFrame:frameRect])
{
//* Nothing done right now.
}
return (self);
}
- (void)_renderBoard
{
int w, h, xx, xy;
NSRect r;
w = [_theModel width]; //* this will be 250
h = [_theModel height]; //* this will be 250
if (_boardImage != nil)
[_boardImage release];
_boardImage = [[NSImage alloc] initWithSize:NSMakeSize((w *
CELL_SIZE), (h * CELL_SIZE))];
[_boardImage lockFocus];
for (xy = 0; xy < h; xy++)
{
for (xx = 0; xx < w; xx++)
{
r = NSMakeRect((xx * CELL_SIZE), (xy * CELL_SIZE), CELL_SIZE,
CELL_SIZE);
switch ([_theModel whatIsAtX:xx y:xy])
{
case TYPE_SOLID:
[[NSColor grayColor] set];
break;
case TYPE_ROAD:
[[NSColor blackColor] set];
break;
default:
[[NSColor redColor] set];
break;
}
[NSBezierPath strokeRect:r];
[NSBezierPath fillRect:r];
}
}
[_boardImage unlockFocus];
// [self setImage:_boardImage];
}
- (void)drawRect:(NSRect)rect
{
[_boardImage compositeToPoint:NSMakePoint(0,0)
operation:NSCompositeCopy]; //* will eventually only copy the parts
necessary
// [self _renderBoard]; //* If I do this each time it is very slow
}
- (void)setModel:(RKGameModel *)theModel
{
int w, h;
if (_theModel != nil)
[_theModel release];
_theModel = theModel;
[_theModel retain];
w = [_theModel width];
h = [_theModel height];
[self _renderBoard];
[self setBounds:NSMakeRect(0, 0, (w * CELL_SIZE), (h * CELL_SIZE))];
[self setFrame:NSMakeRect(0, 0, (w * CELL_SIZE), (h * CELL_SIZE))];
[[self superview] setNeedsDisplayInRect:[self frame]];
[self setNeedsDisplay:YES];
}
- (BOOL)isFlipped
{
return (YES);
}
@end
</code>
_______________________________________________
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.