Archived

This topic is now archived and is closed to further replies.

boxdot3

2D sprite thing

Recommended Posts

I am having a problem displaying a 2d Sprite, and have no idea what I am doing wrong. Here is the code that i use to set up the sprite, and then the rendering code. I am using an orthographic camera, and i know that my device is being set up correctly. I cant seem to figure out whats wrong though...
  
BOOL CSprite::Create(LPDIRECT3DDEVICE8 dev, char * texture, int num, float width, float height)
{
	if (width < 1 || height < 1 || num < 1)
		return FALSE;
	if (dev == NULL)
		return FALSE;

	device = dev;
	numInd = num * 6;
	numVerts = num * 4;
	numSprites = num;

	Verts = new spriteVertice[numVerts];
	Indices = new WORD[numInd];

	if (texture != NULL)
	{
		if(FAILED(D3DXCreateTextureFromFile(device, texture, &tex)))
			return FALSE;
	}

	if(FAILED(device->CreateVertexBuffer(numVerts * sizeof(spriteVertice),
                                               0, SPRITE_VERTEX,
                                               D3DPOOL_DEFAULT, &vb)))
		return FALSE;

	if(FAILED(device->CreateIndexBuffer(numInd * sizeof(WORD), 
											  0, D3DFMT_INDEX16, D3DPOOL_MANAGED,
											  &ib)))
		return FALSE;

	int vCount = 0;
	for (int i = 0; i < num; i++)
	{
			Verts[vCount].color = D3DCOLOR_XRGB(255, 255, 255);
			Verts[vCount].tu = 0.0f;
			Verts[vCount].tv = 0.0f;
			Verts[vCount].x = 0.0f;
			Verts[vCount].y = 0.0f;
			Verts[vCount].z = 0.0f;

			Verts[vCount + 1].color = D3DCOLOR_XRGB(255, 255, 255);
			Verts[vCount + 1].tu = 1.0f;
			Verts[vCount + 1].tv = 0.0f;
			Verts[vCount + 1].x = width;
			Verts[vCount + 1].y = 0.0f;
			Verts[vCount + 1].z = 0.0f;

			Verts[vCount + 2].color = D3DCOLOR_XRGB(255, 255, 255);
			Verts[vCount + 2].tu = 0.0f;
			Verts[vCount + 2].tv = 1.0f;
			Verts[vCount + 2].x = 0.0f;
			Verts[vCount + 2].y = height;
			Verts[vCount + 2].z = 0.0f;

			Verts[vCount + 3].color = D3DCOLOR_XRGB(255, 255, 255);
			Verts[vCount + 3].tu = 1.0f;
			Verts[vCount + 3].tv = 1.0f;
			Verts[vCount + 3].x = width;
			Verts[vCount + 3].y = height;
			Verts[vCount + 3].z = 0.0f;

			vCount += 4;
	}

	int iCount = 0;
	int ivCount = 0;

	for (int j = 0; i < num; i++)
	{
		Indices[iCount] = 0 + ivCount;
		Indices[iCount + 1] = 1 + ivCount;
		Indices[iCount + 2] = 2 + ivCount;
		Indices[iCount + 3] = 3 + ivCount;
		Indices[iCount + 4] = 2 + ivCount;
		Indices[iCount + 5] = 1 + ivCount;
		ivCount	+=	4;
		iCount	+=	6;
	}
	VOID* pVertices;
	VOID* pIndices;

	vb->Lock(0, numVerts * sizeof(spriteVertice),(BYTE**)&pVertices, 0);
		memcpy(pVertices, Verts, sizeof(Verts));
	vb->Unlock();
	ib->Lock(0, numInd * sizeof(WORD),(BYTE**)&pIndices,0); 
		memcpy(pIndices, Indices, sizeof(Indices));
	ib->Unlock();

	return TRUE;
}




BOOL CSprite::Render()
{
	device->SetStreamSource(0, vb, sizeof(spriteVertice));
	device->SetVertexShader(SPRITE_VERTEX);
	
	device->SetIndices(ib, 0);
	device->SetTexture(0, tex);
	
	device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

	device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, numVerts, 0, numSprites);

	return TRUE;
}
  
Ideas anyone? Thanx in advance! Simon

Share this post


Link to post
Share on other sites
Couple of questions... Where are the type declarators for:


  
device = dev;
numInd = num * 6;
numVerts = num * 4;
numSprites = num;
Verts = new spriteVertice[numVerts];
Indices = new WORD[numInd];


this lot?

Secondly, can you not use the debugger to find out exactly what is going on?

Rory.

Share this post


Link to post
Share on other sites
The code is part of a class, so im setting some of the internal variables of the class up there. The

Verts = new spriteVertice[numVerts];

is to create an array of vertices at runtime.
Also, how exactly do you use DX Debug? Does it just write it in the debug window or something?

later

Simon

Share this post


Link to post
Share on other sites