Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Optimizing Code for Xbox XNA Games Is Required
Date 2/2/2009    Tags XNA    (0)

It amazing me that, give how good games can look on the Xbox 360, how little processing power the system really has. GTA IV for instance looks great, so when it came to developing a simple 2D single screen sidescroller, I figured code/performance optimization would be the least of my worries. After all, my game won't have anywhere near the graphic prowess or complexity of GTA IV or Halo 3 or any of the other games in my personal library. Turns out that I was very wrong.

When you do the small XNA tutorials, you typically interact with only a few different object and the complexity is limited enough that performance is not an issue. Now that I've gotten to a point in my game where I have ten different enemies (some of which shoot at the player), two characters that can be controlled by the user, and a number of block (many that have different properties), the nice smooth 60 fps I once had has gone by the wayside, especially in levels with lots of blocks (this requires a lot more collision detection checks).

What has become abundantly clear to me now is that, even though the Xbox 360 can create a great looking game, it doesn't happen just through hardware brute force. Optimization (class design, garbage collection, when assets are loaded) needs to be accounted for each and every step of the way. If done right (like GTA IV), you can have a beautiful game. If done poorly (like my game thus far), you end up with a choppy mess that reminds you of the good old "four enemies on the screen" NES days (without the flashing at least, the Xbox 360 is a beast when it comes to actually drawing to the screen).

The biggest issue on my game appears to be with the class design. In the most excellent article High Performance Game Development, it is noted that using a virtual method can be as much as 40% slower than a static or instance method. My game is unfortunately riddled with these due to how the class structure was designed.

I currently have a sprite class that all objects in my game inherit from. Then there is also an enemy class that has some specific enemy only things. Then inheriting once more from that are classes for the specific enemies. This leads to far too many virtual methods and far too much casting of objects. When you work on the web, an extra one millisecond of processing time isn't worth optimizing for. In a game, one millisecond of extra processing time can cost us 6 fps or more. It's easy to see how these small things could add up to a poor playing game.

In order to get my game performing as desired, I will now be doing a massive refactoring of class structure. The biggest change will be eliminating all the different enemy classes and instead having just one enemy class with an EnemyType enum (I will also eliminate the sprite class). By doing this, I should be able to eliminate all the virtual methods used for enemies (also for the player, blocks, and items).

The second major thing I will be doing is preloading my objects. Instead of just creating enemies/items/shots as needed, I will have a pre-allocated pool just waiting to be used, thus eliminating any in-game loading of objects. Because I will reuse these objects, it will also reduce the amount of garbage collection needed.

Having learned now what I have about how optimized Xbox games really are, I have a new appreciation for the coding required to create them. I hope that the changes I have proposed will be able to get my game back to performing at the standard 60 fps clip. I'll update you all in the next work or so and let you know how it went. <


This article has been view 2107 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)