OPENGL CHESS
Add controllers and menus with OpenGL
Never a man to miss deadlines, Andrew Smith stays in control adding mouse menus to our OpenGL-powered 3D chess game.
OUR EXPERT
Andrew Smith is a software developer for NHS England. He enjoys video gaming and started coding in C/C++ in 1997 on a Borland C++ for DOS compiler.
QUICK TIP
You can find the full source to this project at https:// github.com/ asmith1979/ lxf302/.
This month, we are continuing with the 3D chess program that we started in LXF301. T We’re creating a basic mouse-driven menu system to be shown at the start of the game program and adding game controller support.
The original source code for the 3D chess program can be downloaded or cloned from https://github.com/ stevenalbert/3d-chess-opengl. Some aspects of the original code have been modified for this tutorial.
We used a PC-compatible Xbox One controller to test and implement the functionality, but other PCcompatible game controllers should also be applicable.
Before we continue any further, let’s set up our development environment: $ sudo apt-get update $ sudo apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev $ sudo apt-get install libglfw3 $ sudo apt-get install libglfw3-dev $ git clone https://github.com/asmith1979/lxf302
Run the OpenGL test program to make sure OpenGL has been set up properly. There should be a file called testOpenGL.cpp in the lxf302 folder. The g++ build command is in the build.txt file.
Going on the basis that OpenGL and GLFW have been set up correctly, navigate into the folder called 3d-chess-lxf302. You should find a file called build.txt that contains the build code for the project, which you can copy and paste into the command line to build an executable program called chessgame. After this has been built, run the program to familiarise yourself with the chess program. Currently, it is keyboard controlled using the a, s, w and d keys to navigate round the chess board, and Space to select a piece on the board. When you’ve finished, press Escape to exit the program.
To edit and view the source code, you can use any editor or IDE of your choice. The build code to put into the command line is stored in build.txt, which can be copied and pasted to save you having to type out all the commands. Also, as we will be both editing and executing the chess program simultaneously to see the result of code changes we have made, it may be advisable to open up two terminal windows – one used for editing and the other for building and executing the game program.
The test OpenGL program showing that OpenGL is installed correctly. Adjust the size of the window if it opens with no image.