using System.Linq; using Chernobyl.Collections.Generic.Event; using Chernobyl.Graphics.Drawing; using Chernobyl.Graphics.Texture; using Microsoft.Xna.Framework.Graphics; namespace Chernobyl.Graphics.Xna.Texture { /// /// Allows access to the XNA back buffer. /// public class XnaBackBuffer : Drawable, IBackBuffer { /// /// Configures this render target. /// /// The object that holds the services. /// The width of the render target. This must be a /// power of two. /// The height of the render target.This must be a /// power of two. /// The number of mipmap levels to generate. /// See http://en.wikipedia.org/wiki/Mipmap for more information /// about mipmaps. Set this to 0 to generate all mipmaps down to 1x1 pixels. /// The format of the texture (aka, surface). public void Configure(IEventCollection services, uint width, uint height, uint mipmapLevels, TextureFormat format) { if(IsConfigured == false) { GraphicsDevice = services.OfType().First(); IsConfigured = true; } } /// /// Called before the "Draw" method to apply any states /// or preconditions. /// public override void PreDraw() { // grab the current render target PreviousTarget = GraphicsDevice.GetRenderTarget(0); // set the back buffer GraphicsDevice.SetRenderTarget(0, null); // clear this render target GraphicsDevice.Clear(ClearOptions.Target, Microsoft.Xna.Framework.Graphics.Color.CornflowerBlue, 0, 0); } /// /// Called after the "Draw" method to remove any states /// or preconditions. /// public override void PostDraw() { // switch back to the previous device. GraphicsDevice.SetRenderTarget(0, PreviousTarget as Microsoft.Xna.Framework.Graphics.RenderTarget2D); } /// /// The number of mipmaps generated for this render target. /// public uint MipmapLevelsCount { get { return 1; } } /// /// The width of the render target. /// public uint Width { get { return (uint)GraphicsDevice.PresentationParameters.BackBufferWidth; } } /// /// The height of the render target. /// public uint Height { get { return (uint)GraphicsDevice.PresentationParameters.BackBufferHeight; } } /// /// True if the render target has been configured, false if /// otherwise. /// bool IsConfigured { get; set; } /// /// The XNA graphics device. /// GraphicsDevice GraphicsDevice { get; set; } /// /// Holds the past render target when PreDraw is called. /// RenderTarget PreviousTarget { get; set; } } }