/* Sparx
* Copyright August Zinsser 2007
* This program is distributed under the terms of the GNU General Public License
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Pina3D.Particles
{
///
/// Manages the images used for particles.
///
public class ParticleSprite
{
///
/// The standard particle sprite sheet, made public so it can be passed by reference
///
public static Texture2D SpriteSheet;
private static Dictionary mImageCoords; // Refers to each individual image in the sprite sheet
private Vector2 mUVUpperLeft; // Pick a slice out of the sprite sheet
private Vector2 mUVLowerRight; // ''
public static Dictionary ImageCoords { get { return mImageCoords; } }
public Vector2 UVUpperLeft { set { mUVUpperLeft = value; } get { return mUVUpperLeft; } }
public Vector2 UVLowerRight { set { mUVLowerRight = value; } get { return mUVLowerRight; } }
///
/// Create a new sprite using the entire spritesheet as the texture
///
public ParticleSprite()
: this ("CrispDot")
{
}
///
/// Create a new sprite using a region of the spritesheet as the texture
///
/// The name of the image within the sprite sheet to use
public ParticleSprite(string imageName)
{
PopulateImages();
// Lookup the appropriate image (allow an exception to be thrown if an image isn't found)
mUVUpperLeft = new Vector2(mImageCoords[imageName].X, mImageCoords[imageName].Y);
mUVLowerRight = new Vector2(mImageCoords[imageName].Z, mImageCoords[imageName].W);
}
///
/// Create a new sprite using a region of the spritesheet as the texture
///
/// UV Coords of the upper left corner
/// UV Coords of the lower right corner
public ParticleSprite(Vector2 uvUpperLeft, Vector2 uvLowerRight)
{
PopulateImages();
mUVUpperLeft = uvUpperLeft;
mUVLowerRight = uvLowerRight;
}
///
/// Load in the sprite sheet and image coordinates
///
private static void PopulateImages()
{
// Load the sprite sheet from the assembly's meta data
if (SpriteSheet == null)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string[] resourceNames = assembly.GetManifestResourceNames();
foreach (string resourceName in resourceNames)
{
if (resourceName == "Pina3D.Resources.resources")
{
SpriteSheet = Pina.ResourceContent.Load(@"ParticleSpritesSTD");
}
}
}
// Fill the image lookup info
if (mImageCoords == null)
{
// Vector4 format: (left U, top V, right U, bottom V) == (upperLeft(u,v), lowerRight(u,v))
mImageCoords = new Dictionary();
mImageCoords.Add("CrispDot", new Vector4(0f, 0f, .1f, .1f));
mImageCoords.Add("FuzzyGreyDot", new Vector4(.1f, .05f, .15f, .10f));
mImageCoords.Add("Blast1", new Vector4(.20f, .10f, .30f, .20f));
mImageCoords.Add("FuzzyBlast", new Vector4(.1f, 0f, .15f, .05f));
mImageCoords.Add("Spark", new Vector4(.1f, .1f, .2f, .2f));
mImageCoords.Add("Squiggle", new Vector4(.15f, 0f, .20f, .10f));
mImageCoords.Add("Star", new Vector4(0f, .10f, .10f, .20f));
mImageCoords.Add("Puff", new Vector4(.20f, 0f, .30f, .10f));
mImageCoords.Add("Meteorite", new Vector4(0f, .20f, .20f, .40f));
}
}
}
}