المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : XNA: Game updating slowly



C# Programming
11-05-2009, 09:30 AM
Hello there I am hoping that someone might be able to help me optimize my code for an XNA Program I have made, basically I have made my own custom interface in XNA, now my interface works pretty similar to the ways that windows does, however if you nest too many controls inside each other, then the game really starts to run at a slow rate (fps wise), as it obviously the draw method for the controls are taking too long.

I have tried doing a background draw (using an event via BeginInvoke and EndInvoke), but I get lots of flashing and sometimes it even crashes, as it tries to access the Sprite Batch sometimes when it is still is use

Is there another way to optimize my code that will allow me to get my performance back again??

Here is a sample of the draw code for a container object:


if (m_pTexture != null)
{
graphicsManager.Begin();
if (bSetupClientArea)
graphicsManager.SetupClientRect(TranslatedArea);
if (Image_States == 0 || Image_States == 1)
graphicsManager.Draw(m_pTexture, TranslatedArea, new Color(255, 255, 255, (byte)Alpha));
else
{
int nSubHeight = m_pTexture.Height / Image_States;
Rectangle src = new Rectangle(0, (int)Current_State * nSubHeight, m_pTexture.Width, nSubHeight);
graphicsManager.Draw(m_pTexture, TranslatedArea, src, new Color(255, 255, 255, (byte)Alpha));
}
graphicsManager.End();
DrawBorder(graphicsManager);
}
for (int i = 0; i < m_pComponents.Count; ++i)
{
Component component = m_pComponents[i];
if (component.Visible)
component.Draw(gameTime, graphicsManager, true);
}


As you can see each container has its own Rectangular Area, and it will basically use the graphics manager (which contains the sprite batch object for my XNA Program) to setup it's clip area and draw all of the contents to it, of course using this method means every container has to use begin and end from the graphics manager, to make sure that the objects are drawn correctly

Does calling the SpriteBatch.Begin and End function numerous times cause it to slow down considerably??
Or do you think something else is causing it to slow down??