hello raven,
So, to sum up, my question basically is: is this the way to go? Or is there some other method that's better when making 3D games? For example, should I be looking into threading each type of manager so it runs independantly?
as a rule of thumb, you should avoid threading whenever you can. it only complicates things and cost some performance (due to syncronization and context switching overhead). use threading where you have to do so (for example when use blocking i/o)
in karga, to simplify things, i targeted a constant fps. even collision detection and other non-render stuff doesnt cost much compared to rendering, so if i cannot reach targeted fps, i simply skip rendering for some frames and update the game state only. this method depends on measuring render time, something like this:
while (running) {
long startTime = System.nanoTime();
gameUpdate();
gameRender();
long elapsedTime = System.nanoTime() - startTime;
if (elapsedTime < expectedElapsedTime) {
// sleep here for some time
} else {
// do some gameUpdate without render
}
}
with this approach, even on a slow machine updates stay closer to high targeted fps (50 for instance) so you can decide animation indexes etc depending on targeted fps
instead of controling animations and other things (movements etc) from a central location i've also defined a FrameListener interface which lots of my 3d objects implement. so they can 'take care of themselves'
public interface FrameListener {
public void timeForNewFrame(/* pass here whatever you want like frame no, world etc*/);
public void timeForRender(/* same as above */);
}
so what i do in gameUpdate and gameRender is:
private void gameUpdate() {
for (FrameListener l : frameListeners) {
l.timeForNewFrame(/* some parameters */ );
}
}
private void gameRender() {
for (FrameListener l : frameListeners) {
l.timeForRender(/* some parameters */ );
}
}
hope this helps :wink:
r a f t