Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 7 ■ IntroduCtIon to JavaFX 9: overvIew oF the JavaFX new MedIa engIne


JavaFX Pulse Synchronization: Asynchronous Processing for Your JavaFX


Scene Graph Elements


A JavaFX pulse is a type of timing, or synchronization event, which synchronizes the states of your elements
that are contained within any given Scene Graph structure that you create for your Pro Java 9 game or IoT
application. The pulse system in JavaFX is administered by the Glass Windowing Toolkit. Pulse uses the
high-resolution (nanoseconds) timers, which are also available to Java programmers using the System.
nanoTime() method, introduced as of Java 7.
The pulse management system in JavaFX is “capped” or “throttled” to 60 FPS. This is an optimization so
that all the JavaFX threads we discussed earlier have enough “processing headroom” to do what they need to
do. A JavaFX application will automatically spawn up to three threads, based on what you’re doing in your
pro Java 9 game logic. A basic business application would probably only use the primary JavaFX thread,
but an i3D game would also spawn the Prism rendering thread and if that pro Java 9 game also uses audio
and or video, which it usually will, it would also spawn a Media playback thread, and if it also implements
a social media interface or element, it would also spawn the WebKit rendering thread. So, as you will see,
robust Java 9 games will require careful processor time management.
We will be using audio, 2D, 3D, and possibly video during the course of our game development journey,
so our JavaFX game application will certainly be multithreaded! As you will see, JavaFX has been designed to
be able to create games with multithreading and nanosecond timing capabilities and i3D PRISM rendering
hardware support.
When something is changed in your Scene Graph, such as UI control positioning, a CSS style definition,
or an Animation is playing, a pulse event is scheduled and is eventually “fired” to synchronize the states of
elements in the Scene Graph. The trick in JavaFX game design is to optimize pulse events so that they are
focusing on gameplay logic (animation, collision detection). For this reason, for pro Java 9 games, you’ll
want to minimize nongameplay changes (UI control location, style changes) that the pulse engine needs to
process. You will do this by using a Scene Graph for a Static design system, that is, to design the fixed visual
elements (UI, background imagery, etc.) that are not altered by the pulse engine. This will save “pulses” for
use on dynamic elements of the game that animate or are interactive.
What I mean by this is you will use the Scene Graph to design your game’s structure but will not
manipulate static design Nodes (UI, background, decoration) in real time via the Scene Graph, using
dynamic programming logic, as the pulse system would need to be utilized to perform these UI updates,
and we’ll most likely need those real-time processing events to use for our Pro Java 9 gameplay processing.
There it is again: static versus dynamic game design.
The JavaFX pulse system allows developers to handle events asynchronously, or out of order, and
schedules tasks on the nanosecond level. Next, we will take a look at how to schedule code in a pulse using a
.handle() method.


Harnessing JavaFX Pulse Engine: Extending AnimationTimer Superclass to


Generate Pulse Events


Extending your AnimationTimer class is a great way to get the JavaFX pulse engine to process Java code
on every pulse that it processes. Your real-time game programming logic will be placed inside of your
.handle(long now) method and can be started and stopped at will by using the other two AnimationTimer
methods, .start() and .stop().
The .start() and .stop() methods are called from the AnimationTimer superclass, although the two
methods can also be overridden; just make sure to eventually call super.start() and super.stop() in the
overridden code methods. The code structure for this might look like the following, if you were to add it as

Free download pdf