Gamasutra is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Gamasutra: The Art & Business of Making Gamesspacer
From XNA to MonoGame
View All     RSS
November 13, 2019
arrowPress Releases
November 13, 2019
Games Press
View All     RSS







If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 

From XNA to MonoGame


May 15, 2013 Article Start Previous Page 5 of 6 Next
 

Pushing Performance

Both the iOS and Android versions of MonoGame run on top of the mono platform. Mono has a great compiler, and these two mobile platforms also include a linker, which is used to reduce the package size of your application by removing code that is not needed. It also has a really good garbage collector, but even though there have been some major advances in the mono GC in the last few years, you still have to remember you are running on mobile devices which vary dramatically in terms of power (especially when it comes to Android devices). Make sure to look at your Update methods; you might be making work for the GC by creating lots of temporary objects that will just be disposed of. You should also think about whether you actually need to do a particular calculation in a certain way -- after all, the fastest code is the code that doesn't get executed. Consider the following class definition:

public class Player

{

  public Vector2 Position;

  public Vector2 Scale;

  public void Update(GameTime gameTime) { … }

 public void Draw(GameTime gameTime) { … }

}

In this segment, we are defining a player that has a position and a scale. Now suppose we are using the Matrix property of the sprite batch to render this player using the position and scale data, and also we are using that information for collision detection. One easy way to create the matrix is to do it in the update method like so:

var matrix = Matrix.CreateTransform(Position) * Matrix.CreateScale(Scale);

On desktop machines, this would probably work just fine, but on lower-powered mobile devices it could be a problem. Even though the Matrix class is a struct, and cheap to create, we are still making this calculation every call to Update. If your game is running at 30 frames per second, that alone is many matrix calculations that you might not have had to do. Instead, you could declare a field for the Matrix and just update it in the Update method. Or, even better, you could just update the Matrix only when the Position or Scale properties are changed (see Listing 2).

Listing 2: Optimizing update calls for mobile devices.

public class Player

{

  Vector2 position;

  Vector2 scale;

  Matrix matrix;

 public Vector2 Position {

   get { return position;}

   set {

       if (position != value) {

          position = value;

          UpdateMatrix();

       }

   }

 }

public Vector2 Scale {

   get { return scale;}

   set {

       if (scale != value) {

          scale = value;

          UpdateMatrix();

       }

   }

 }

 public void UpdateMatrix() {

   matrix = Matrix.CreateTransform(Position) * Matrix.CreateScale(Scale);

 }

  public void Update(GameTime gameTime) { … }

 public void Draw(GameTime gameTime) { … }

}

I know this is a simple example, but if it gets you thinking about what tiny improvements you can make, then it has done its job. Those of you who know your math will know that you can probably remove the scale and position fields from this class, and just store the matrix. 


Article Start Previous Page 5 of 6 Next

Related Jobs

Supergiant Games
Supergiant Games — San Francisco, California, United States
[11.12.19]

Engine Programmer at Supergiant Games
Sucker Punch Productions
Sucker Punch Productions — Bellevue, Washington, United States
[11.12.19]

Camera Designer
Sucker Punch Productions
Sucker Punch Productions — Bellevue, Washington, United States
[11.12.19]

Encounter Designer
Big Red Button Entertainment
Big Red Button Entertainment — El Segundo, California, United States
[11.12.19]

Unity UI/UX Programmer





Loading Comments

loader image