iDevGames Forums
Rare behavior. rendering<->logic sync howto - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Rare behavior. rendering<->logic sync howto (/thread-2009.html)



Rare behavior. rendering<->logic sync howto - godexsoft - Dec 22, 2008 10:53 AM

Hello everybody,

I am working on a game where objects should move smooth but very fast across the screen. I have two threads, one for rendering and one for logic. My objects have two methods. One updates the logic and the other can be used to render the object. These methods are called from the two threads mentioned above.
Of course I'm forced to use a mutex so that my threads wont mess around. Most of the time things go pretty ok, however the whole system lags a lot. It looks like we are skipping some frames so the object just jumps from one place to another.
I use texture maps so I'm not loading a lot of different small textures for every object and thus not switching between textures that much. I wouldn't expect the bottleneck to be in opengl. To me it looks more like a synchronization issue or something like that. Maybe thread priorities should be set? I tried to play with them but it didn't do any better.

Anyway my question is: What is the usual/better way to handle synchronization between two threads provided that we are developing a game here? How do _you_ do/did this? How games like jelly car, quake, doom etc. get such smooth graphics (these games are MUCH more complicated than mine)?
Maybe someone can provide some code or point me to a _good_ example in a opensource game for the iPhone..

Thanks in advance!
Alex


Rare behavior. rendering<->logic sync howto - ThemsAllTook - Dec 22, 2008 11:03 AM

Is there an actual compelling reason to use two threads? You could potentially save yourself a huge amount of headache by switching to a single-threaded paradigm, but I'm not well-versed enough in iPhone programming to know if there's some inherent benefit to multithreading for this that I'm missing. Since you only have one CPU core, it seems like it's likely to slow you down more than anything.


Rare behavior. rendering<->logic sync howto - Skorche - Dec 22, 2008 03:38 PM

Yeah, I'm not sure I'd go for the multithreaded route. The only thing it buys you is that your process is not blocked when swapping buffers due to vsync. That's only going to be an issue if your game would run at less than the vsync rate to begin with if unthrottled.

I found the best thing that you can do is to move away from using NSTimers to regulate your timing. My game loop is a tight while loop, update 0 or more ticks of logic until it catches up with the current time and draw a frame. The catch is that you have to pump the events yourself by calling
Code:
while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE) == kCFRunLoopRunHandledSource);
That simply pumps all the sources (events, timers, etc) and forces them all to update. Using an NSTimer, events are handled while the timer sleeps. You need to force the event actions to occur as you aren't allowing the flow of control to return to the main run loop.


Rare behavior. rendering<->logic sync howto - godexsoft - Jan 2, 2009 06:00 AM

Thanks guys,

I have moved everything to single threaded model with the tricky code provided by Skorche and things seems to work a lot smoother now. Thanks!

Happy new year!
Alex