Sign in to follow this  

Smooth animation with asynchronous rendering

This topic is 2661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on a 2D game engine which renders asynchronously from the game logic - that is, the logic runs in one thread, and the rendering core runs in another. The logic is all done in Lua, which is not thread-safe. Lua provides a hook that allows a function to be called once every so-many instructions, which I've used to implement a faux "interrupt handler".

The interrupt routine makes a list of references to all objects that are currently visible, and sends them a few at a time to the rendering thread (into a queue which is kept small) until the list is depleted, then refills it.

This is working pretty well, until I want to animate an object. Now a problem arises. My animations are terribly choppy.

The problem is, the rendering thread is just going as fast as it can, while the game logic thread has a small delay in its main loop to keep things going at a reasonable speed. So the two are rarely if ever in sync.

If I update an object's animation frame counter in the game logic, there's no telling how many times each frame gets rendered. It depends how busy the rendering thread is at any given time. Frame 1 might get drawn 3 times, and frame 2 only once. This makes for very choppy animation.

If I update the animation frame counter in the interrupt routine, the speed of the animation will depend on how busy the logic thread is, since there's no guarantees as to how often that interrupt actually fires. (Once every N instructions, but who knows how long that takes?) So my animations would be much faster on some machines than others, and vary in speed during play.

If I wanted to create another thread solely to update animation frame counters, I'd need to restructure and store the frame counters outside of the Lua state, since it's not safe to modify a running Lua state from another thread. So I'd like to avoid this if possible.

I wonder if someone can suggest a method to run animations that will play at the same speed all the time, while using an asynchronous rendering thread?

Share this post


Link to post
Share on other sites
I don't know if this of any help to you, but the method I would use with my current experience (= not that much) would be to simply don't let your engine calculate more then 1 frame ahead. Say you have a global variable isdrawn or something. You set it to 1 when you've drawn the current frame, and set it to 0 when your performing calculating 1 frame ahead. Then you can assure that only calculate 1 frame ahead. If you do more, you will skip a frame so that frame won't be drawn.
Like this you would still need to make sure that you can calculate ahead without messing your graphics up.
No idea if this is of any help, I'm no expert on multithreading but I just got this idea.

Share this post


Link to post
Share on other sites
Why not add some sort of animation support into the render, along with support for timing. This way, your script could say, animate this sprite using this animation sequence for this amount of time, or until I tell you to stop. That way, the rendering core can do the animation and not worry about what the script is doing. If you add in a timer, it can animate it at a specific interval even if it doesn't hear back from the script. And the script doesn't need to update the renderer every frame, just needs to notify something started/stopped, etc.

Share this post


Link to post
Share on other sites

This topic is 2661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this