// Project: XnaShooter, File: AnimatedTexture.cs // Namespace: XnaShooter.Graphics, Class: AnimatedTexture // Path: C:\code\XnaBook\XnaShooter\Graphics, Author: abi // Code lines: 46, Size of file: 1,03 KB // Creation date: 07.12.2006 18:22 // Last modified: 07.12.2006 21:41 // Generated with Commenter by abi.exDream.com #region Using directives using System; using System.Collections.Generic; using System.Text; using System.IO; using XnaShooter.Helpers; using XnaShooter.Game; using Microsoft.Xna.Framework.Graphics; using XnaTexture = Microsoft.Xna.Framework.Graphics.Texture2D; using XnaShooter.Sounds; using Microsoft.Xna.Framework; #endregion namespace XnaShooter.Graphics { /// /// Animated texture /// public class AnimatedTexture : Texture { #region Variables /// /// Textures for the animation. /// You can still use d3dTexture, it will just hold current animation /// texture! See Select(.). /// private XnaTexture[] xnaTextures = null; /// /// Get animation length (how many textures are used inside of this /// animated texture). Can be used for the Select(.) method. /// /// Int public int AnimationLength { get { return xnaTextures != null ? xnaTextures.Length : 1; } // get } // AnimationLength /// /// Get animated texture /// /// Animation number /// XnaTexture public XnaTexture GetAnimatedTexture(int animationNumber) { return xnaTextures[animationNumber % xnaTextures.Length]; } // GetAnimatedTexture(aniNumber) #endregion #region Constructor /// /// Create animated texture, don't allow empty constructor from outside. /// protected AnimatedTexture() { } // AnimatedTexture() /// /// Create animated texture from given filename. /// Will go through all filenames with textureFilename + "0001", "0002", /// etc. until the filename is not longer found. /// /// Set filename, must be relative and /// without the 0001.dds, 0002.dds, etc. extension. public AnimatedTexture(string setFilename) { if (String.IsNullOrEmpty(setFilename)) throw new ArgumentNullException("setFilename", "Unable to create texture without valid filename."); // Set filename texFilename = StringHelper.ExtractFilename(setFilename, true); // Try to load texture try { string filenameFirstPart = Path.Combine(Directories.ContentDirectory, texFilename); // Build full filename string fullFilename = filenameFirstPart + "0001"; // Check if file exists, else we can't continue loading! if (File.Exists(fullFilename+".xnb") == false) throw new FileNotFoundException(fullFilename + ".xnb"); // Try loading as 2d texture internalXnaTexture = BaseGame.Content.Load(fullFilename); // Get info from the texture directly. texWidth = internalXnaTexture.Width; texHeight = internalXnaTexture.Height; // We will use alpha for Dxt3, Dxt5 or any format starting with "A", // there are a lot of those (A8R8G8B8, A4R4G4B4, A8B8G8R8, etc.) hasAlpha = internalXnaTexture.Format == SurfaceFormat.Dxt5 || internalXnaTexture.Format == SurfaceFormat.Dxt3 || internalXnaTexture.Format.ToString().StartsWith("A"); loaded = true; CalcHalfPixelSize(); // Ok, now load all other animated textures List animatedTextures = new List(); animatedTextures.Add(internalXnaTexture); int texNumber = 2; while (File.Exists(filenameFirstPart + texNumber.ToString("0000")+".xnb")) { animatedTextures.Add(BaseGame.Content.Load( filenameFirstPart + texNumber.ToString("0000"))); texNumber++; } // while (File.Exists) xnaTextures = animatedTextures.ToArray(); } // try catch (Exception ex) { // Failed to load loaded = false; Log.Write("Failed to load animated texture " + texFilename + ", will use empty texture! Error: " + ex.ToString()); } // catch (ex) } // AnimatedTexture(name) #endregion #region Select /// /// Select this texture as texture stage 0 /// /// Number public void Select(int animationNumber) { if (xnaTextures != null && xnaTextures.Length > 0) { // Select new animation number internalXnaTexture = xnaTextures[animationNumber % xnaTextures.Length]; } // if } // Select(num) #endregion #region Unit Testing #if DEBUG /// /// Test textures /// public static void TestAnimatedTexture() { AnimatedTexture explosionTexture = null; TestGame.Start("TestAnimatedTexture", delegate { explosionTexture = new AnimatedTexture("destroy"); }, delegate { explosionTexture.Select((int)BaseGame.TotalTimeMs / 33); int screenSize = Math.Max(BaseGame.Width * 2 / 19, BaseGame.Height * 2 / 19); explosionTexture.RenderOnScreen(new Rectangle( BaseGame.Width / 2 - screenSize / 2, BaseGame.Height / 2 - screenSize / 2, screenSize, screenSize)); if (Input.MouseLeftButtonJustPressed) Sound.Play(Sound.Sounds.Explosion); }); } // TestAnimatedTexture() #endif #endregion } // class AnimatedTexture } // namespace XnaShooter.Graphics