XNA Component - 2d Background and Drawing Components
Tuesday, September 05 2006
Well I finally got to sit down and play with XNA. One of the things I have been doing a lot with MDX is playing around with 2D, so I thought a good start would be to play and convert the code that I already have. The first thing I needed to make the transfer was the ability to draw a static background for my Applications (In the future I will make it animated, but lets move in small steps).
For starters I started with the XNA Documentation and the First Game Example, this is just a simple bouncing sprite on a plain background. Next I wanted to use the features of the components as this background object would be used in more then one of my applications. To start with this I started to look at all of the code that was popping up all over the place and breaking down what was being done. For those who are interested if you drop over to the XNA Game Studio Express groups you will find lots of different small games being announced every day.
To get started with designing components I would suggest dropping over to the XNA Team Blog and download the Video that Mitch Walker has done on the Game Components. After watching this i thought I would finally give it a go, there is a good example of a spinner control in the video. So here is my code, it is a little ruff but I am going to tidy it up and present it properly later (I will be writing up a full Tutorial on what I am doing, and posting it on a new project we are working on)
Code Attached...
When working on this example though I did find a small problem with the component system. What I found is that there is no way to determine the order that the components are rendered onto the device/display. The only way you can control the order is to make sure that you add the controls in the order that you want them displayed. I found this out as I was going and following the examples, when you do follow the examples and start your application off by using the wizard you are presented with a draw call like the following...
protected override void Draw()
{
// Make sure we have a valid device
if (!graphics.EnsureDevice())
return;
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
graphics.GraphicsDevice.BeginScene();
// TODO: Add your drawing code here
// Let the Game Components draw
Draw Components();
graphics.GraphicsDevice.EndScene();
graphics.GraphicsDevice.Present();
}
The problem is that the section where you are advised to draw your objects happens before you render the components. In my example and code doing it this way would mean that my sprites are drawn first then the background is drawn over the top. For the moment to fix this I have swapped this around so that the draw components happens first and my custom drawing is done next. All I have to remember with this example is that I need to add my components in order and I should be right.
Over the next few days I will be working on a small post that will go through changing the base template so that these lines are swapped around, But for now I have posted this find in the feedback section for the XNA System.
** I need to remind every one that if you do find problems/bugs or have suggestions for the XN System and Game Studio Express you should post your comments to the feedback centre so that the XNA Teams can follow there progress. **
