// Project: XnaTetris, File: SpriteHelper.cs // Namespace: XnaTetris.Helpers, Class: SpriteHelper // Path: C:\code\XnaTetris\Helpers, Author: Abi // Code lines: 62, Size of file: 2,14 KB // Creation date: 13.10.2006 23:14 // Last modified: 20.10.2006 08:29 // Generated with Commenter by abi.exDream.com #region Using directives using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework; #endregion namespace XnaTetris.Helpers { /// /// Sprite helper class to manage and render sprites. /// public class SpriteHelper { #region SpriteToRender helper class class SpriteToRender { public Texture2D texture; public Rectangle rect; public Rectangle? sourceRect; public Color color; public SpriteToRender(Texture2D setTexture, Rectangle setRect, Rectangle? setSourceRect, Color setColor) { texture = setTexture; rect = setRect; sourceRect = setSourceRect; color = setColor; } // SpriteToRender(setTexture, setRect, setColor) } // SpriteToRender #endregion #region Variables /// /// Keep a list of all sprites we have to render this frame. /// static List sprites = new List(); /// /// Sprite batch for rendering /// static SpriteBatch spriteBatch = null; /// /// Texture for this sprite /// Texture2D texture; /// /// Graphic rectangle used for this sprite inside the texture. /// Can be null to use the whole texture. /// Rectangle gfxRect; #endregion #region Constructor public SpriteHelper(Texture2D setTexture, Rectangle? setGfxRect) { texture = setTexture; if (setGfxRect == null) gfxRect = new Rectangle(0, 0, texture.Width, texture.Height); else gfxRect = setGfxRect.Value; } // SpriteHelper(setTexture, setGfxRect) #endregion #region Dispose /// /// Dispose the static spriteBatch and sprites helpers in case /// the device gets lost. /// public static void Dispose() { sprites.Clear(); if (spriteBatch != null) spriteBatch.Dispose(); spriteBatch = null; } // Dispose() #endregion #region RenderSprite public void Render(Rectangle rect, Color color) { sprites.Add(new SpriteToRender(texture, rect, gfxRect, color)); } // Render(texture, rect, sourceRect, color) public void Render(Rectangle rect) { Render(rect, Color.White); } // Render(texture, rect, sourceRect) public void Render(int x, int y, Color color) { Render(new Rectangle(x, y, gfxRect.Width, gfxRect.Height), color); } // Render(texture, rect, sourceRect) public void Render(int x, int y) { Render(new Rectangle(x, y, gfxRect.Width, gfxRect.Height)); } // Render(texture, rect, sourceRect) public void Render() { Render(new Rectangle(0, 0, 1024, 768)); } // Render(texture) public void RenderCentered(float x, float y, float scale) { Render(new Rectangle( (int)(x * 1024 - scale * gfxRect.Width/2), (int)(y * 768 - scale * gfxRect.Height/2), (int)(scale * gfxRect.Width), (int)(scale * gfxRect.Height))); } // RenderCentered(x, y) public void RenderCentered(float x, float y) { RenderCentered(x, y, 1); } // RenderCentered(x, y) public void RenderCentered(Vector2 pos) { RenderCentered(pos.X, pos.Y); } // RenderCentered(pos) #endregion #region DrawSprites public static void DrawSprites(int width, int height) { // No need to render if we got no sprites this frame if (sprites.Count == 0) return; // Create sprite batch if we have not done it yet. // Use device from texture to create the sprite batch. if (spriteBatch == null) spriteBatch = new SpriteBatch(sprites[0].texture.GraphicsDevice); Texture2D lastSpriteTexture = null; bool spriteBatchStarted = false; // Render all sprites foreach (SpriteToRender sprite in sprites) { // Start rendering sprites // Note: Now moved inside loop to fix most render sorting errors! if (lastSpriteTexture != sprite.texture) { if (spriteBatchStarted) spriteBatch.End(); spriteBatchStarted = true; spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None); } // if spriteBatch.Draw(sprite.texture, // Rescale to fit resolution new Rectangle( sprite.rect.X * width / 1024, sprite.rect.Y * height / 768, sprite.rect.Width * width / 1024, sprite.rect.Height * height / 768), sprite.sourceRect, sprite.color); } // foreach // We are done, draw everything on screen with help of the end method. if (spriteBatchStarted) spriteBatch.End(); // Kill list of remembered sprites sprites.Clear(); } // DrawSprites() #endregion } // class SpriteHelper } // namespace XnaTetris