Performance improvements and porting Super Rawr-Type to Win8

This past weekend I finished the web version of my shooter, Super Rawr-Type, and began porting it to Windows 8. Much of Jesse Freeman’s Win8 starter kit work came in very handy here, and I would have been completely lost without it.

The web version plays fine, although it does have some slight hiccups from time to time. When porting to Win8 I realized that I really needed to correct the performance issues, as they game often became nearly unplayable in that build.

My biggest performance issues came from two distinct areas:

1)      Lack of object pooling for bullets and particles

2)      All entities (enemies) are initialized when the map loads, and therefore updating constantly

Object Pooling

Object pooling is essential any time you find yourself quickly creating or destroying objects. Both are memory intensive, and the garbage collection is really the killer. The way to get around this is through the use of object pooling. While I haven’t implemented it myself before this, I did find an excellent guide from Liza Yulayeva who covered exactly this, only a few weeks prior.

With object pooling, you are creating a predetermined number of objects and storing them in an array. This downfall to this is the initialization cost up front when loading a level, but performance should improve. Rather than creating new objects when they are needed, I’m instead pulling them from the array and activating them, and when they need to be terminated, rather than killing the objects and having them removed via the GC, I simply deactivate them and return them to the array.

While I did get it working in my game, my isolated performance tests aren’t necessarily showing improvement over what the results were to prior to this. I’ve got to investigate this more, and go from there.

Entity initialization and updates throughout the level

Updating hundreds of entities at once is by far the most memory intensive process I have occurring. In the web build I never really saw a performance hit until I threw a number of bullets and particles on screen at once.  Unfortunately, this wasn’t the case with the Win8 port. I’m not exactly sure why, but that’s a story for a different day.

spawnEnemyGrpA: function () {

 // Rolls a random number
 this.rndNum = this.randomFromTo(1, 10);

 // Spawns enemies within the Y bounds of the screen
 this.randomSpawnLocY = this.randomFromTo(ig.system.height - 20, ig.system.height / 20);

 // determines which enemy type will spawn
 if (this.rndNum > 5){
     ig.game.spawnEntity(EntityEnemyKamikaze, this.pos.x, this.random SpawnLocY);
 }
 if (this.rndNum < 5){
     ig.game.spawnEntity(EntityEnemyOrb, this.pos.x, this.randomSpawnLocY);
 }
  // Resets timer
  this.spawnTimerGrpA.reset();
 }

I needed to find another solution for spawning my entities, so I created a box which sits slightly off screen. The box’s position is about 100 pixels from the right edge of the screen, and on the same Y plane as the player.

From there, I’ve paired up similar enemy types into groups. My kamikaze style ships (ones which go directly for the player’s ship when within a certain range) are one group. Enemies that fire multiple projectiles are in another.

init: function (x, y, settings) {

 this.parent(x, y, settings);
 // Add animations
 this.addAnim('idle', 1, [2]);

 // this.spawnTimerGrpA = new ig.Timer(this.randomFromTo(2, 4));
 this.spawnTimerGrpA = new ig.Timer(1); // On for debug, spawn 1 enemy every second
 this.spawnTimerGrpB = new ig.Timer(this.randomFromTo(1, 5));
 }

These groups are each on a timer. The timer is set to go off on some random value between 1-4 seconds, and allow the enemies to spawn just off the screen, but within the top and bottom bounds of the screen. This gives the appearance that the enemy ships have a bit of randomness to them. My performance has improved exponentially. I’m cleaning up the code at the moment, but I’ll be sure to post it on the on my Git account when it’s complete.

Conclusion

The Win8 port is taking some time, but honestly, it’s things I should have done in the first place. My goal was to break this up into small chunks: the first month was creating the game – don’t worry about the code. The second month has been porting and optimization. Three weeks left, and then I’m off to another project.

The benefit to working in this manner is that it forces me to learn quickly, investigate new options, and have something to show at the end of the day. I’m finding that I’m creating a lot of great habits from this, including keeping a log of my work, and plan to continue this methodology for some time.

-----------------------


subscribe-to-youtube

One thought on “Performance improvements and porting Super Rawr-Type to Win8

  1. Pingback: Creating the random enemy spawner, improving performance | Dave Voyles

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.