Re: drawing pixels in NSView
Re: drawing pixels in NSView
- Subject: Re: drawing pixels in NSView
- From: Tom Waters <email@hidden>
- Date: Mon, 26 Nov 2001 15:37:17 -0800
first of all, don't use bezierpath, it is way too slow given that you
are drawing rectangles.
here's something i just threw together that seems to keep up just
fine... it lacks a million features, but it does show one way of
getting the "fatbits" to draw quickly...
#import <Cocoa/Cocoa.h>
#define SIZE 128
@interface FatbitsView : NSView
{
int scale;
unsigned long color;
unsigned long pixels[128][128];
}
@end
@implementation FatbitsView
- (id)initWithFrame:(NSRect)r
{
if (self = [super initWithFrame:r]) {
scale = 16;
color = 0xff000000;//red
}
return self;
}
- (void)drawRect:(NSRect)rect
{
int row, col;
int minRow = MIN(rect.origin.y / scale, SIZE);
int minCol = MIN(rect.origin.x / scale, SIZE);
int maxRow = MIN(NSMaxY(rect) / scale + 1, SIZE);
int maxCol = MIN(NSMaxX(rect) / scale + 1, SIZE);
for (row = minRow; row < maxRow; row++) {
for (col = minCol; col < maxCol; col++) {
NSRect fatbitRect = NSMakeRect(col * scale, row * scale,
scale, scale);
unsigned long p = pixels[row][col];
float r = (0xff & (p >> 24)) / 255.0;
float g = (0xff & (p >> 16)) / 255.0;
float b = (0xff & (p >> 8)) / 255.0;
[[NSColor colorWithDeviceRed:r green:g blue:b alpha:1.0] set];
NSRectFill(fatbitRect);
}
}
}
- (void)mouseDown:(NSEvent *)theEvent
{
int lasti = -999;
int lastj = -999;
int pixel = color; // cache instance reference.
do {
NSPoint p = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
int i = p.x / scale;
int j = p.y / scale;
if (i != lasti || j != lastj) {
lasti = i;
lastj = j;
pixels[j][i] = pixel;
[self setNeedsDisplayInRect:NSMakeRect(i * scale, j *
scale, scale, scale)];
}
theEvent = [[self window]
nextEventMatchingMask:NSLeftMouseDraggedMask | NSLeftMouseUpMask];
} while ([theEvent type] == NSLeftMouseDragged);
}
@end
On Monday, November 26, 2001, at 10:47 AM, Carsten Koehler wrote:
Hi all,
I'm trying to create an application where I can draw 'big' pixels on a
NSView. (Just like an Icon Editor)
What I'm doing is to get the mouse position, calculate a rectangle,
append it to an NSBezierPath and redraw the NSView.
Basically it's working the only problem is, that it doesn't respond to
my drawing quite well. If I move the mouse very slowly, it draws every
dot. But if I move the mouse faster, the distance between the dots get
bigger and bigger.
I tried to speed the application up and gave the NSView the first
responder, repain the NSView in the rect that changed only , and still
it's too slow.
Two questions:
Is there a possibility to make the NSView more responsive?
Is there a better way of painting an image with 'big' pixels? Maybe
with a BitmapRep?
Any ideas? Or some source code sample?
Thanks for your help
Carsten
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev