NSTimer / animation framerate question

Post: #1
I have a simple little program that has a custom NSView which draws a field of stars moving across the screen- basically just an array of various integers that keep track of a bunch of white dots that scroll by on a black background. An NSTimer fires the redraw to make it animate.

My problem is that I have the framerate speed for the NSTimer, as well as the number of stars (white dots) defined by the precompiler with #define but no matter how I change them something internally seems to be capping my framerate.

It's easy enough to slow things down, I can reduce the timer firing speed. But when I increase the framerate variable, I eventually reach a certain point where it stops making the stars go by faster. I thought maybe my processor had just maxed out somehow through inefficient code or something, but then I changed the predefined number of stars from 60 to 300, and it animated at the exact same speed.

I can't seem to figure out why, regardless of the amount of drawing, or the Timer speed, it seems to cap off at a certain framerate. Does it have something to do with the built-in setNeedsDisplay refresh rate or something?

I'm very much a noob at Obj C but if anyone could give me some feedback on this I'd appreciate it! thank you!
Quote this message in a reply
Posts: 1,199
Joined: 2004.10
Post: #2
I could be wrong, but I *think* that normal NSView drawing is beam-synced, meaning it wil never draw faster than the refresh rate of your video card.

There may or may not be a way to turn this feature off. But consider, you're getting a cap for free -- there's no way your monitor could display what you're drawing any faster, anyway.

What you should do, and you may be doing anyway, is to give your timer an interval of zero and keep track of the actual time between frame drawing via NSDate. Then, use a time based animation to progress your stars by the time interval between frames. You'll get a constant speed of motion regardless of wether your framerate is 20, 60, or 120 ( if you're running on a good CRT ).
Quote this message in a reply
Posts: 1,563
Joined: 2003.10
Post: #3
In general, it's a good idea to assume that your timer will not fire at predictable intervals. This article may be of some use to you:

Quote this message in a reply
Posts: 469
Joined: 2002.10
Post: #4
enable/disbable vsync in NSOpenGLContext.

If you're doing single threaded game/drawing, then you can turn off your vsync for faster framerates. You'll get ugly tearing though, so I don't recommend it. Better to just do multithreading or some kind of independent game/drawing loop.

15.4" MacBook Pro revA
Quote this message in a reply
Posts: 196
Joined: 2003.10
Post: #5
You really shouldn't be modifying the speed of your animation by reducing or increasing your frames per second. Look into time-based animation. I have sample Cocoa code here:


That works well. The actual problem is probably vsync, although your implementation seems like it will give you problems later on when someone runs your program on a machine that can't achieve the same fps as yours can.
Quote this message in a reply
Post: #6
Thanks for the help, I'll check out those links! Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Using NSTimer in SDL Loop. Talyn 7 6,906 Oct 7, 2008 09:40 PM
Last Post: OneSadCookie
  Framerate control + basic physics math MacGoober 4 5,279 Jan 10, 2007 11:40 AM
Last Post: Frank C.
  Best way to calculate framerate ferum 18 29,964 Sep 7, 2006 11:05 AM
Last Post: TomorrowPlusX
  Cocoa Event Loop/NSTimer revisited Fenris 6 8,203 Oct 29, 2005 11:27 PM
Last Post: maaaaark
  NSTimer Running always or somtimes unknown 3 4,424 Jul 27, 2005 12:49 PM
Last Post: unknown