Files
MonoGame.Extended/source/MonoGame.Extended.Particles/ParticleExtensions.cs
T
Christopher Whitley 8850875fb4 Texture Atlas Refactor (#890)
* Moved `TextureRegion2D` and `NinePatchRegion2D` into Graphics namespace
These are used for more than just texture atlas related things and are primarly for working with `Texture2D` instances and creating sub textures from them.  Moving them to Graphics namespace where `Texture2D` lives in MonoGame

* Dropped the `2D` suffix from `TextureRegion2D` and `NinePatchRegion2D`

* Moved `RectanglExtensions` to project root
This moves the extensions for `Rectangle` to the same relative namespace location as `Rectangle` is in MonoGame.

* Renamed to `Rectangle.Extensions.cs`

* Separate `Rectangle` from `RectangleF` extensions

* Added `GetRelativeRectangle` method

* Updated documentation

* Added `GetRelativeRectangle` method

* Updated documentation

* Top-level namespace

* Remove whitespace

* Move properties up

* Top-level namespace

* Don't recreate structs for `Bounds` and Size`
By having the property return a new struct, this allocates on the stack during situations like a loop during draw.

* Size should be `Size` not `SizeF` value
Textures deal in pixels (ints not floats)

* Removed setter for `Texture` property
Texture should not be set after region is created

* Added UV properties

* Refactor constructors

* Added documentation

* Added license header

* Make it an actual extension method

* Added `GetSubRegion` extension methods for `TextureRegion`

* Update documentation about ObjectDisposedException

* Move const values to top

* Resolve errors from`TextureRegion` refactor

* Update unit tests for TextureRegion refactor

* Started NinePatch rework

* Refactored NinePatch

* Update for texture region constructor change

* Remove ninepatch from atlas

* Moved `TextureAtlasExtensions` into `SpriteBatch.Extensions`
These are extension methods for the sprite batch

* Updated to use new `DrawTextureRegion` method after rename

* Renamed to NinePatchJsonConverter

* Removed `GetRegion<T>`
TextureAtlas no longer contains nine patch regions, so this method isn't needed

* Update after `NinePatchRegion2DJsonConverter` name change

* Resolve errors from NinePatch refactor

* TextureAtlas rework

* Renamed `TextureRegion` to `Texture2DRegion`

* Moved ContentReaders to new Content\ContentReaders directory

* Moved TextureAtlasJsonConvert to Serialization directory

* Adjusted name from `Texture*` to `Texture2D*`

* Update runtimereader and runtimettype strings
2024-06-15 01:37:18 -04:00

60 lines
1.9 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Graphics;
namespace MonoGame.Extended.Particles
{
public static class ParticleExtensions
{
public static void Draw(this SpriteBatch spriteBatch, ParticleEffect effect)
{
if(effect.IsDisposed)
{
return;
}
for (var i = 0; i < effect.Emitters.Count; i++)
UnsafeDraw(spriteBatch, effect.Emitters[i]);
}
public static void Draw(this SpriteBatch spriteBatch, ParticleEmitter emitter)
{
if(emitter.IsDisposed)
{
return;
}
UnsafeDraw(spriteBatch, emitter);
}
private static unsafe void UnsafeDraw(SpriteBatch spriteBatch, ParticleEmitter emitter)
{
if(emitter.TextureRegion == null)
return;
var textureRegion = emitter.TextureRegion;
var origin = new Vector2(textureRegion.Width/2f, textureRegion.Height/2f);
var iterator = emitter.Buffer.Iterator;
while (iterator.HasNext)
{
var particle = iterator.Next();
var color = particle->Color.ToRgb();
if (spriteBatch.GraphicsDevice.BlendState == BlendState.AlphaBlend)
color *= particle->Opacity;
else
color.A = (byte) (particle->Opacity*255);
var position = new Vector2(particle->Position.X, particle->Position.Y);
var scale = particle->Scale;
var particleColor = new Color(color, particle->Opacity);
var rotation = particle->Rotation;
var layerDepth = particle->LayerDepth;
spriteBatch.Draw(textureRegion, position, particleColor, rotation, origin, scale, SpriteEffects.None, layerDepth);
}
}
}
}