Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTimer and recursive drawing

NSTimer and recursive drawing
Thread Tools
jamil5454
Mac Elite
Join Date: Oct 2004
Location: Downtown Austin, TX
Status: Offline
Reply With Quote
Dec 4, 2005, 04:43 PM
 
After spending hours of reading tutorials and trying understand Cocoa 2d drawing on my own, I've finally given up and decided to see if anyone here can solve my problem.

The point of this small app is to recursively draw rectangles inside rectangles, with each successive rectangle smaller than the one before it. More specifically, each smaller rectangle is either 1/3 the width of its predecessor, or 1/3 the height (depending on a random 0 or 1 value). Each rectangle has a random color assigned to it. The recursive loop runs until the rectangles become smaller than a predetermined size (in this case, it's 5 pixels across and 5 pixels high). So, in effect, you should see the screen being split into smaller and smaller rectangles, until the whole screen is covered with tiny colored rectangles. It's actually really cool, if it'll work.

I've managed to code this using Java, which I'm much more familiar with (2 KB):

http://homepage.mac.com/jboukheir/.P...sProg.java.zip

For some reason, this doesn't work unless you're using the 1.5 version of the java runtime. With 1.4, it just shows a blank screen. Also, to exit the program, just click the mouse. Otherwise it repeats itself forever.


Now, the Cocoa version is a standard Cocoa Application built using XCode 2.2 on Tiger 10.4.3 using my Rev. C DP 2.0 G5. I'm using an NSTimer to try to slow it down to a rate of 24 fps (or 24 rectangles drawn per second). The problem is that the rectangles are being drawn to the view all at once, and then the view is displayed when it's done. I've tried using '[self setNeedsDisplay:YES]' and '[self display]' after each rectangle I draw, but to no avail. So here's the XCode project (1.7 MB):

http://homepage.mac.com/jboukheir/.P...aphicsProg.zip

I congratulate you if you've gotten this far. I've been yearning to learn Cocoa so I could port a few games I wrote in Java to run natively on my Mac. Much thanks, and replies are appreciated.
     
Catfish_Man
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Dec 4, 2005, 07:06 PM
 
You should never call [self display].

I think the confusion here is between stateful and stateless canvases. Java's assumption is that you "paint" onto the window, and it stays there. Cocoa instead asks you application to redraw *all* of the contents of a certain rectangle (your view's entire bounds if you called setNeedsDisplay, or a "dirty rectangle" if you called setNeedsDisplayInRect:). Probably the way I would implement this is to have an array of NSRects and have stepAnimation: add the next level of rects to the array. drawRect: would then just either loop over the array calling NSRectFill() or NSRectFillWithOperation(), or just call NSRectFillList() on the whole array. That's just my off-the-top-of-my-head idea, there are probably other ways of doing it.


Actually, come to think of it, you might be able to get away with just having each run through drawRect add the next level of rects. It wouldn't be 24fps (iirc it'd be 30, but I might be misremembering), and it wouldn't redraw correctly if you did something that would normally require the view to be redisplayed, but it would be much faster and simpler.

Definitely switch over to using NSRectFill rather than instantiating a new NSBezierPath for each rect.
     
jamil5454  (op)
Mac Elite
Join Date: Oct 2004
Location: Downtown Austin, TX
Status: Offline
Reply With Quote
Dec 5, 2005, 04:02 AM
 
Thanks for the suggestions. Hopefully I'll have some time to try them tomorrow evening after class
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:23 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,