I'm trying to understand how JavaFX's threading model works, in order to figure out the best way to write my game loops. Right now, my approach is as follows:
EventHandler<ActionEvent> gameUpdate = event ->
{
// update the world based on a 33.3ms timestep (see below)
};
gameLoop = new Timeline(new KeyFrame(Duration.millis(33.3), gameUpdate));
gameLoop.setCycleCount(Animation.INDEFINITE);
My understanding is that:
- This should cause the game loop to run at approximately 30 FPS (the target frame rate), assuming that the gameUpdate doesn't take more than 33.3ms.
- The changes I make during the gameUpdate will cause a pulse to be triggered.
- This pulse is an event on the JavaFX application thread. Such a pulse ends with a synchronisation between the JavaFX application thread and a Prism render thread.
- The gameUpdate executes on the same application thread, so the pulse will run after the gameUpdate.
By this I mean the gameUpdate will never be interrupted by a pulse, which could cause the world to be rendered in an in-between state. - Event handlers also run on the same thread, so I need not worry about synchronizing them.
- Changes made to live nodes by event handlers will cause an extra pulse to be triggered.
My questions are:
- Am I understanding this correctly?
- When working with animations (eg. RotateTransition), will this animation trigger its own pulses, effectively running at a higher framerate than the game loop?
Thanks!