Follow SpaceSector.com on G+ Follow SpaceSector.com on Twitter Subscribe the SpaceSector.com Facebook page Subscribe the SpaceSector.com RSS feed Receive notifications of new posts by email

Currently developing Interstellar Space: Genesis
A turn-based space 4X strategy game for the PC.

Interstellar Space: Genesis | Turn-based space 4X strategy game for the PC

Making a Space 4X Game: An Overview on Graphics Libraries

By on January 9th, 2013 2:50 pm

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.

Graphics Libraries

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.

OpenGL example:

// Draw A Quad
gl.glBegin(GL2.GL_QUADS);
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
gl.glEnd();
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.

Wrapper Libraries

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.

Simple and Fast Media Library

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.”.

Simple Directmedia Layer

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:

Java 2D

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.

Java 3D

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.

LWJGL

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.

LibGDX

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

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.

GUI Libraries

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.

QT

QT is a library that is quite popular and powerful. It uses C++ but has bindings for other languages. The Wikipedia page goes into great detail about QT. And their main website can be found here.

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

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

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.

Conclusion

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

Tags: , ,

Interstellar Space: Genesis | Turn-based space 4X strategy game for the PC

38 Comments


  1. David Karnok says:

    I’m developing a 2D game by using Java 2D. I started with Java 6, but it was really slow and CPU intensive. Switching to Java 7 improved the performance considerably. Now, I get 30FPS with 25% CPU utilization on my 5 year old rig. One place that is still has performance problems is where I frequently modify hundreds bitmaps and it takes ~100ms till it gets accelerated properly.

    Its too late for me to switch to LWJGL but I want to start my next game with it. Do you have any experience with frequently changing bitmaps used as textures? (If the frequent copying of image data to VRAM is similarly slow as in Java2D, then probably I have to get the same effects via shader programming.)

    • Garmine says:

      I’ve played the game you mentoined (it’s great btw! :)), and if you’re talking about the day/night changings it can be made with shaders, and I believe it’s fairly easily: you should just pass a value to the shader, which then’ll multiplicate R, G and B with it (i.e. 1.0 -> 100% brightness 0.0 -> pitch black). It should be possible with something like 4 lines of shaders.

      But if you gonna use LWJGL only (and thus OpenGL) you won’t avoid shader programming.

      (If the game will be open sourced like the other one maybe I could help a bit, however, I’m not a professional (either?)).

      Good luck with your next game!

      • Garmine says:

        P.s.: actualy these is what pixel shaders are for: rapidly manipulating thousands of pixels in milliseconds.

      • dayrinni says:

        My experience with shaders is limited at this time, but AFAIK, LibGDX has way better shader support than Slick. So that is something to definitely look into.

    • dayrinni says:

      The best I could do with Java2D was reusing the same image reference multiple times. Though, there may be a better way with volatile image, I’m not so sure.

      With Slick2D, they made a new type of drawImage method, called drawEmbedded. This allows you to render the image (of portion of an image for use as a texture atlas) while not changing the texture currently in VRAM. This has huge performance bonuses (you already know this, but maybe others don’t) – since you can just keep that image in memory. At this point in time, you’d be limited by your fill rate (http://en.wikipedia.org/wiki/Fillrate).

      When I used Java2D I did on 6, I didn’t try 7. I don’t think it released yet, not sure. But, when I moved from Win XP to Win 7 in the middle of development, I noticed I had a terrible performance issue on Win 7, and I had to change some of the rendering keys on the Graphics object.

  2. Zero says:

    I am using the XNA Game Developer’s Toolkit from Microsoft, which is basically a Direct X library. It is extensively documented with tons of tutorials, has a great community, and is written in C#. C# and Java are almost identical in syntax so it is pretty easy to switch gears.

    I will probably switch to Unity in future projects because I am comfortable in C#, but we’ll see.

    • dayrinni says:

      Oh ya XNA! I should of remembered that and added it. One of my other friends was making a plat-former using XNA. We would get together and talk game and code. It was lots of fun.

      My old artist went and got a job making games and he now uses Unity, I believe. That is the Unreal Engine? Anyways, he seems to really like it and suggested that I even use it for future games. I’ll take a look at it…

      The good thing is – there are so many different choices.

      • Kamil Andrzejewski says:

        Unity is not Unreal…

        You should check it out; http://www.unity3d.com
        There is a free version to try if you’re interested. The community is quite active and there is an asset store which helps a lot.

        • dayrinni says:

          Thanks, I’ll check it out. I must of got something confused with something else he said – he was using quite a few different APIs/etc at once, and it was almost a year ago.

  3. Garmine says:

    Right now I’m using C++ with GLEW and GLUT (gonna switch the latter to something usable, though), and LWJGL for Java. However, I don’t know yet if I’ll use the C++ or the Java setup, since I mostly code in Java, but C++ is somewhat exotic for me.

    I guess I’ll run some preformance comparisons for the two and see which one is the best.

    And yes, I’m also developing a game, a space RTS to be precise.

    • dayrinni says:

      Syntactically, Java, C and C++ are all very similar. Though, there are huge differences in some of things, such as virtual functions, templating, and the usage of pointers. But I’m not sure how complex the C++ libraries are for OpenGL. If I had to guess, they would be fairly straight forward, but you never know :)

      What is your game about?

      • Garmine says:

        Right know I’m just experimenting with almost anything that I find, especially OpenGL, OpenAL, and OpenCL, and related libraries. So I haven’t actually coded the game yet. :)

        The future game is called ‘Imperium Zandagortica’, with the story of Zandagort (a hungarian browser MMO strategy game which got cancelled unfortunately) and hopefully a gameplay similar to Imperium Galactica.

        The story is the usual: the aliens see that we’re here and for some supernatural reason they decide to attack us, and we need to stay alive (or as Zandagort’s church did during the first server: betray humanity and help the aliens, lol).

        I want to incorporate as many freedom as I can. So I’ll avoid making too many modells by letting the player design ships. But that also means that I’ll need some mad physics simulations too, so the design part will be much deeper than e.g. Spore’s used to be. And that’s why I’ll try to use OpenCL, but I have no idea if that’ll actually help… :)

        The big problems that I have are:
        -if it’ll be MMO-like (it looks like it’ll be) then maybe RTS isn’t the best choice. However, by changing the regular “one empire has only one king/emperor/etc.” conception, and introducing the possibility of more players controlling one empire hopefully this part will be fixed. It also means that if some people prefer crazy eceonomy but don’t prefer the battles they can have fun too while who prefer burning whole fleets can enjoy tiny debris floating everywhere.
        -land combat and realistic planets! It’s ridiculosly hard to make something balanced, fun, and realistic for a space game in the case of planets. A regular planet has huge surface, but if I was to modell every city/tank/whatever that’d be madness. Star Wars: Empire at War solved it nicely: they had a tiny area on the ground where 2 surface forces exterminated each other. But that doesn’t feel good to me, it’s like “conquer the village, conquer the world”. (one “solution” would be to just nuke the planets)

        The original story of Zandagort was quite nice: 5 months of preparation, building, exterminating each other, and then a 2-3 weeks period of alien invasion. I’m going to change the timing, of course, but I’m planning to keep it similar. The real fun weren’t the game itself, but the players in it. :)

        But all this stuff is nothing more than a rough plan/concept, I’m going to change anything that doesn’t feel right. (The same holds true for the code of course ;)) Also, it’ll be my first “very-serious” game project.

        Well, I’m very curious to see what will this become in a year or two. :)

        (p.s.: I typed this on my phone, so some typos and/or inconsistent text and/or radiation may be present!)

        • Garmine says:

          Psps: I forgot to mention above (at the avoid modelling part) that I’m not a good artist. I mean: I can’t even use paint! :)

          And I’ll also try to make some modding capabilites like SW:EAW did. It’s amazing what did the modders do (and they’re still doing) with it!

        • dayrinni says:

          So if I am understanding what you said correctly, you are going to make a game based on another game (Zandagortica)? Maybe I need to drink some more coffee :)

          Your points you bring up though, are really. It deals with scale, management (macro vs micro), and realistic vs abstraction. We all run into these situations and they can be a challenge to find out what seems to work best. I was going to touch on these in a future article because it is something I see that always pops up in these types of games.

        • Garmine says:

          Yes, a game based on a game – absent-minded enough for me :)

          I’ve also started making a client for the game but it’s now kinda pointless, however, the source was released and there are some forks of it (still very early, ~1 months old). But it’s time for me to start my own game and not just imagine it I believe :-)

        • dayrinni says:

          It can be very fun to do something like that.

          Prior to 4X games, I did a lot of MUD programming. My very first MUD was pretty much a remake of an older game that went downhill. I did it with my best friend and it was a blast! That is also were I got most of my programming experience. I spent 4 years on it. It was so much fun and I have many fond memories from it.

          Doing it this way can also be a source of motivation – which can sometimes be lacking :)

  4. Kamil Andrzejewski says:

    Not doing 4x Games but using Unity extensively.

    Quite easier if you want to port for MAC/Win/Android/iOS and more!

    Good advice; use C# from the get-go.

  5. JD says:

    First problem while reading: You are comparing OpenGL, a graphics API with DirectX a multimedia library for the windows operating system.

    Your comparison should be: OpenGL and Direct3D. Sorry I’m antfucking you here on this.

    Another good small and lightweight library for C/C++ is GLFW (www.glfw.org), a free, Open Source, multi-platform library for opening a window, creating an OpenGL context and managing input.

    I am looking forward to your other articles. Nicely done. And you’ve drawn me intriqued about that game of yours :-)

    • dayrinni says:

      Yes, you are correct on this – I opted not to go into that much detail about it though. The article was getting very lengthy, so I trimmed out a lot of things. I also put in quite a few disclaimers about that (in addition to saying the reader would need to do some investigation on their own). DirectX contains a load of different capabilities.

      Slick2D, for example, offers more functionality, and one of these is the ability to play sound. I’m sure the other wrappers, allow for similar instances.

      I’m glad you are enjoying the read! I am quite excited for the next one on Empires and Races. I have lots of good things to say :)

      • JD says:

        Yeah I am looking forward to it and your previous article sparked and old interest in trying to coding a game again (just as a hobby for the moment) I’m not worried about the technical stuff, but more on my idea.

        Design and the mechanics are hard to get right. And what I’ve take from your previous article is to actually put something on paper first, which I never did and thus always just gave up due to well a lack of a proper concept.

        • dayrinni says:

          I hear you on the design and mechanics aspect. I think it boils down to how many games you’ve played/experience in dealing with that sort of aspects you want to use.

          I originally started out with RPGs so I know those systems very well so it’s pretty easy for me to come up with good game play elements quickly. But with 4X it was different. I had to try a lot of different things and get feedback on it all. It paid off since I know the dos and don’ts of things now(or at least, much better than before!). Hopefully, I can spread some of my experiences around and help other people.

    • zigzag says:

      I know it’s not possible to list every library, but I think Ogre and Panda3d are worth mentioning. Ogre especially because of its popularity.

      Also, while we’re being pedantic, I’m fairly sure that OpenGL has moved away from its early, procedural interface (glVertex(), etc.) like in the example towards an interface more similar to Direct3D’s.

      • JD says:

        LWJGL works the way it works as described in the article.

        I am only familiar with OpenGL 2.0/2.1 (the equivalent of DirectX 9.0). But OpenGL 3.x does things differently of that’s what you mean.

      • dayrinni says:

        I think we are getting some great comments about what other libraries are out there – it should be a help to some people who are in the investigation phase of their games.

  6. Mark says:

    Very interesting and well written article, its great to get a handle on the basics of what graphics and GUI libraries are and how they work. It really gives me a greater appreciation for the complexity behind the games I play.

    I look forward to reading your future articles, thanks again for taking the time to give us a glimpse into the world of game making.

    • dayrinni says:

      Thanks!

      There is a lot of activities that goes on behind the scenes – that most people don’t get to see. So I am glad that you are enjoying it.

  7. Towerbooks3192 says:

    Wow! these are some pretty cool hardcore stuff. I really want to make my own game but I am more of an RPG type of person. So far I have been dabbling with RPG maker VX ace but its really great to learn how to use stuff like these so I could really make everything inside my game. I have to say I kind of want to change my course of study to something related to game making and stuff but I am not a very creative person and arts was the reason why I didn’t get a medal back in elementary.

    • dayrinni says:

      I originally got started with paper and pencil games, and then text based MUDs in the late 1990’s. My best friend and I really wanted to make a RPG game. We played around with an earlier version of RPG Maker, but decided to go the route of MUDs. It was a lot of work but also loads of fun. It is really great to see your ideas come together into something fun and that people want to play.

      I’m not a very artistic person either, and I don’t think that will stop anyone from making a game, even though a lot of art is involved. What is important I think is passion, as that can drive someone to go on unbounded lengths.

      I hope the rest of my articles are as good as this one for you.

      • Towerbooks3192 says:

        Great article though and please keep them coming. I might get out of business school to get a degree in a course related to gaming. I really dream of creating my own RPG since I really loved those old sprite based JRPGs and I am kind of disappointed that games nowadays focus solely on graphics while disregarding the gameplay. Also its disappointing that aside from the indie devs, no major companies are willing to take risks in something new and innovative or something like the good niche games of before. The only downside of creating a game though is that personally I don’t want to play with it because I will know everything that is in the game.

        • dayrinni says:

          I feel the same way about the old RPG games. I grew up on them and had some great memories.

          I especially found your point about not being able to play your game interesting. That is one of the downfalls of making games – you know everything about it and some of that fun goes away. But there are different funs involved: seeing your project/ideas come together, watching other people have fun, watching a community build before your eyes, seeing the players really get into the game and so on. It can be very rewarding.

          In the case of my old MUD, I didn’t play it that much (also had to do there was so much work that needed to be done with it), but I did run a bunch of staff ran quests for the players, and that was really fun, almost like playing the like but in a different way.

          Thanks for bringing up that point. I’ve never see anyone else talk about it actually.

        • Towerbooks3192 says:

          Well I guess that is the true “reward” a game maker feels when he/she makes a game. As long as you are passionate with what you make then the real happiness and success is making a game that makes people happy and sort of see them experience and get satisfied with what you have made. I really feel good whenever I added something new the time I tinkered RPG maker like when I added my first NPC and it worked as intended and shared some screenies of it to my friend and he said he wants to play something like it, its just really rewarding.

          I gave it some thought though and I think that kind of feeling (where you dont want to play a game you have made) could only be applicable to RPGs or story-driven games compared to strategy or arcade shooters. I mean I would love to play if ever I create a chess game or strategy game with the decent or great AI because I know I still have the chance to lose and it throws a lot of surprises at me despite knowing the game like the back of my hand (random map generators for strategy games could hold surprises).

          Also in relation to that feeling, I don’t tinker with the map/scenario maker of all my strategy games because if I make a map/scenario, I would know what surprises that would await me and how to take advantage of the map. I skipped strategy games that are really scenario/user-made scenario or map focused despite being good because as a personal preference I really prefer randomly generated maps and scenarios.

  8. Serge says:

    In my opinion wrappers around OpenGL are not helping, and often hurt. All nontrivial things still need OpenGL, matrices are often still needed, new API have to be learned and wrapper often poorly portable. OpenGL on the other hand once learned could be used for years and years. I predict it will merge with GPGPU eventually (CUDA or OpenCL)

    • dayrinni says:

      I don’t know about the other libraries, but for LWJGL, and Slick, you can use OpenGL calls within Slick, so if you need something specific, it’s possible.

      Sometimes wrapper libraries do cause problems, like you said. It just really depends on the situation, I think.

      • Garmine says:

        Also, in Java you don’t really have a choice. If you don’t want to use JNI and/or C/C++ yourself you’re going to need some kind of a wrapper library for OpenGL.

  9. dayrinni says:

    Hello all,

    I just wanted to give an update – Slick2D is dead and it is better to use LibGDX now.

    I have made the switch from Slick2D to LibGDX.

    Thanks.


Related Articles:

Post category: Game Design, Ideas & Concepts