Re: NSBitmapImageRep problem (I think)
Re: NSBitmapImageRep problem (I think)
- Subject: Re: NSBitmapImageRep problem (I think)
- From: email@hidden
- Date: Fri, 19 Apr 2002 11:31:16 -0700
I'm not sure why you are using an imageRep to store your simple 2D
boolean array...
unless you were planning on making an NSImage out of it later, it makes
no sense at all.
what's wrong with:
w = frame.size.width;
h = frame.size.height;
boardData = malloc(w * h);
Or if you want to save memory, since you're only using 1 bit...
assert(w&7 == 0); // make sure the width is a multiple of 8...
(probably will be for a screen saver)
bpsl = w >> 3; // bytes per scanline
boardData = malloc(bpsl * h);
and write a couple of accessors:
- (BOOL)isSet:(int)x y:(int)y
{
unsigned char val = boardData[y * bpsl + (x >> 3)];
unsigned char mask = 0x80 >> (x & 7);
return ((val & mask) != 0);
}
- (void)set:(int)x y:(int)y val:(BOOL)on
{
unsigned char *val = boardData + y * bpsl + (x >> 3);
unsigned char mask = 0x80 >> (x & 7);
if (on)
*val |= mask;
else
*val &= !mask;
}
then in dealloc
free(board);
On Friday, April 19, 2002, at 04:08 AM, Brian Williams wrote:
Hi everyone,
I took the advice of the kind folks here and decided to use an ImageRep
to hold
my data for a screen saver. The screen saver is an implemintation of
Langsont's
Ant. It's very simple the "ant" (like the turtle in logo) moves forward
turns
right if on a colored square or left if not and then flips the square
color.
Problem is that it keeps crashing. It runns for awhile and then poof,
but not
in a predictable fashion. It's driving me nuts. I get :
Exception: EXC_BAD_ACCESS (0x0001)
and
2002-04-19 13:49:39.260 System Preferences[3176] init with frame
*** malloc[3176]: error for object 0x1d98440: Incorrect check sum for
freed
object - object was probably modified after beeing freed; break at
szone_error
*** malloc[3176]: error for object 0x1d9ad30: Incorrect check sum for
freed
object - object was probably modified after beeing freed; break at
szone_error
I have included my source code if any cares to have a look.
Thanks for any help!
#import <ScreenSaver/ScreenSaver.h>
@interface antView : ScreenSaverView
{
NSPoint position; //position on the board
int direction; //direction the ant is heading
NSBitmapImageRep* board; //the board to hold the square values
unsigned char * boardData;
}
@end
@implementation antView
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
NSLog(@"init with frame");
self = [super initWithFrame:frame isPreview:isPreview];
if (self) {
int i;
[self setAnimationTimeInterval:1/30.0];
position = NSMakePoint(frame.size.width/2,frame.size.height/2);
direction = 0;
//set up a one bit bitmap of the same size as the screen
board = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:frame.size.width
pixelsHigh:frame.size.height
bitsPerSample:1
samplesPerPixel:1
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSDeviceWhiteColorSpace
bytesPerRow:0
bitsPerPixel:0];
boardData = [board bitmapData];
//be sure the bit map is clear
for(i=0; i<frame.size.width*frame.size.height; i++)
boardData[i]=0;
}
return self;
}
- (void)dealloc
{
NSLog(@"dealloc");
[board release];
}
- (void)animateOneFrame
{
int i;
//go 100 squares every tick to speed things up a little
for(i=0;i<100;i++)
{
int pos = position.x+position.y*[board pixelsWide]; //offset
for our
position
if(0==boardData[pos])//turn and flip the square
{
boardData[pos] = 1;
direction++;
[[NSColor whiteColor]set];
}else{
boardData[pos] = 0;
[[NSColor blackColor] set];
direction--;
}
NSRectFill(NSMakeRect(position.x,position.y,1,1)); //fill in
our square
if(direction<0) //fix the direction
direction +=4;
direction = direction%4;
switch(direction){ //move the ant
case 0:
position.y++;
break;
case 1:
position.x++;
break;
case 2:
position.y--;
break;
case 3:
position.x--;
break;
}
if(position.x>=[board pixelsWide]) position.x=0; //wrap around the
screen
if(position.y>=[board pixelsHigh]) position.y=0;
if(position.x<0)position.x=[board pixelsWide]-1;
if(position.y<0)position.y=[board pixelsHigh]-1;
}
}
- (void)startAnimation
{
[super startAnimation];
}
- (void)stopAnimation
{
[super stopAnimation];
}
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
}
- (BOOL)hasConfigureSheet
{
return NO;
}
- (NSWindow*)configureSheet
{
return nil;
}
@end
=====
Brian Williams
homepage == <http://chromaticgray.com>
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/
_______________________________________________
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.
_______________________________________________
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.