Today I was looking around for some quick copy and paste code to add FPS gaming control to a small GLUT application I am writing. Half an hour or so of searching later and I still couldn’t find anything that I liked. A lot of the results were from forums of people trying to roll out their own code. I gave up and resorted to something I didn’t think I’d ever do, use code I wrote during my PhD …
After an hour or so of tinkering I got something that I’m happy with. I got my GLUT demo to do the following:
- W,A,S,D keys for moving and strafing
- Mouse look (default is inverted mouse)
- Mouse button to fly up/down
- SPACEBAR to toggle FPS control mode
- Mouse always stays within the window with the cursor hidden
It has the feel of a proper FPS game.
Here’s what the demo looks like. Interestingly, when taking a screenshot the mouse cursor appears in the image.
The demo code can be used as a copy and paste project to quickly get a viewer running. All you have to do is add your rendering code into the Display() function.
The demo requires freeglut to be installed. You can use CodeBlocks to open up the project or type make if you’re in Linux. If you’re using Windows you’ll have to setup your own Visual Studio project.
If you have any questions just leave a comment and I’ll get back to you.