DirectX 10 and drawing a sprite

Hi there! I am trying to make a class that makes it easier for me to draw images with DirectX 10. What it's supposed to do is that when I write Sprite(L"Testsprite.png", 300, 300, 200, 200); in my Render() function, it will create the shader resource view and all that shit that's needed to draw, and put it in the sprite pool. However, it is not working and I've been trying to find the solution for hours.
The problem is that when I run the program, the images is only shown on the first Render() call, then they disappear.

I think the problem may be in the SetSRVPointer function, which looks if an image has been drawn before, and use its shader resource view if it has.

I have removed most of the code like the stuff that set directX up and the window, as I am fairly sure the problem is not there. If it should be relevant, the entire code can be found here: http://pastebin.com/6uD66472
If you're going to run the program on your own computer, remember to make an image called Testsprite.png and put it in the project folder.

I understand there's a lot of code and maybe a little too few comments, but I'd appretiate it a lot of someone would like to waste their time by going through this =)

If there's any information I've forgotten, please tell.
Oh and be nice. I know the code may be horrible, but I'm still an apprentice.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class Sprite
{
public:
	Sprite();
	Sprite(std::wstring fileName, float spriteXPos, float spriteYPos, float spriteWidth, float spriteHeight);

private:
	void SetSRVPointer(std::wstring fileName);
	void CalculateMatWorld();
	void CopyToSpritePool();
	ID3D10ShaderResourceView* CreateSRV(std::wstring fileName);
	ID3D10Texture2D* texture;
	float width;
	float height;
	float xPos;
	float yPos;
	D3DXMATRIX matWorld;

	static int numberOfSprites;
	static std::wstring SRVNames[5];
	static ID3D10ShaderResourceView* SRVPointers[5];
	ID3D10ShaderResourceView *pSpriteSRV;
};

int Sprite::numberOfSprites = 0;
std::wstring Sprite::SRVNames[5];
ID3D10ShaderResourceView* Sprite::SRVPointers[5];

Sprite::Sprite()
{
	width = 0;
	height = 0;
	xPos = 0;
	yPos = 0;
	pSpriteSRV = NULL;
}

Sprite::Sprite(std::wstring fileName, float spriteXPos, float spriteYPos, float spriteWidth, float spriteHeight)
{
	width = spriteWidth;
	height = spriteHeight;
	xPos = spriteXPos;
	yPos = spriteYPos;
	SetSRVPointer(fileName);
	CopyToSpritePool();
}

void Sprite::SetSRVPointer(std::wstring fileName)
{
	bool didNotFindSRV = true;

	// Look for a SRV in RVPointers by comparing fileName to RVNames
	for (int i = 0; i < numberOfSprites; ++i)
	{
		if (fileName.compare(SRVNames[i]) == 0)
		{
			pSpriteSRV = SRVPointers[i];
			didNotFindSRV = false;
		}
	}
	
	// If no SRV is found, create one
	if (didNotFindSRV)
		pSpriteSRV = CreateSRV(fileName);
}

void Sprite::CalculateMatWorld()
{
	D3DXMATRIX matTranslation;
	D3DXMatrixTranslation(&matTranslation, xPos, (windowHeight - yPos), 0.1f);

	D3DXMATRIX matScaling;
	D3DXMatrixScaling(&matScaling, width, height, 1.0f);

	matWorld = (matScaling * matTranslation);
}

void Sprite::CopyToSpritePool()
{
	CalculateMatWorld();
	spritePool[numActiveSprites].pTexture = pSpriteSRV;
	spritePool[numActiveSprites].TexCoord.x = 0;
	spritePool[numActiveSprites].TexCoord.y = 0;
	spritePool[numActiveSprites].TexSize.x = 1.0f;
	spritePool[numActiveSprites].TexSize.y = 1.0f;
	spritePool[numActiveSprites].TextureIndex = 0;
	spritePool[numActiveSprites].ColorModulate = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	spritePool[numActiveSprites].matWorld = matWorld;
	numActiveSprites++;
}

ID3D10ShaderResourceView* Sprite::CreateSRV(std::wstring fileName)
{
	ID3D10ShaderResourceView* SRV = NULL;
	ID3D10Texture2D* texture = GetTexture2DFromFile(fileName.c_str());

	if (texture != NULL)
	{
		D3D10_TEXTURE2D_DESC desc;
		texture->GetDesc(&desc);
		D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc;
		ZeroMemory(&SRVDesc, sizeof(SRVDesc));
		SRVDesc.Format = desc.Format;
		SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
		SRVDesc.Texture2D.MipLevels = desc.MipLevels;

		pD3DDevice->CreateShaderResourceView(texture, &SRVDesc, &SRV);
	}

	else
		MessageBox(NULL, L"Texture not valid", L"", MB_OK);

	texture->Release();


	SRVPointers[numberOfSprites] = SRV;
	SRVNames[numberOfSprites] = fileName;
	numberOfSprites++;

	return SRV;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void Render()
{
	// Reset the active sprite counter before any Sprite objects are made
	numActiveSprites = 0;

	// Sprite drawing test
	Sprite(L"Testsprite.png", 300, 300, 200, 200);
	Sprite(L"Testsprite.png", 500, 500, 100, 100);

	if (pD3DDevice != NULL)
	{
		// Clear the target buffer
		pD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f));

		// Start drawing the sprites
		spriteObject->Begin(D3DX10_SPRITE_SORT_TEXTURE);

		// Draw all the sprites
		spriteObject->DrawSpritesImmediate(spritePool, numActiveSprites, 0, 0);

		// Finish up and send the sprites to the hardware
		spriteObject->End();

		// Display the next item in the swap chain
		pSwapChain->Present(0, 0);
	}
}


Thanks in advance :)
Last edited on
Topic archived. No new replies allowed.