For the last couple of days I’ve been doing some pen and paper sketching on a small Java game. Right now I’m
trying to make up my mind about if I should use just one loop with both rendering and updating or splitting them up in separate loops. The idea is to have a SceneGraph class that holds all game objects (GraphObjects) and a SceneGraphCanvas that renders the content of the SceneGraph to the screne.
I’m leaning towards having separate threads, but I’ve heard some rumours that this could lead to very choppy frame rates. But if I could, some how, get around this I think it I would have some really useful classes and thing like a split-screen would be a matter of minutes to implement. I know threading can lead to code complexity problems, but since both threads will more or less be minding there own business I don’t think this will be much of a problem in practice.
SceneGraph.run() [in pseudo code]
while(running)
{
if(!paused)
{
while( there still are canvases rendering this graph)
Sleep(2)
- calculate delta time and FPS
- Update all graph objects
- Collision detection on all GraphObjects
- Collision reaction
m_cycle_count++
Sleep(5);
}
else
{
Sleep(100)
}
}
SceneGraphCanvas.run() [in pseudo code]
while(running)
{
if(m_screen_graph.m_cycle_count > m_last_rendered_cycle)
{
renderScreenGraph()
m_last_rendered_cycle = m_screen_graph.m_cycle_count
}
else
{
Sleep(10)
}
}
(SceneGraphCanvas extends javax.awt.Canvas)
Does this sound like a reasonable way to do things?
Have any one here tried something like this? If you have I’d really like to hear if and how you got it to work (or why you didn’t and why it’s a really stupid idea to use the separate threads approach)
