Home Personal BlogDaily Blogs Daily Blog #5 – Omnia game update

Daily Blog #5 – Omnia game update

by wforbes

Game Update for Omnia

I know I won’t always be able to work on Omnia every day, so I’m adding updates on these blogs as I can. I mean, I worked on it for almost all my free time today… so it’s one of the only things I have to share about today…

My task today was to get the Top-Down view of the game running on JavaFX and I really struggled with it until I found a dead simple solution. Here are some details, for the code-minded readers out there:

In AWT, the framework the game uses right now to render it’s pixels to the game window, the code uses the Graphics2D object. The game code figures out how the game should look every frame and renders it’s pixels to that Graphics2D object in the form of an array of RGB integer. For each pixel, you have a number that represents the Red, Green, and Blue color information. Those pixels are added to a BufferedImage, then you just call the ‘drawImage’ function on Graphics2D and pass it that BufferedImage. Relatively easy.

In JavaFX, to render it’s pixels to the game window, the code uses the GraphicsContext object. After looking through the documentation for a while, I couldn’t find how to use my old algorithm to render pixels. For hours I tried to come up with another solution for taking my RGB pixel array and rendering it to the GraphicsContext the same way. It doesn’t natively work with the BufferedImage I had, so instead I started monkeying around with the JavaFX equivalent. You’re suppose to use a WritableImage, PixelFormat, and PixelWriter to loop through your pixel array and “Write” them to the WritableImage… then call the drawImage function on your GraphicsContext.

The problem was… my RGB pixels didn’t easily translate to the ARGB pixels that PixelWriter wanted. But man, did I try to make that happen. After a few hours of trying to shift the bits of the RGB integers around to show up as ARGB integers… this was the best I could do:

The greens came out yellow, the grey solid tiles were pink, the blues were grey… and everything was aliased so it came out kind of blurry.

So, after working 8 hours at my day job… then bumbling 5 hours on this problem… and 2 hours of researching and reading… around midnight I was tired. When I was about to give up for the night (something I hate doing)… I found a dead simple solution: FXGraphics2D! It’s an interface that will allow you to use your old Graphics2D object functions on a newer JavaFX GraphicsContext! Yes! Soon enough, I was able to just plug that in and go. I could keep the old rendering algorithm and the game renders with the correct colors in JavaFX.

The next problem was the blurriness….

JavaFX without image smoothing
Java AWT

Sure enough, I found that JavaFX has a “setImageSmoothing” function that you can use to turn off anti-aliasing, which would reduce that blurry appearance. The next problem was, it’s not supported by the JavaFX version I was using and if I wanted to upgrade the version I would have to manually install it and include it in the project. Stuff like that is always a pain, especially in a language and IDE you’re not familiar with. An hour or so later I got it all set up and this is what I found.

First, I went ahead and installed the newest version of Java JDK 13, got that running and yes – it doesn’t support JavaFX at all. I think that’s incredibly silly, I mean JavaFX is the newest most widely used Java UI framework … why would you stop including it in your language?

Next, I found where to get this new JavaFX 15 that supports “setImageSmoothing” (and probably other new stuff). To include it in my project and get it working I needed to add it as a dependency in the Maven pom.xml file. From what I could tell, I only needed javafx-fxml and javafx-controls artifacts…..

Then, I found that I had to add a module-info.java file to the root of the game package. I don’t know anything about this but it seems like it mimics JavaScript modules slightly? It appears to declare dependencies and which of your code packages are using which dependency. I’ll have to read more about it when I can make the time.

So, with all that set… I could use “setImageSmoothing” and the appearance of the JavaFX game rendering looks just like it did on AWT…

setImageSmoothing !

With that all in place, I got the Top-Down view of the game running and accomplished what I set out to do for the evening.

That’s it for now, on to the Platformer view of the game.

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.