# [.net] Side Scroller Lag

## Recommended Posts

I am writing a side scroller using C# and MDX. I am only drawing what's being shown on the screen. In other words, I don't draw the whole map, only the part being shown on the screen. When my game starts off, it renders at 60 FPS. It stays at 60 FPS until I get closer to the end of the map. Then it drops drastically to about 20 FPS. Any ideas what might be going on? Here is my code for drawing the map.
        void drawMap(float fScrollInc)
{
int[] iCounter1 = new int[3];
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < str_Maze[0].Length - 1; j++)
{

char cTile = str_Maze[i][j];
switch (cTile)
{
case '0':
//395, 197
//if (Sky[iCounter1[0]].myPosition.X >= (-32) && Sky[iCounter1[0]].myPosition.X <= (200))
if (Sky[iCounter1[0]].myPosition.X > (-32) && Sky[iCounter1[0]].myPosition.X <= (200))
Sky[iCounter1[0]].draw();
Sky[iCounter1[0]].myPosition.X -= fScrollInc;
iCounter1[0]++;

break;
case '1':
if (Grass[iCounter1[1]].myPosition.X > (-32) && Grass[iCounter1[1]].myPosition.X <= (200))
Grass[iCounter1[1]].draw();
Grass[iCounter1[1]].myPosition.X -= fScrollInc;
iCounter1[1]++;

break;
case '2':
if (Dirt[iCounter1[2]].myPosition.X > (-32) && Dirt[iCounter1[2]].myPosition.X <= (200))
Dirt[iCounter1[2]].draw();
Dirt[iCounter1[2]].myPosition.X -= fScrollInc;
iCounter1[2]++;

break;
}

}
}
}


##### Share on other sites
Bob Janova    769
I don't see how this works. Why is MyPosition an element of all the tiles? Is that the position of the tile, or the player? If the tile, then why the 'My'?

If it does what I think, you want to move the tile.Position -= scroll (please rename the member!) above the test. I don't see why it should be slower when the scroll increment is higher, though.

##### Share on other sites
I have developed a class called DXSprite. Every tile in my map including my player is a DXSprite Object. MyPosition is the position of that paticular instance of the DXSprite. Every tile has its own MyPosition, which is a Vector3, respectively. Perhaps I should consider renaming some of the members.

My code does exactly what you mentioned,

Dirt[iCounter1[2]].myPosition.X -= fScrollInc;

If you want to see exactly what my problem is, here is a demo of my application (15KB). Run it and scroll back and forth. You will notice that the frame rate drops drastically when you reach the end of the level on the right side.

http://www.brandonfogerty.com/programming/projects/files/MDXMapTest.zip

GOD Bless you Always!!!!!

##### Share on other sites
thezbuffer    706
I don't see any variation in fps. I"m running an ATI 9800 and I get 15fps if I'm not moving and 7fps while moving no matter where I am in the scene.

Given how little you are drawing there is something very wrong here.

Why write your own sprite class there is a perfectly good (and fast) sprite class already in D3Dx which allows you to batch and sort very efficiently.

##### Share on other sites
Actually my own sprite class uses the MDX Sprite Class however I have added functionality like Animations and Bitmapped fonts. Please try the program again. I have a new version avaible. It seems that It runs fine on my desktop computer but on my laptop( which I am doing the development on) it acts the way I described earlier. I get around 70 FPS but when I get closer to the end of the level, the FPS drop drastically to about 15 FPS. Any ideas would be greatly appreciated! Thanks in advance!

##### Share on other sites
thezbuffer    706
Yes I see the same a you now. I notice the framerate also increases when I hit the black at the end. The slower parts are when there are more tiles visible too.

So either:
1. You are incorrect and you are drawing more of the world than you think. I suggest adding a counter in your draw call so that you can print the number of tiles rendered to your screen
2. There is some big inefficiency in your draw() code. Put some profiling code using the StopWatch class in the draw call so you can see how long its taking.

##### Share on other sites
I think it is the latter because I have run a test where I drew less than the screen width on both the left and right hand sides, so in other words I know that I am only drawing what is on the screen. I still got the same results. It must be something with the sprite drawing code but I am not sure what.

It seems if I run it on my dekstop (ATI Radeon 7500), the Frame rate never decreases, it runs as it should. However some of the graphics, the player, are a little bit corrupted.

On my laptop (ATI MOBILITY Radeon X300) however, the graphics are fine but I get the decrease in FPS near the end of the level.

Thanks for looking at it.

##### Share on other sites
Skoobydoo    133
I just get errors when I try to run it...

Could not load file or assembly 'Microsoft.DirectX.DirectDX...

Any ideas?

Do you need to add a dll or something?

When I tell it to ignore then I get a black screen and that is all.

##### Share on other sites
thezbuffer    706
Skooby: You probably don't have the right managed directx runtime on your machine - run the web installer

(unless he is using MDX 2.0 in which case you need to install the full SDK)

##### Share on other sites
xeddiex    238
Wierd cause my FPS is a constant 60 no matter what I do. There's also another problem: When you move a window over the game, it'll throw an unhandled exception error.

- xeddiex