Using Mercury Particle Engine with Windows Phone 7

29Apr12

This a very simple walk through to use Mercury on a Windows Phone 7 project.

Get the binaries

As far as I can see, Mercury supports Windows Phone 7 only in version 4.0, if you go to the project page you wont find this on downloads, as it’s not yet released.

Capture

So, you have to get the sources from here and build the project and find the correct binaries. An alternative is to download them from here (I forked the repo and added the binary download for WP7 ) If you ask nicely I’ll add all the other binaries.

Once you have the binaries, include ProjectMercury.dll in the project where you want to use the particle engine, and add ProjectMercury.ContentPipeline.dll to the content project.

Code

I am not entirely sure where is the best place for the initialization code, you can probably place it in LoadContent, tho I did create the new instance of SpriteBatchRenderer in the constructor of my game class.

 1: // It needs the GraphicsDeviceManager

 2: _spriteBatchRenderer = new SpriteBatchRenderer{ GraphicsDeviceService = _graphics };

 3:

 4: _particleEffect = Content.Load<ParticleEffect>("Demo1");

 5:

 6:             foreach (var emitter in _particleEffect.Emitters)

 7:             {

 8:                 emitter.ParticleTexture = Content.Load<Texture2D>("Star");

 9:                 emitter.Initialise();

 10:             }

 11:             _spriteBatchRenderer.LoadContent(Content);

All you are doing is loading the particle effect through the content pipeline. Then you are iterating over the emitters in the effect to assign the ParticleTexture value, ie a texture you just loaded, and initializing each of the emitters. Finally you need to call LoadContent, calling this method is required because it creates the internal SpriteBatch instance* .

In the update you will need to call

 1: //position is a Vector3

 2: _particleEffect.Trigger(ref position);

 3: _particleEffect.Update((float) gameTime.ElapsedGameTime.TotalSeconds);

You need to Trigger the particle with a position (there are a number of overloads for this method, make sure you check this out) and then Update. If you fail to do either of those calls, no particles will be rendered.

Finally in the Draw function:

 1: var matrix = Matrix.Identity;

 2: var cameraPosition = Vector3.Zero;

 3: _spriteBatchRenderer.Transformation = Matrix.Identity;

 4: _spriteBatchRenderer.RenderEffect(_particleEffect, ref matrix, ref matrix, ref matrix, ref cameraPosition);

Please find a full working sample here

And this would be what you see when you run the project Smile

Capture

Some thoughts

I have to admit, I find it strange that  the constructor for SpriteBatchRenderer doesn’t require GraphicsDeviceManager, and instead you need to initialize the property, if you  don’t set this property before you call LoadContent, then you get a  NullReferenceException, as this is where the internal instance of SpriteBatch  is created and a GraphicsDevice is required.

My suggestion here would be to have two versions of the constructors, one where you pass the reference of spriteBatch you have, and another one where you pass the instance of GraphicsDevice, as all usages point to only actually consuming that.

These are just some thoughts on the API, and there might be really good reasons for the decisions made this way that I don’t know about. I think that all in all this is a great project, thanks to the creators and maintainers of it.

Advertisements


7 Responses to “Using Mercury Particle Engine with Windows Phone 7”

  1. 1 Juan Santos

    Hi. First of all thanks for the tutorial. Im having problem with the line { GraphicsDeviceService = _graphics };. The compiler says the name does not exist. Then I tried your suggestions of the ‘some thoughts’ area and it didnt work either. Any tips? Thanks

  2. 2 roundcrisis

    Hi Juan:
    Did you have a look at the full sample? https://github.com/Andrea/MercuryParticleEngineWP7Sample?

    In a game that you just created from the File-> New Game Project , graphics would be your private instance of GraphicsDeviceManager . Can you give me more details about the problem you are having?
    Cheers

    Andrea

  3. 3 Jacquot

    I wrote a couple of cuostm WinForms controls; one that inherits from PictureBox to represent the image thumbnails and draw a border around them, and another that inherits from FlowLayoutPanel to act as a container for the pictures, with some simple selection and deselection code.I’ll upload the source in another post so you can see it.

  4. 4 Hernan

    Great example!!! did you test it on monogame or exen?

  5. 5 Hernan

    if you say ” I find it strange that the constructor for SpriteBatchRenderer doesn’t require GraphicsDeviceManager, and instead you need to initialize the property, if you don’t set this property before you call LoadContent, then you get a NullReferenceException”, why is this line commented con the sample?

    _spriteBatchRenderer = new SpriteBatchRenderer();//{ GraphicsDeviceService = _graphics };

    Greetings from Argentina
    HERNAN

  6. 6 roundcrisis

    Will try very soon 🙂

  7. 7 bzkarcade

    first of all thank you very much for your example.

    Download the effects editor, create an effect, add it to the project, and causes error, after some tests, I found that the xml files have a different structure.

    I guess 3.1 to 4.0 change the structure

    but how I can migrate the effects of the editor to version 4.0?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: