Re: NSAffineTransform on iOS

2012-04-13 Thread Matt Neuburg
On Mon, 09 Apr 2012 15:55:45 +0100, Pascal Harris 45rpmli...@googlemail.com 
said:

And are there any books that you'd recommend that cover these kind of issues, 
books to assist a Mac OS X developer write for iOS?

Yes, see my .sig below for one. :) Since I too came to iOS programming from Mac 
OS X programming, certain key differences jumped out at me, and are often 
explicitly noted in the text. m.

--
matt neuburg, phd = m...@tidbits.com, http://www.apeth.net/matt/
A fool + a tool + an autorelease pool = cool!
Programming iOS 5! http://shop.oreilly.com/product/0636920023562.do
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


NSAffineTransform on iOS

2012-04-09 Thread Pascal Harris
I'm trying to write some code for iOS and Wow!  I didn't expect it to be so 
unfamiliar.  Kind of like walking into a familiar city, like London, and 
discovering that everyone is speaking Dutch and no one speaks English.  Weird.  
I'm slowly getting to grips with the differences and similarities - but I'm a 
bit perplexed by transforms.  As I say, I'm writing a tile based game and the 
following code works very nicely in Mac OS X:

NSGraphicsContext *context = [NSGraphicsContext currentContext];
[context saveGraphicsState];

id transform = [NSAffineTransform transform];
[transform translateXBy:NSMidX(tileFrame) yBy:NSMidY(tileFrame)];
[transform concat];

[self drawTile:tileFrame];

[context restoreGraphicsState];


Unfortunately, my iOS version doesn't work. That isn't to say that it crashes - 
it just doesn't produce the expected output.

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

CGAffineTransform transform;
CGAffineTransformTranslate(transform, CGRectGetMidX(tileFrame), 
CGRectGetMidY(tileFrame));
CGContextConcatCTM(context, transform);

[self drawTile:tileFrame];

CGContextRestoreGState(context);

I'm sure that my error will be obvious to anyone who isn't a complete newb, but 
I am - so it isn't obvious to me!

And are there any books that you'd recommend that cover these kind of issues, 
books to assist a Mac OS X developer write for iOS?
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSAffineTransform on iOS

2012-04-09 Thread Dave DeLong
I think the major source of confusion is that NSAffineTransform is an object, 
and CGAffineTransform is not; it's a struct.  This is really important.  :)

 CGAffineTransform transform;
 CGAffineTransformTranslate(transform, CGRectGetMidX(tileFrame), 
 CGRectGetMidY(tileFrame));

The first line declares a CGAffineTransform struct, but doesn't initialize it, 
which means it contains garbage values.
The second line takes a garbage affine transform, attempts to translate it, and 
then ignores the return value.
The result is that your call to CGContextConcatCTM is being passed a garbage 
CGAffineTransform.

What you're probably look for is this:

 CGAffineTransform transform = CGAffineTransformIdentity;
 transform = CGAffineTransformTranslate(transform, CGRectGetMidX(tileFrame), 
 CGRectGetMidY(tileFrame));

Or more tersely:

 CGAffineTransform transform = 
 CGAffineTransformMakeTranslation(CGRectGetMidX(tileFrame), 
 CGRectGetMidY(tileFrame));


HTH,

Dave

On Apr 9, 2012, at 7:55 AM, Pascal Harris wrote:

 I'm trying to write some code for iOS and Wow!  I didn't expect it to be so 
 unfamiliar.  Kind of like walking into a familiar city, like London, and 
 discovering that everyone is speaking Dutch and no one speaks English.  
 Weird.  I'm slowly getting to grips with the differences and similarities - 
 but I'm a bit perplexed by transforms.  As I say, I'm writing a tile based 
 game and the following code works very nicely in Mac OS X:
 
   NSGraphicsContext *context = [NSGraphicsContext currentContext];
   [context saveGraphicsState];
   
   id transform = [NSAffineTransform transform];
   [transform translateXBy:NSMidX(tileFrame) yBy:NSMidY(tileFrame)];
   [transform concat];
   
   [self drawTile:tileFrame];
   
   [context restoreGraphicsState];
 
 
 Unfortunately, my iOS version doesn't work. That isn't to say that it crashes 
 - it just doesn't produce the expected output.
 
   CGContextRef context = UIGraphicsGetCurrentContext();
   CGContextSaveGState(context);
   
   CGAffineTransform transform;
   CGAffineTransformTranslate(transform, CGRectGetMidX(tileFrame), 
 CGRectGetMidY(tileFrame));
   CGContextConcatCTM(context, transform);
 
   [self drawTile:tileFrame];
 
   CGContextRestoreGState(context);
 
 I'm sure that my error will be obvious to anyone who isn't a complete newb, 
 but I am - so it isn't obvious to me!
 
 And are there any books that you'd recommend that cover these kind of issues, 
 books to assist a Mac OS X developer write for iOS?
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/davedelong%40me.com
 
 This email sent to davedel...@me.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSAffineTransform on iOS

2012-04-09 Thread Jens Alfke
Also keep in mind that iOS defaults to a flipped (or what non-Cocoa people 
would call normal) coordinate system, with (0,0) at the top left and the Y 
axis increasing downward.

—Jens

smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com