Original Post
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...
Ideas anyone?
Thanx in advance!
Simon
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;
}