简体   繁体   中英

DirectX Release function with Textures (C++)

I have a question regarding DirectX 9, textures and releasing them. I'm currently building a 2D game in C++. I have many textures that I'm loading with D3DXCreateTextureFromFileEx, and then when moving on from that 'scene' releasing them with Texture->Release(); which is fine.

I've made a class which deals with the game's item list. In it, I'm loading textures for each of the items when the class is created, and when the game exits it releases each of those textures. However, I keep running into a problem.

Even though I have already loaded the texture, if I do not ->draw() it, the program will crash when trying to release it.

Can anyone explain why this is please? I imagine I will need some sort of conditional check to see if the texture has been drawn before I attempt to ->release() it?

EDIT:

Here's a much watered down version of what I was trying to describe. This works, but if you comment out the d3dspt->Draw line, it fails when releasing the texture. It appears to only be able to release textures once they have been drawn. I assumed you would need to release them after you've loaded and finished with them, regardless if they have been drawn or not. I was just looking for a confirmation, explanation or anything else that might enlighten me. For now, I have added a boolean to each texture to check whether it's been drawn or not when I come to releasing it.

LPDIRECT3DTEXTURE9 Texture;

D3DXCreateTextureFromFileEx(d3ddev,
                        L"Texture.png",
                        D3DX_DEFAULT,
                        D3DX_DEFAULT,
                        D3DX_DEFAULT,
                        NULL,
                        D3DFMT_A8R8G8B8,
                        D3DPOOL_MANAGED,
                        D3DX_DEFAULT,
                        D3DX_DEFAULT,
                        NULL,
                        NULL,
                        NULL,
                        &Texture);

d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
d3dspt->Begin(D3DXSPRITE_ALPHABLEND);

D3DXVECTOR3 TextureCentre(0.0f, 0.0f, 1.0f);
D3DXVECTOR3 TexturePos(0.0f, 0.0f, 1.0f);

d3dspt->Draw(Texture, NULL, &TextureCentre, &TexturePos, D3DCOLOR_ARGB(255, 25, 255, 255));

d3dspt->End();
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);

Texture->Release();`
IDirect3DTexture9 *Texture;

Texture = //Load Texture. Hope u did it.

When Releasing

if(Texture)
{
    Texture->Release();
    Texture = NULL;
}

if this doesn't solve it. Upload ur code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM