April 11, 2012

Game Programming with Java

When you tell someone that you're programming a computer game with Java, the first response might very well be: really, is it fast enough ?

Short answer is: sure it is.
The more detailed answer is simply, as always: it depends.
It depends on the type of game, on the architecture and programming, on CPU and naturally, very important, for polished graphics, on the graphics card.
For casual games, for 2D games, it is.
Sadly, established myths are hard to overcome, and one is the „Java-is-slow“ myth.

In the old days, probably the Assembler guys were thinking similiar of C++, before it became the game development standard. And why not use the standard ? Well, first of all, when I started, Java was my job language, and second, C++, well... let's say I find not that attracting :)

Here is an older (nicer shading soon to come) of my current game under development. It is a 2D top-down-shooter inspired by games from ancient ages...

The scrolling view is made of tiles, each 64x64 pixels. Visible are 16x12 tiles, thus 192 tiles need to be rendered for each view frame. The game has an event driven multithreaded client/server architecture, runnable either via TCP or as standalone game sending in-memory events.

With pure Java2D as rendering module, on a machine with an Intel Q6600 at 2.4GHz and a GeForce GTX 460, after loosing the brakes, I get a framerate of ~1400 fps.
Thus, Java2D draws 16*12*1400 = 268.800 images each second.
Considering that the average monitor only has a refresh rate of 60 fps, that is pretty much fast enough.

The game server, the game's logic core responsible for actor movement, collision detection, game AI, path finding and field-of-view calculation reaches ~ 20.000 fps, where the client side (without rendering) shows up to ~140.000 fps.
Again, just to illustrate possibilites.

So far, this was Java 7 with Java2D for rendering (basically drawing everything with VolatileImage).
Some external libraries and tools are used to keep from reinventing too much wheels:
- collection classes from Trove, Javolution and Colt
- SoundSystem for 3D sounds
- JGoodies FormLayout for the Swing based game lobby
- JInput for game controller support
- Tiled map editor

Later additions were LibGDX, Box2DLights and LWJGL for nicer, more polished graphics based on OpenGL.

So, is it all nice, easy, fun and beautiful ? Certainly not, but where is it like that ?


No comments:

Post a Comment