Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 24 ■ Optimizing game assets and COde, and game prOfiling Using netBeans

vocal) may need higher (22 and 32 kHz) sampling rates. I recommend using the 16-bit sample resolution
across the board, however, as it fits into memory better since memory “chunks” at 8-bit, 16-bit, and 32-bit,
and you want a seamless fit.


Java Game Code Optimization: Leverage Java Tricks


You may have noticed that I have been using long-form, easily readable (and understandable) Java code
as we design and construct our pro Java 9 game over the course of this book. This code is legitimate and
might be optimized by the Java 9 compile and build (and execute) software stages, so it’s not “bad” code,
but there are some optimization processes and constructs that would make it significantly shorter and more
streamlined. The problem in doing this process for a book is that every Java programmer has a different
preferred way of doing this, so which approach do I pick? Or do I show most of them? Unfortunately, I have a
fixed number of pages for this book to cover new media asset development, game design, development and
testing, and similarly broad topics requiring mastery in order to be a pro Java 9 game developer. In the final
section of this chapter, I will go over some of the other things that you may want to add to this board game
later on your own to get some practice with what you’ve learned.
Let’s take a look at the populateQuadrant() methods code first. The populateQuadrantOne() method
begins with the following Java sequence, with one of these constructs for each of five game board squares in
quadrant 1:


pickS1 = random.nextInt(3);
if (pickS1 == 0) { diffuse1 = new Image("/gamesquare1bird0.png", 256, 256, true, true, true); }
if (pickS1 == 1) { diffuse1 = new Image("/gamesquare1bird1.png", 256, 256, true, true, true); }
if (pickS1 == 2) { diffuse1 = new Image("/gamesquare1bird2.png", 256, 256, true, true, true); }
Shader1.setDiffuseMap(diffuse1);


You’ll simplify this code (once you know it works, after prototyping) by replacing it with the following
code, which also eliminates the conditional if() CPU processing and memory overhead:


pickS1 = random.nextInt(3);
diffuse1 = new Image("/gamesquare1bird" + pickS1 + ".png", 256, 256, true, true, true);
Shader1.setDiffuseMap(diffuse1);


It’s true this makes it more difficult to see what you are doing in the gameplay code, but the
code runs the same way and is almost half the number of lines of code, allowing you to reduce the
populateQuadrantN() methods from 26 lines of code to 16 lines of code, which is a 38 percent reduction in
code, or 40 lines of code across all four methods.
Next, consider the following code block from Chapter 19 , which could take 17 to 25 lines of code to
write:


if (picked == spinner) {
int spin = random.nextInt(4);
if (spin == 0) {
rotGameBoard.setByAngle(1080); rotSpinner.setByAngle(-1080); spinDeg += 1080;
}
if (spin == 1) {
rotGameBoard.setByAngle(1170); rotSpinner.setByAngle(-1170); spinDeg += 1170;
}
if (spin == 2) {
rotGameBoard.setByAngle(1260); rotSpinner.setByAngle(-1260); spinDeg += 1260;
}

Free download pdf