main page | gallery | rationale | contact

asteroids
a simple game using the GL Utility Library (GLUT)

Written:Spring 2001
Last Touched:Spring 2001



Introduction

While I wasn't busy helping students in my role as teacher's assistant in my high school Computer Science course, I put my time to use writing a simple game using OpenGL. Nothing fancy here, just used the GL Utility Library (GLUT) and some good old C structs and linked lists to get the job done.

What Was Difficult?

I ran into a bit of trouble getting the code to run on my Linux box at home. In the Windows labs at school, I used the fairly-standard game programming trick of polling the system clock to calibrate game objects' movement and achieve a sort of framerate independence. I used the handy GetTickCount() Windows API function, which returns the current system time in milliseconds.

Perhaps I didn't look hard enough, but I couldn't seem to find an appropriate library function in Linux that performed the same, simple way the Windows one did. My solution? Synthesize it using preprocessor definitions.

main.c (excerpt)

#ifndef WIN32	// POSIX (? we hope) compiler
typedef unsigned long DWORD;
#include <sys/time.h>
#include <unistd.h>
#include <memory.h>
#include <stdio.h>

DWORD GetTickCount(void)
{
	static struct timeval tv;
	gettimeofday(&tv, NULL);
	return tv.tv_sec*1000 + tv.tv_usec*0.001;
}

#else	// regular Windows compiler
#include <windows.h>
#include <memory.h>
#endif

A simple check to see which platform we're compiling on, and poof -- cross-platform framerate independence. In case you were interested, I got about the same performance under Linux with Mesa3D as I did in Windows; no surprises there.

What you need to compile & run this

Well, for Windows, I'd recommend Microsoft Visual C++ 6.0 or greater, since that's exactly the tool I used to develop this project. You'll also need to grab and install the GLUT port from this URL:
http://www.xmission.com/~nate/glut.html

For Linux you'll need the Mesa3D runtime & development libraries and a working X server. I don't have a huge amount of experience outside of Debian, but you should generally be able to grab these packages from your distribution maintainer's site. Of course, if you're feeling particularly adventurous, you can always compile them yourself like I did (back in the day...), but other than the learning experience there's no real reason to do that anymore. Problems? Google is the Linux sysadmin's number one reference manual; chances are that others have had the same problem and have found a solution. Try it.


Controls

Rotate Ship:Mouse X Axis
Fire:Left Mouse Button
Accelerate:A
Decelerate:Z
Quit:Escape, Alt+F4


Executableasteroids.zip (116 K)
Sourceasteroids-src.zip (5.98 K)



Last updated: 2003.02.27 by falger@calpoly.edu