Mercury Particle Engine Tutorial

Part 2: Multiple effects

Hello again in the second part of MPE tutorial. Today we will draw a few more effects in our project. It’s seems to be very easy to add some effects. And indeed it is easy – of course if you know how to do this ;) Ok, let’s start. Copy and rename a project from the first lesson to “MPE example 02” and change code in an update function:

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();

    particleEffect.Trigger(new Vector2(400, 300));      // new

    float SecondsPassed = (float)gameTime.ElapsedGameTime.TotalSeconds;
    particleEffect.Update(SecondsPassed);

    base.Update(gameTime);
}

What’s a difference ? In the first example we drew special effect on mouse position – now we draw our special effect at point (400, 300).

We need to do some changes in project “MPE example 02”. So please save this project and make a copy under name “MPE example 03”. It’s necessary to have both of these project saved on disk ("MPE example 02” and “MPE example 03”) to compare a difference between them. Ok, we are ready to add more explosions to our project. The most logical way is to add more particle effects. So we do that. Add inside the Game class:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Renderer particleRenderer;
    ParticleEffect particleEffect, particleEffect2, particleEffect3;// new

    // ... rest of code 

Next we need to change next part of code. In game constructor:

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";

    particleRenderer = new SpriteBatchRenderer
    {
        GraphicsDeviceService = graphics
    };

    particleEffect = new ParticleEffect();
    particleEffect2 = new ParticleEffect();                // new
    particleEffect3 = new ParticleEffect();                // new
}

In LoadContent:

protected override void LoadContent()
{
     spriteBatch = new SpriteBatch(GraphicsDevice);

     particleRenderer.LoadContent(Content);
     particleEffect = Content.Load(("BasicExplosion"));
     particleEffect.LoadContent(Content);
     particleEffect.Initialise();

     particleEffect2 = Content.Load(("BasicExplosion"));   // new
     particleEffect2.LoadContent(Content);                 // new
     particleEffect2.Initialise();                         // new

     particleEffect3 = Content.Load(("BasicExplosion"));   // new
     particleEffect3.LoadContent(Content);                 // new
     particleEffect3.Initialise();                         // new
}

In Update function:

 protected override void Update(GameTime gameTime)
 {
      // Allows the game to exit
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();

      particleEffect.Trigger(new Vector2(400, 300));
      particleEffect2.Trigger(new Vector2(100, 100));       // new
      particleEffect3.Trigger(new Vector2(700, 500));       // new

      float SecondsPassed = (float)gameTime.ElapsedGameTime.TotalSeconds;
      particleEffect.Update(SecondsPassed);
      particleEffect2.Update(SecondsPassed);                // new
      particleEffect3.Update(SecondsPassed);                // new

      base.Update(gameTime);
 }

And finally in Draw function:

 protected override void Draw(GameTime gameTime)
 {
      GraphicsDevice.Clear(Color.CornflowerBlue);

      particleRenderer.RenderEffect(particleEffect);
      particleRenderer.RenderEffect(particleEffect2);       // new
      particleRenderer.RenderEffect(particleEffect3);       // new

      base.Draw(gameTime);
 }

That was easy, wasn’t it ? So please compile and run our new project.

Surprised ? As I think you can’t see more than one effect at the time. You probably see one effect like in "MPE example 02", but drawn three time faster. The Question is: why ? Why you can’t see three special effects ? To find an answer I spend a lot of time, but I finally found it on the official project forum (http://mpe.codeplex.com/discussions/221924). How to fix it ? It’s really simple. We need to modify LoadContent function:

 protected override void LoadContent()
 {
      // Create a new SpriteBatch, which can be used to draw textures.
      spriteBatch = new SpriteBatch(GraphicsDevice);

      particleRenderer.LoadContent(Content);
      particleEffect = Content.Load(("BasicExplosion")).DeepCopy();  // change
      particleEffect.LoadContent(Content);
      particleEffect.Initialise();

      particleEffect2 = Content.Load(("BasicExplosion")).DeepCopy(); // change
      particleEffect2.LoadContent(Content);
      particleEffect2.Initialise();

      particleEffect3 = Content.Load(("BasicExplosion")).DeepCopy(); // change
      particleEffect3.LoadContent(Content);
      particleEffect3.Initialise();
 }

As you can see, we just only add DeepCopy() to all particle effect. Compile the project again. You should see three nice explosions. Simple ? Simple. Now you can try to add more special effect to see how many effects your hardware can display at once in 60fps ;)

All Examples you can download from MPE Example 02, MPE Example 03. Tutorial in PDF can be download from http://chimerian.net/files/Mercury_tutorial.pdf.

At next part of tutorial we will talk about independent resolution rendering. Believe me, it's very useful and you probably will need it into your game.

 

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.