Wednesday, November 17, 2010

Atlas Textures

When we first started with Call Connect we used the method for textures that I had been using for my OpenGL framework since I created it which was to have each visual item in it's own texture. And whilst this was working fine for the most part we discovered that there were a few visual abnormalities cropping up such as sprites not appearing to draw at exactly the right size as well as there being artifacts around the edges of some of the textures.

As OpenGL requires that all textures put loaded by it need to be at a power of two this meant that the majority of our images needed to be stretched or squashed into a power of two texture which OpenGL would then resize to the correct size when drawing.

  • An example of the stretched plug animation
 Greg noticed that when he resized the sprites to be saved out into power of two textures and then resized them back the textures would sometimes gain artifacts around the edges. As we could always notice these visual quirks when playing the game we decided something needed to be done.

That was when we came to Atlas Textures. I'd known about using atlas textures for a while but it was always the same thing of choosing between spending time going back and rewriting a bunch of code that worked already (for the most part) or writing new code for features of the game. The latter choice always won out but now that there were some real issues to be resolved we decided to spend the time to make it work properly.

If you don't know about atlas texturing the basic concept is to have one big texture that you put all of your various sprites and animations in, fitting them like a puzzle, and whenever you need to draw a specific sprite you only draw that section of the atlas texture.
The benefits of using this technique are twofold.
Firstly, you don't need to resize any of your sprites into textures of power of two as you can fit them into the atlas texture in their original form.
Secondly, the way OpenGL works is that it loads in each texture to the memory when it needs to draw it so therefore the more textures you have the more time OpenGL will spend swapping and loading in new textures each time the game draws. But if you use one atlas texture OpenGL just needs to load that one big texture at the start of the draw call and each sprite in the game will just draw it's own subsection from that texture. This can be a big performance boost in a sprite heavy game.

  • Here is the (unfinished) atlas texture we are using for Call Connect
 Once we had the atlas texture set up all that was needed was to reprogram the sprite and animation classes to handle drawing from the big atlas texture and to calculate the pixel positions for the top left and bottom right of each sprite.
Whilst the static sprites made the change quite easily the animations required a bit more mathematical rejiggering and after a few "WTF why isn't this working!" moments I managed to get it working as smoothly as it was before. The visual improvement on the static sprites was noticable straight away and the animations looked better too. This blog post is being written after when the atlas texture was implemented and before all the textures have been swapped over to their atlas texture version (I was keen to write this down!) so we shall the overall improvement when that process is complete.

And even though we probably won't see a performance boost in Call Connect due to it's relatively simple nature - in terms of processing and sprite drawing - we have set ourselves up to be able to create much more system intensive games in the future.

So I guess what I'm saying is: Use atlas textures, they'll be worth it in the long run.
Oh and there's some handy tools out there like this flash Atlas Texture creation thingy which you can also download:


Sunday, November 7, 2010

Mein Craft

Few people on the gaming globe (it's like the regular globe, just a tad smaller) have yet to hear about Minecraft.  Speaking of globes, the potential maximum size of a Minecraft map would allegedly take around 6 years to traverse on foot.  I am willing to take on this Craft Walk challenge and am currently taking donations on behalf of ClockFund, a non-profit organization dedicated to... well, obviously that is a lie.

Lying aside, Minecraft is fast becoming the definitive PC game of this year and possibly the decade to follow.  While much is made of the adventuring aspects, the griefing, the ingenuity and dedication of certain players (see the functioning 16bit ALU for a frightening example), the vast, complex projects constructed using the myriad tools and editors that have sprung up since MC first appeared, my time with MC has revealed perhaps it's most appealing aspect; teamwork.

I dabbled briefly with the singleplayer to become acquainted with the rules and control scheme, but multiplayer was clearly the budding minecrafter's natural habitat.  Having joined a server of like-minded types, we set about creating an environment that lacked any kind of architectural consistency.  Castles here, sunspheres there, an impossibly high diving board and a tree that touched the world's invisible ceiling.  None of it really fit; these would not be 'council approved' constructions.  I had built my own little castle and continue to work on it but the most interesting and discussion-worthy experience I have so far is interaction with fellow crafters.  Myself and one other local decided to collaborate on a rail line, one that would run a complete circuit of the inconsistent township, sort of a sightseeing venture.

He began to dig a trench on an island beach, close to where his own personal project was located, then began excavating in earnest.  In the meantime I set up a smelter and workbench to facilitate the construction process.  We communicated via the in-game chat function and pretty soon had created a length of track that, encased in glass, dipped below the water line and continued on under water until it reached the land mass opposite.  Visible from the surface, it was quite pretty.  The sense of achievement derived from completing that particular project easily equaled and probably surpassed that which I'd felt when creating my own personal project.  My interest in collaboration and the nature of collaborative spaces probably was cause for deeper analysis than "well, that was fun", but collaborating on and completing a project in this kind of space was not something I had experienced before, at least not virtually.

This, to me, is Minecraft's real hook.  Investment of time and effort to create something impressive in single player mode is doubtless satisfying, but that shared experience ensures a more emotionally resonant experience for the players that contributed.  Minecraft's gameplay can quite rightly be labeled as emergent, but this emergence is creative and collaborative, surely worth far more to the player.

Few games (if any) are like Minecraft and many games are not designed to be used in such a way, but give players a tool-set, a space in which to use those tools and the capacity to collaborate with other players and voila, remarkable things happen.  A quick glance at youtube shows any number of Minecraft videos, many of them concerning group projects (notably this one).  People, generally, enjoy working with other people and further to that, people enjoy creating with other people.  This is not exactly a new concept for game developers, but never has it been so starkly highlighted as it is in Minecraft.

The creative act is highly addictive.  Minecraft gives you a relatively simple building system (one consistent unit of measurement) and the capacity to do virtually anything with with it.  Throw in other people who can build with you in real time and hours, days, weeks are lost creating something that has little relevance (beyond youtube) outside of the space in which it was created, yet is maddeningly addictive and very rewarding.

Whether Markus Persson thought so deeply about the multiplayer experience or not, his game allows free expression of creativity through a shared experience and the importance of that to those that play Minecraft is utterly evident.

- Greg