SIGN UP MEMBER LOGIN:    
ARTICLE

How to use IDrawable in XNA

Posted by Ibrahim Ersoy Articles | XNA with C# August 09, 2010
In this mini article I will show you how to use IDrawable Interface.
Reader Level:

In this mini article I will show you how to use IDrawable Interface.

In your game project add an extra IDrawable near your Game object and Implement interface "IDrawable". 

1.gif 

It will add this region with some functions and events:

#region IDrawable Members

public new void Draw(GameTime gameTime)
{
  throw new NotImplementedException();
}

public int DrawOrder
{
  get { throw new NotImplementedException(); }
}

public event EventHandler<EventArgs> DrawOrderChanged;

public bool Visible
{
  get { throw new NotImplementedException(); }
}

public event EventHandler<EventArgs> VisibleChanged;

#endregion

Generally Draw function is already being used in the Game1.cs so its better you remove the first one added.Dont forget to insert these codes inside your new Draw Function:

GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);

So the updated structure will be like that:

public class Game1 : Microsoft.Xna.Framework.Game,IDrawable
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }
    protected override void Initialize()
    {        
        base.Initialize();
    }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
    }
    protected override void UnloadContent()
    {
    }
    protected override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
    }

    #region IDrawable Members
    public new void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        base.Draw(gameTime);
    }
    public int DrawOrder
    {
        get { throw new NotImplementedException(); }
    }
    public event EventHandler<EventArgs> DrawOrderChanged;
    public bool Visible
    {
        get { throw new NotImplementedException(); }
    }
    public event EventHandler<EventArgs> VisibleChanged;
    #endregion
}

DrawOrder is used when to decide which DrawableGameComponents should draw in order. Such as:

DrawableGameComponent d3 = new DrawableGameComponent(this);
d3.DrawOrder = 3;
this.Components.Add(d3);

DrawableGameComponent d1 = new DrawableGameComponent(this);
d1.DrawOrder = 1;
this.Components.Add(d1);

DrawableGameComponent d2 = new DrawableGameComponent(this);
d2.DrawOrder = 2;
this.Components.Add(d2);

We can set its order here and draw it in order.

we have an event named DrawOrderChanged.When we set this,this event throws and we can add anything to control draworder functionality.

Visible indicates whether IDrawable.Draw should be called in Game.Draw for the game components.

And we can even know if a visibility changed so;

When adding Idrawable make sure you implement it on a DrawableGameComponent.Its much more useful there.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor