Using NSTimer in SDL Loop.

Member
Posts: 215
Joined: 2008.06
Post: #1
As most of you are aware, the NSTimer initialization call goes something like this:

timer = [NSTimer timerWithTimeInterval:<#(NSTimeInterval)ti#> target:<#(id)aTarget#> selector:<#(SEL)aSelector#> userInfo:<#(id)userInfo#> repeats:<#(BOOL)yesOrNo#>];

I am using SDL in a C based implementation in my main file. What information can I feed the NSTimer to call a C function (such as update(), defined in the main file)? I cannot pass "self" as the object since my main file does not have a Obj-C id.

Thanks!

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
Why not use a SDL timer if you are using SDL?

http://www.libsdl.org/cgi/docwiki.cgi/SDL_AddTimer

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #3
Skorche Wrote:Why not use a SDL timer if you are using SDL?

http://www.libsdl.org/cgi/docwiki.cgi/SDL_AddTimer

That could work. I don't like the fact that direct action is not encouraged due to separate threading of the timer. It's functional, but not optimal. Thanks nonetheless, that will probably solve my problem for now. If anyone can answer my original question, that would be awesome. Thanks again.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #4
Skorche Wrote:Why not use a SDL timer if you are using SDL?

http://www.libsdl.org/cgi/docwiki.cgi/SDL_AddTimer

That did the trick. I suppose it works well enough, thanks again.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #5
To answer your original question, the simplest solution would be to write a tiny Objective-C class that does nothing but call your C function when its timer fires (via a function pointer passed into the class if there's more than one function you're interested in calling).
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
I don't think SDL timers are what you want... and I don't see why you need NSTimers. Just check each time round the event loop whether enough time has expired, "manually"...
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #7
OneSadCookie Wrote:I don't think SDL timers are what you want... and I don't see why you need NSTimers. Just check each time round the event loop whether enough time has expired, "manually"...

Do what now? What do you mean manually? Like using a counter variable?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
use SDL_GetTicks to find out what the time is, check whether that's after the time you want your "timer" to fire, if so, fire it.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  NSTimer / animation framerate question MonitorFlickers 5 5,156 Dec 4, 2005 01:10 PM
Last Post: MonitorFlickers
  Cocoa Event Loop/NSTimer revisited Fenris 6 5,337 Oct 29, 2005 11:27 PM
Last Post: maaaaark
  NSTimer Running always or somtimes unknown 3 2,973 Jul 27, 2005 12:49 PM
Last Post: unknown
  NSTimer/NSAffineTransform Coin 6 3,894 Feb 4, 2005 06:23 PM
Last Post: Coin