Jump to content
  • Advertisement
Sign in to follow this  
yewbie

Directx9 Program is eating up massive chunk of memory

This topic is 3272 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am very new to directx9 and I am also programming my first isometric tile plotting sample program. Here is what is happening, everything draws fine but the memory used by the program just keeps going up and up and up and finally crashes. I will post some code here and see if you guys know what could be causing it? I think it might have something to do with my vertex buffer? do I need to clear it every frame? I just assumed it would overwrite. here is my rendering loop
void GraphicsWrapper::DrawTile(int x, int y, float rotate)
{	
	TLVERTEX* vertices; //my vertices
	
	
	vertexBuffer->Lock(0, 0, (void **)&vertices, NULL);//Lock the vertex buffer
	

	//Setup vertices
	//A -0.5f modifier is applied to vertex coordinates to match texture and screen coords
	//Some drivers may compensate for this automatically, but on others texture alignment errors are introduced
	//More information on this can be found in the Direct3D 9 documentation
	
	
	int Lighting =  MapInfo[x][y][0].Lighting;
	float V0Height = MapInfo[x][y][0].v0Height;
	float V1Height = MapInfo[x][y][0].v1Height;
	float V2Height = MapInfo[x][y][0].v2Height;
	float V3Height = MapInfo[x][y][0].v3Height;
	
	//Calculate Center Point
	//float CenterX= (float) ( (x*TILE_WIDTH) + (y&1) * (TILE_WIDTH/2));
	//float CenterY= (float) (y*(TILE_HEIGHT/2));
	float CenterX=(float)(x*TILE_WIDTH+(y&1)*(TILE_WIDTH/2));
	float CenterY=(float)(y*(TILE_HEIGHT/2));

	//top left
	vertices[0].colour = D3DCOLOR_ARGB (255,255 - Lighting,255 - Lighting,255 - Lighting);
	vertices[0].x = CenterX - TILE_WIDTH/2;
	vertices[0].y = (CenterY) - V0Height;
	vertices[0].z = 0.0f;
	vertices[0].rhw = 1.0f;
	vertices[0].u = 0.0f;
	vertices[0].v = 0.0f;
	


	//top right
	
	//vertices[1].colour = D3DCOLOR_ARGB (255,255,255,255);
	vertices[1].colour = D3DCOLOR_ARGB (255,255 - Lighting,255 - Lighting,255 - Lighting);
	vertices[1].x = CenterX;
	vertices[1].y = CenterY - TILE_HEIGHT/2  - V1Height;
	vertices[1].z = 0.0f;
	vertices[1].rhw = 1.0f;
	vertices[1].u = 1.0f;
	vertices[1].v = 0.0f;

	
    //bottom right
	
	//vertices[2].colour = D3DCOLOR_ARGB (255,255,255,255);
	vertices[2].colour = D3DCOLOR_ARGB (255,255 - Lighting,255 - Lighting,255 - Lighting);
	vertices[2].x = CenterX ;
	vertices[2].y = CenterY + TILE_HEIGHT/2 - V2Height;
	vertices[2].z = 0.0f;
	vertices[2].rhw = 1.0f;
	vertices[2].u = 0.0f;
	vertices[2].v = 1.0f;

	

	//bottom left
	
	//vertices[3].colour = D3DCOLOR_ARGB (255,255,255,255);
	vertices[3].colour = D3DCOLOR_ARGB (255,255 - Lighting,255 - Lighting,255 - Lighting);
	vertices[3].x = CenterX + TILE_WIDTH/2;
	vertices[3].y = CenterY - V3Height;
	vertices[3].z = 0.0f;
	vertices[3].rhw = 1.0f;
	vertices[3].u = 1.0f;
	vertices[3].v = 1.0f;
	

	
	//Handle rotation
	if (rotate != 0)
	{
		
	}

	//Unlock the vertex buffer
	vertexBuffer->Unlock();





	IDirect3DTexture9 *d3dTexture;
	//Use a magenta colourkey
	D3DCOLOR colorkey = 0xFFFF00FF;
	// Load image from file
    if (FAILED(D3DXCreateTextureFromFileEx (d3dDevice, "Texture.jpg", 0, 0, 1, 0, 
          D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT, 
          colorkey, NULL, NULL, &d3dTexture)))
    {
        return;
    }
  

	//Set texture
	d3dDevice->SetTexture (0, d3dTexture);
	
	d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
	//Draw image
	d3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 4 );

}



And here is how I draw that using my present
HRESULT hr;

	hr = d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0, 0.0f, 0); //clear our backbuffer
	if (hr != D3D_OK) 
		return false;


	//Begin drawing
	hr = d3dDevice->BeginScene();
	if (hr != D3D_OK) 
		return false;
	
	for (int x=0;x<10;x++)
		{
			for (int y=0;y<25;y++)
			{
				DrawTile(x,y, 0);
			}
		}
	
	
	
	//End drawing
	hr = d3dDevice->EndScene();
	if (hr != D3D_OK) 
		return false;


	hr = d3dDevice->Present (NULL, NULL, NULL, NULL);//present our scene
	if (hr != D3D_OK) 
		MessageBox(hWndMain, "Present Error", "ERROR", MB_OK);
		return false;


Any help would be greatly appreciated Im pretty sure it has to be something im missing with the vertex buffer. I tried clearing it before my vertex processing but that just crashed it out :/

Share this post


Link to post
Share on other sites
Advertisement
You are creating your texture again and again at every tile. This will quickly eat up memory.

Your vertex buffer usage is also inefficient, but it is not the source of the crash.

Share this post


Link to post
Share on other sites
One thing I notice is that you load the texture once per tile per frame. This will kill your performance, and since you never release the texture is probably also your memory leak.

Quote:

IDirect3DTexture9 *d3dTexture;
//Use a magenta colourkey
D3DCOLOR colorkey = 0xFFFF00FF;
// Load image from file
if (FAILED(D3DXCreateTextureFromFileEx (d3dDevice, "Texture.jpg", 0, 0, 1, 0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
colorkey, NULL, NULL, &d3dTexture)))
{
return;
}


Instead, load the texture once in the start of your program, and then reuse it. Also, remember to always release your resources when you are done with them. E.i. d3dTexture->Release();

Share this post


Link to post
Share on other sites
I was going to put all of my texture code in a class I just was doing some test apps (this one) =p

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!