For my next article, I am going to talk about graphics libraries. This article strays away from the development of my game a bit – some of the previous comments had questions about graphics. So I decided to write a more generalized entry. My first article covered the basics of developing a space 4X game – what I called “setting the stage”. My next article is going to be about the Empires/Races, so it will be purely on the game’s development.
Before I begin, I want to take a brief moment to thank you all for reading and commenting on my first article. I had a lot of fun writing it, and it looks like the readers had fun reading it.
First off, what is a graphics library? In simplest terms, it is a collection of APIs (application programmer interfaces) that allows a programmer to gain access to graphical operations for the display.
The two most well known libraries are: OpenGL (multi-platform) and DirectX (Windows/Microsoft platforms). These libraries can be used by different programming languages. The exact languages supported vary.
There can be wrapper libraries that sit on top of the graphic library and can make it easier to use the graphical APIs. The benefit of this is that it makes writing graphical code a lot easier. This is due to the fact the wrapper can present easy to use ways for complex activities. An example is, in OpenGL, one would have to manually write code to draw a rectangle(complex). But if a wrapper existed called drawRect(x, y, width, height), it’d be easy to draw rectangle.
// Draw A Quad
gl.glColor3f(0.0f, 1.0f, 1.0f); // set the color of the quad
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
gl.glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
// Done Drawing The Quad
Simple wrapper example:
g.drawRect(0, 0, 100, 100); //Draw a rectangle at position (0,0) and the size is 100×100 pixels)
It should be fairly clear which would be easier to use!
Now, the exact nature and functionality of these wrappers vary. They are all different. So the documentation would need to be referenced.
I have not tried to write an OpenGL or DirectX program in any other language than Java, so my knowledge here is limited. I’ve done a bit of research and found some wrapper libraries. Maybe some of the readers can chime in on some good ones.
From their website: “Welcome to the SFML official website. SFML is a free multimedia C++ API that provides you low and high level access to graphics, input, audio, etc.”.
From their website: “Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer. It is used by MPEG playback software, emulators, and many popular games, including the award winning Linux port of “Civilization: Call To Power.”.
SDL is written in C and supports other languages, such as C++.
Wrapper Libraries for Java
I’m more familiar with Java so I am going to talk about these wrapper libraries in more detail. There are a lot of different libraries available for use but here are some of them:
This is the standard graphical rendering library for 2D graphics in the Java SDK. It is written and maintained by Oracle. Java 2D is mostly software rendering, but hardware acceleration can be turned on (usually Direct3D).
Java 2D should not be used for game development for it has awful performance. Java2D does have very accurate geometric shapes (rectangles, polygons, ellipses and so on), rendering capabilities (consistent drawing of shapes/lines regardless of hardware), lots of good documentation and fairly easy to learn. If you want to read more about Java 2D, see this site.
I have not used Java 3D at all so I’m not going to talk about it. It is also maintained by Oracle. You can find project’s website here.
This stands for Light Weight Java Game Library. LWJGL allows a Java program to gain access to OpenGL via the native interface. This means – yes – your Java programs can now use OpenGL! LWJGL is very popular and is used across many different libraries/wrappers.
A word of caution, this allows for low-level use of OpenGL and it can be complex to learn/use. I personally don’t use it because I am not an OpenGL programmer (not yet). However, this is easily remedied by the next library. LWJGL’s website can be found here. It has a very active community and development is still going on.
This graphics library is backed by LWJGL. It is capable of supporting both 2D and 3D games, as well as multiple platforms such as Android. It has an active community, and has many complex OpenGL features present within. It’s website can be found here.
Slick2D is a library backed by LWJGL, like LibGDX. Its main goal is to present a series of APIs that look very similar to Java 2D (in other words, the functionality and way you type the code is similar). But the difference is – Slick2D is significantly faster than Java 2D. This is because Slick2D uses OpenGL, while Java 2D mostly does software rendering.
Slick2D is very easy to learn if you know Java 2D, or basic graphics programming in general. Its development is fairly active and has a good community (I post on their site). The source is open, and anyone can contribute (I have contributed some code). Finally, Slick2D also allows for easy integration of sound and music files.
Their site can be found here.
They also have a very useful wiki located here.
Slick2D is what I am currently using for my game. The reason for this is because my previous game, SpaceIT, was unfortunately, written in Java 2D. And as mentioned before, due to a host of reasons, which included performance, I needed to switch to something else. Slick2D made sense for me because of it’s similarity to the Java 2D APIs. This meant that I could quickly get coding and do a conversion of sorts with little time wasted learning something new. This proved to be correct, I was able to quickly get started with a small learning curve.
Previously, I was barely able to run SpaceIT using 30 FPS in Java 2D (using hardware acceleration). With Slick2D, on a huge game, I am easily in the hundreds (~200-500 depending on zoom level), and after I do optimization, I may be able to get close to 750 FPS.
Now, I’m no graphics expert, and I can’t really recommend what would suit your project. But I have had great performance and much progress with using LWJGL and Slick 2D. Though LibGDX may be the way to go if you want to use more complex features of OpenGL and/or deploy on Android. I also have not investigated the possibility of using DirectX through Java. I wanted multi-platform so that wasn’t an option for me.
The next part, which I am going to talk about is the GUI Library. This type of library consists of the widgets/components that are displayed to the user. So functionality exists for things such as labels, tables, panels, windows and so on.
A GUI library can be bound close to a graphic’s library. For example, Swing (never use Swing for a game), is bound tightly to Java 2D.
There are a few GUI libraries out there that can be used with LWGJL, one being Nifty-GUI and the another being TWL. There could be more, but I haven’t investigated any others.
I used QT while in graduate school for a little program to display my results from my detail router. It was fairly easy to install, use, and then program for.
Nifty-GUI is backed by LWJGL, and uses XML in order to perform it’s configuration (though this is optional). It is quite powerful and looks really nifty. As with the other projects, this is actively being developed.
Some demos can be found here.
The main site is found here.
TWL stands for Themable Widget Library. This library uses LWGJL and can easily intrastate into Slick2D. It also is very powerful in terms of controlling the look and feel of the components through XML. You still have to write code to use the components/widgets, but there is very little look and feel code. It is all defined in the XML. The XML (or theme file) is extremely powerful. It is significantly easier to use than Swing after you get over the initial learning curve. The author, MatthiasM, also actively supports it. This can be used with Slick2D. The website can be found here.
I am currently using TWL for my UI. I selected TWL because it had good support for heavy components – I would need to display quite a few tables, trees, windows filled with many labels and so on. It also integrated nicely into Slick. I had a requirement of doing lots of Slick drawing on components and TWL can easily support that.
There were a few hiccups along the way, but MatthiasM, has been very responsive and helpful in my questions and feature requests. TWL is easy to use and write code for (compared to Swing), and it works with very little bugs and issues. In particular, Swing, would behave oddly at times for no apparent reasons. TWL on the other hand, has been very consistent and I’m pleased. In addition, TWL has great performance.
Well, this pretty much wraps up what I wanted to say for this article, which was to give a brief overview of some of the Graphic and GUI libraries that I know and have used before.
Hopefully, you have found this an interesting article and helpful in some way. I know I have made some generalizations which may not hold true for everything – there are always special and unique cases. But I had to or otherwise this article would be 50 pages and no one would read it :)
I can’t tell you what libraries to use – only you can with proper investigation and requirements for your game. Also, there is a learning curve to everything – it will take time to learn to use these libraries and APIs.
Thanks for reading and I look forward to your comments!
dayrinni has been a Space Sector contributor since October 2011. This is his first foray into writing articles for any review site. He is an avid gamer in the genres of 4X, Strategy, MMO’s and RPGs. Finally, he has been the implementor of several MUDs and is currently working on a space 4X game that offers large scope and complexity. See all dayrinni’s posts here. In particular, check his “what makes a good game” and “making a space 4X game” series.Subscribe RSS
Space Sector is a participant in GOG's Affiliate Program and earns commissions on games purchased through this banner.
- Making a Space 4X Game: Setting the Stage
- Making a Space 4X Game: Modding
- What Makes A Good Game – User Interface
- Civilization 5: Game of the Year Edition Contest [RESULTS]
- What Makes A Good Game – Immersion