3D Game Programming: Launching the Game

Welcome to episode 29 of 3D Game Programming in Java! In this episode, we will focus on launching the game. But before we dive into that, I wanted to share a quick update. I recently upgraded my computer’s hardware with an SSD and extra RAM, which has improved the performance of my system. Now, let’s get back to the main topic of this episode.

3D Game Programming: Launching the Game
3D Game Programming: Launching the Game

Setting up the Launcher Class

In order to launch the game, we need to make some changes to the Launcher class. Currently, our buttons are not functioning except for the exit button. So, the main task today is to make our play button work.

First, let’s open the Launcher class and locate the method where the buttons take action. In the run() method, we have a print statement for the play button. However, we want to do more than just print a message.

To make the play button work, we need to do the following:

  • Close the current launcher window
  • Open a new window for the game
  • Load the resolution that we set in the XML document

To achieve this, we can reuse the code from the renderMenu() method. So, let’s copy that code and paste it into the run() method, replacing the print statement.

Next, we need to handle the issue of disposing the frame. If we dispose the frame without dealing with the buffer strategy, it will crash with an illegal set exception. To address this, we need to make some changes.

Further reading:  EnTT: An Introduction to Entity Component Systems for Game Engines

Currently, we are using a canvas and a buffer strategy. Instead of disposing the frame, we can modify the renderMenu() method to throw an illegal set exception. By doing this, we can handle the exception separately and avoid crashing the program.

To handle the exception, we need to use a try-catch block when calling the renderMenu() method. In the catch block, we can print a message indicating that the exception has been handled.

Implementing the Stop Menu Thread

Another important task is to stop the menu thread when we start the game. We need to close the menu thread so that the game thread can take over. However, we can’t use the menu thread to close itself because it will crash. Instead, we need to use another thread to execute the close command for the menu thread.

To implement this, we can create a new static method called stopMenuThread() in the Display class. In this method, we can call the stopMenu() method from the Launcher class.

Now, in the run() method of the Launcher class, instead of creating a new instance of the Launcher class, we can call the static method getInstance() to get the existing instance. This ensures that we are closing the current thread instead of creating a new one.

Making the Options Button Work

In addition to launching the game, we also want to make the options button work. Currently, when we click the options button, nothing happens. To fix this, we need to make some changes to the Options class.

First, we need to create a new JFrame and a new JPanel called window in the Options class. We also need to set the visibility of the frame to true and the default close operation to JFrame.EXIT_ON_CLOSE.

Next, we need to add the window to the frame and set the layout to null to prevent any layout interference. Finally, we need to call the repaint() method to update the window.

Further reading:  Intermediate C++ Game Programming DirectX Pattern Matching Tutorial - Solution

With these changes, the options button should now open a new window with the options menu.

Conclusion

In this episode, we focused on launching the game and making the options button work. We modified the Launcher and Options classes to achieve these goals. Next week, we will continue with the game development, exploring level design and collision detection.

If you enjoyed this episode, please hit the like button and help us reach 100 likes. If we achieve this goal, we will prioritize level design in the next episode. Thank you for watching, and see you next week!