Mercury Particle Engine Tutorial

Part 3: Independent Resolution Rendering

Hi. Let’s start third lesson about special effects. Today we will talking about independent resolution rendering. What is that ? I will try to explain. If you create a game, you probably give an opportunity to change display resolution by user. This is a one of problems for programmer – how to write a 100% compatible game with all standard resolutions. Well, we have luck. On the Internet we can find many solutions. One of them was published by David Amador. He created a very useful program for independent resolution rendering. You can read more about independent resolution rendering on his site: http://www.david-amador.com/2010/03/xna-2d-independent-resolution-rendering/. We can just use his hard work for our purpose because his class is absolutely free. On his site he explain very well how to create independent resolution application, but I will write how to add independent resolution rendering to our project by myself ;) So let’s make a copy of “MPE example 01” and save it as “MPE example 04”. At first we will add a new texture – it will be nice to draw nicely background (CornflowerBlue background is so boring). Add to our project’s content this file http://chimerian.net/screens/mpe_background.jpg. Next add to the Game class:

Texture2D background;

To LoadContent:

background = Content.Load<texture2d>("mpe_background");

And to Draw function:

spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, 
                  SaveStateMode.SaveState);
spriteBatch.Draw(background, Vector2.Zero, Color.White);
spriteBatch.End(); 

Ok – try to compile our project. You will see nice background :) Unfortunately texture resolution doesn’t fit to our screen resolution. So now we should try to add independent resolution rendering code to change this situation. Firstly you need to download resolution.cs file (from David’s Amador site http://www.david-amador.com/2010/03/xna-2d-independent-resolution-rendering/, or from my site http://chimerian.net/files/MPE_resolution.zip). Copy it to main project folder and add to our project. You should have something like this:

Pic 09 - Solution Explorer

You won’t change anything in Resolution.cs – you just should add it into the project. Next you should add using directive:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Audio;
 using Microsoft.Xna.Framework.Content;
 using Microsoft.Xna.Framework.GamerServices;
 using Microsoft.Xna.Framework.Graphics;
 using Microsoft.Xna.Framework.Input;
 using Microsoft.Xna.Framework.Media;
 using Microsoft.Xna.Framework.Net;
 using Microsoft.Xna.Framework.Storage;

 using ProjectMercury;
 using ProjectMercury.Emitters;
 using ProjectMercury.Modifiers;
 using ProjectMercury.Renderers;

 using Effects;             // new

Now we should modify our project to use Resolution.cs file. Add to Game class:

Resolution.Init(ref graphics);
Resolution.SetVirtualResolution(1280, 800);
Resolution.SetResolution(1280, 800, false); 

Where:

  • SetVirtualResolution – is a main resolution. We will draw in this resolution.
  • SetResolution – is a resolution set by user. The virtual resolution will be scale to user resolution.

Of course you can change this two resolution if you want to. For this example we will use these above. Only one thing was left to do – we need to modify our Draw class:

 protected override void Draw(GameTime gameTime)
 {
      Resolution.BeginDraw();

      Matrix resolutionMatrix = Resolution.getTransformationMatrix();       // new

      spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate,
                        SaveStateMode.SaveState, resolutionMatrix);         // change
      spriteBatch.Draw(background, Vector2.Zero, Color.White);
      spriteBatch.End();

      particleRenderer.RenderEffect(particleEffect, ref resolutionMatrix);  // new
      base.Draw(gameTime);
 }

That’s it. Now you can compile and run our project. Texture resolution and special effect are scaling to resolution set in

Resolution.SetResolution(1280, 800, false);

Try to change this resolution to another one. For example:

Resolution.SetResolution(1280, 720, false);

On the left and right side of the screen you should see black lines. It’s great and usefull. How is it work ? The resolutionMatrix get transform matrix from Resolution and save it into resolutionMatrix. Next we use this matrix to draw background. At the end we use it to draw special effect:

particleRenderer.RenderEffect(particleEffect, ref resolutionMatrix);

As you can see, particleRenderer use reference to resolutionMatrix. Try to delete second parameter. What’s happened ? Effects are not scaling. And the last changes in Draw function to show very interesting error:

 protected override void Draw(GameTime gameTime)
 {
      Resolution.BeginDraw();

      Matrix resolutionMatrix = Resolution.getTransformationMatrix();

      spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate,
                        SaveStateMode.SaveState, resolutionMatrix);
      spriteBatch.Draw(background, Vector2.Zero, Color.White);
      particleRenderer.RenderEffect(particleEffect, ref resolutionMatrix);
      spriteBatch.End();

      base.Draw(gameTime);
 } 

Try to compile and run. When you click on project you won’t see any effects. But… you will see an error:

    Nesting more than one SpriteBatch.Begin when using a SpriteBatch with SpriteSortMode. Immediate is not allowed.

In line:

particleRenderer.RenderEffect(particleEffect, ref resolutionMatrix);

I made these changes to show you this error. It's impossible to draw special effects in spriteBatch.Begin and spriteBatch.End() blocks. Special effects should be draw outside of them.

Example 04 you can download from MPE Example 04. Tutorial in PDF can be download from http://chimerian.net/files/Mercury_tutorial.pdf.

So, this is it – I told you everything what I intended. I hope that this short tutorial helped you to understand how to use Mercury Particle Engine in your games. I will be very glad if you write to me email about your opinion about this tutorial. If you found any errors, please let me know about it.

 

Language

Polska wersja językowa    English language version

Share

If you like this site, you can recommend it by clicking:

Guest count

Liczniki

Contact

News

16.06.2013

Added description of "System Sprzedaży" and updated texts.

10.05.2012

Site was completely rebuilt. I changed layout, texts, and added English site version.

20.01.2012

Today we released first playable MMShooter version.