• Home
  • What's Tribler
  • Download
  • FAQ
  • Forum
Tribler

Wiki Navigation


Drawing

We will use CoreGraphics (a.k.a. Quartz) to draw bitmaps. It allows access to OpenGL but also a lot of other goodies. Arstechnica has a good overview of Mac OS/X's graphics engine internals. When using Cocoa draw primitives, they are automatically translated to Quartz calls.

Coordinate system

The origin of the coordinate system is in the lower left corner:

Coordinate system

One unit (point) is 1/72 of an inch. This implies the coordinate system is resolution-independent. This allows windows to be scaled according to display resolution (and display ratio!) without the programmer needing to worry about it. It is also possible to do pixel-exact drawing to avoid anti-aliasing which occurs if something is drawn on a pixel boundary.

Example Program

#import <Cocoa/Cocoa.h>

@interface VideoView: NSView {
}

-(void)drawRect:(NSRect)rect;

@end

@interface VideoWindow: NSWindow {
   VideoView *content;
}

-(VideoWindow*) init;

@end

@implementation VideoView

-(void)drawRect:(NSRect)rect
{
   /* create some shapes -- a window is 100pt x 100pt by default */
   NSRect r = NSMakeRect(10, 10, 50, 60);
   NSBezierPath *bp = [NSBezierPath bezierPathWithRect:r];

   NSRect r2 = NSMakeRect(30, 30, 70, 70);
   NSBezierPath *bp2 = [NSBezierPath bezierPathWithRect:r2];

   /* blacken the whole area */
   [[NSColor blackColor] set];
   NSRectFill(rect);

   /* draw a blue filled rectangle */
   [[NSColor blueColor] set];
   [bp fill];

   /* draw an alpha-blended yellow rectangle */
   [[NSColor colorWithCalibratedRed: 0.5 green: 0.5 blue: 0 alpha: 0.5] set];
   [bp2 fill];
}
@end

@implementation VideoWindow

-(VideoWindow*) init {
   [super init];

   if( self ) {
      [self setTitle: @"Video"];

      /* set VideoView as the content */
      content = [[VideoView alloc] init];
      [self setContentView: content];
   }

   return self;
}


@end

int main( int argc, char **argv ) {
   NSAutoreleasePool *pool;
   VideoWindow *window;

   /* initialise the autorelease pool */
   pool = [[NSAutoreleasePool alloc] init];

   /* initialise GUI and connect to WindowServer */
   [NSApplication sharedApplication];

   /* create the window */
   window = [[VideoWindow alloc] init];

   /* set as main window */
   [window makeKeyAndOrderFront: NSApp];

   /* start Cocoa event loop */
   [NSApp run];

   /* release unused objects in the pool */
   [pool release];

   return 0;
}

What's going on: we created an NSView object which contains the content of the actual window. The drawRect: rect method draws the content of the window on the rectangle provided. It is called, amongst other things, when the window is drawn or resized. The result is two overlapping rectangles, one of which is alpha-blended:

See also Apple's Cocoa Drawing Guide for more information.

There is also useful info about drawing and threads.

Prev: Cocoa Memory Management

Attachments

  • window_coordinate.jpg (30.6 kB) -Coordinate system, added by jdmol on 03/05/07 20:48:16.
  • wiki_cocoa_example1.png (3.3 kB) - added by jdmol on 03/05/07 20:49:05.

Latest news

28 October 2008

The long-awaited Tribler 4.5 is here''

29 April 2008

Tribler Team organized a successful course on Advanced P2P Technology
Read More

17 February 2008

19 Million Euro for P2P research
Read More

Please contact us if you have found a news item that's not listed here.


  • News
  • Developers
  • Business
  • Research
  • Jobs
  • About Us
  • Press
  • Contact
Login