Skip to main content
GameDev.net gamedev.net
🔒 Locked

lag problem

Started by TaxCollector Feb 24, 2008 at 9:14 AM 2 replies 650+ views
Original Post
TaxCollector
TaxCollector
This function sometimes takes over 100ms to run through. I use this function in my game to render frames. It's a tile based game so I have to draw the tiles all over the map. I'm using a second tile layer for my buildings/objects. But since "mapsize" is only 32 I thought it shouldn't cause any lag but I guess I'm wrong. Can anyone help me get rid of the lag? tile based games have existed for years and even the computers back then managed to draw the tiles without lag. So why can't my modern computer do it properly. btw I'm 100% sure this is the only function causing the lag because I've been timing it.

void render_frame(void)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
	d3ddev->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
    d3ddev->BeginScene();	
    d3dspt->Begin(NULL);

	for (int i=0;i<mapsize;i++)
		{
			for (int j=0;j<mapsize*4;j++)
			{
				D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);    // center at the upper-left corner
				int PlotX=i*imagesize+(j % 2)*(imagesize/2)-l;
				int PlotY=j*(imagesize/4)-m-imagesize;
				D3DXVECTOR3 position(PlotX,PlotY, 0.5f); 
				d3dspt->Draw(tile[j].spri, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
			}
		}
	
	for (int j=0;j<mapsize*4;j++)
	{
		for (int i=0;i<mapsize;i++) 
			{	
				int PlotX=i*imagesize+(j % 2)*(imagesize/2)-l;
				int PlotY=j*(imagesize/4)-m-imagesize;	
				D3DXVECTOR3 position(PlotX,PlotY, 1.0/float(j));
				D3DXVECTOR3 center(tile2[j].occ.x,tile2[j].occ.y, 0.0f);
				d3dspt->Draw(tile2[j].occ.spri, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
			}
	}
	
	
	if(starteddragging==true && cantbuildroad==false)
	{

			for (int i=0;i<a;i++) 
				{	
					int PlotX=roadpath.inumber*imagesize+(roadpath.jnumber % 2)*(imagesize/2)-l;
					int PlotY=roadpath.jnumber*(imagesize/4)-m-imagesize;	
					D3DXVECTOR3 position(PlotX,PlotY, 0);
					D3DXVECTOR3 center(0,0, 0.0f);
					d3dspt->Draw(roadpath.spri, NULL, &center, &position, D3DCOLOR_ARGB(127, 0, 255,0));
				}
	}

	if(roadbuilding==true)
	{
		uppertile[xbuilding][ybuilding].occ=startbuilding;
		int PlotX=xbuilding*imagesize+(ybuilding % 2)*(imagesize/2)-l;
		int PlotY=ybuilding*(imagesize/4)-m-imagesize;
		D3DXVECTOR3 position(PlotX,PlotY,0);
		D3DXVECTOR3 center(startbuilding.x,startbuilding.y, 0.0f);

		if(checkavailableland(startbuilding)==true ||(tile2[xbuilding][ybuilding].parent->occ.btype==road &&startbuilding.btype==road))
			d3dspt->Draw(uppertile[xbuilding][ybuilding].occ.spri, NULL, &center, &position, D3DCOLOR_ARGB(127, 0, 255, 0));
		else
			d3dspt->Draw(uppertile[xbuilding][ybuilding].occ.spri, NULL, &center, &position, D3DCOLOR_ARGB(127, 255, 0, 0));
	}
	
	//draw interface
	D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 position(0,0,0);
	//
	d3dspt->Draw(inter, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
	
    d3dspt->End();
    d3ddev->EndScene();
    d3ddev->Present(NULL, NULL, NULL, NULL);
    return;
}
Buckeye
Buckeye
12 micro-seconds per draw.. plus whatever the 0 to "a" loop takes.. I'm thinking that doesn't seem too horrible.

Also, is it a typo or do you do 2 clears?

That adds something.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
TaxCollector
TaxCollector
I was indeed doing 2 clears and I've removed 1 so thanks for noticing.
and 'a' is around 20.
Evil Steve
Evil Steve
What do you mean it "sometimes" takes 100ms? You're making ~5140 draw calls to the sprite, you could try Flush()ing it once or twice in there and see if that makes it any faster.

EDIT: 2 other things:
1. You should really be sorting by texture for the sprite (Pass D3DXSPRITE_SORT_TEXTURE instead of NULL (Which should be 0 technically, btw) to Begin())

2. What is your present interval? Imediate or default? If it's default, Present() will block to keep rendering at 60Hz (Or whatever your monitors refresh rate is). Also, Present() can block to stop the GPU getting too far ahead of the CPU, and timing it isn't really meaningful.
In fact, using CPU time to time Direct3D calls is usually pretty meaningless, you should use a GPU profiler like NVPerfHUD instead.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.