Archived

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

Hippokrates

Why doesn`t it work this time?

Recommended Posts

I am still working on my so called sprites. I have sorted them by z order and stuffed pointers to them into an std::vector. The render function which groups them by texture, stuffs them into a VB and renders them does not display anything though. Here is as much code as I have . First adding a sprite:
bool CSpriteList::Add(LPSTR strImageFileName, DWORD dwID, int iStepSizeX, int iStepSizeY, int iStepsX, int iStepsY, int iFrameDelay, float fSpeed, CGraphics3D * pGraphics) {
	LPSPRITENODE pTemp = NULL;
	if(NULL == m_pFirst && NULL == m_pLast) {
		pTemp = new SPRITENODE;
		pTemp->pSprite = new CSprite();
		pTemp->pSprite->SetID(dwID);
		pTemp->pNext = NULL;
		m_pFirst = pTemp;
		m_pLast = pTemp;
	}else {
		pTemp = new SPRITENODE;
		pTemp->pSprite = new CSprite();
		pTemp->pSprite->SetID(dwID);
		pTemp->pNext = NULL;
		m_pLast->pNext = pTemp;
		m_pLast = pTemp;
	}
	m_iElementCount++;
	m_pTextureManager->Add(strImageFileName, pGraphics);
	pTemp->pSprite->m_iTextureIndex = m_pTextureManager->GetTextureIndex(strImageFileName);
	UpdateTextureInfo();
	m_Elements.push_back(pTemp);
	return true;
}
 
Then sorting them:
void CSpriteList::ZSort() {
	LPSPRITENODE pTemp = NULL;
	for(int i = 0; i < m_iElementCount; i++) {
		if(i < (m_iElementCount - 1)) {
			if(SAFE_CHECK(m_Elements) && SAFE_CHECK(m_Elements[i + 1])) {
				if(m_Elements[i]->pSprite->m_Position.GetZPos() < m_Elements[i + 1]->pSprite->m_Position.GetZPos()) {
					pTemp = m_Elements[i];
					m_Elements[i] = m_Elements[i + 1];
					m_Elements[i + 1] = pTemp;
				}
			}
		}
	}
}
 
I am doing just one pass, because this is done every frame anyway. And finally the rendering code:
bool CSpriteList::Render(CGraphics3D * pGraphics) {
	D3DXMATRIX Identity;
	LPSPRITENODE pTemp = NULL;
	CMaterial Material;
	int iVertexStart = 0;
	int iCurrentPosition = 0;
	int * piCurrentSprite = new int[m_pTextureManager->GetElementCount()];

	ZeroMemory(piCurrentSprite, sizeof(int) * m_pTextureManager->GetElementCount());

	ZSort();

	m_pSpriteBuffer->ShutDown();
	m_pSpriteBuffer->Init(pGraphics, (m_iElementCount - m_iDisabledSprites) * 6, SPRITE_VERTEX, sizeof(SPRITEVERTEX), D3DUSAGE_WRITEONLY);
	m_pSpriteBuffer->Lock();

	//Die Vertices sammeln
	for(int i = 0; i < m_iElementCount; i++) {
		pTemp = m_Elements[i];
		if(!(pTemp->pSprite->m_bDisabled)) {
			iCurrentPosition = (m_piSpritesTillTexture[pTemp->pSprite->m_iTextureIndex] + piCurrentSprite[pTemp->pSprite->m_iTextureIndex]) * 6 * sizeof(SPRITEVERTEX);
			pTemp->pSprite->FillVertexBuffer(&((*m_pSpriteBuffer->GetPointer())[iCurrentPosition]), pGraphics);
		}
		piCurrentSprite[pTemp->pSprite->m_iTextureIndex]++;
	}
	
	m_pSpriteBuffer->Unlock();

	pGraphics->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	pGraphics->SetRenderState(D3DRS_ZENABLE, TRUE);
	pGraphics->SetRenderState(D3DRS_LIGHTING, TRUE);
	pGraphics->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	pGraphics->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
	pGraphics->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	pGraphics->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
	pGraphics->SetRenderState(D3DRS_ALPHAREF, 0x0a);
	pGraphics->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);

	pGraphics->SetMaterial(&Material);

	D3DXMatrixIdentity(&Identity);
	pGraphics->GetDevice()->SetTransform(D3DTS_WORLD, &Identity);

	m_pTextureManager->SetCursorStart();
	for(i = 0; i < m_pTextureManager->GetElementCount(); i++) {
		pGraphics->SetTexture(0, m_pTextureManager->GetCurrent()->pTexture);
		m_pSpriteBuffer->Render(iVertexStart, m_piSpritesPerTexture[i] * 2, D3DPT_TRIANGLELIST);
		iVertexStart += m_piSpritesPerTexture[i] * 6;
		m_pTextureManager->StepForward();
	}

	pGraphics->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
	pGraphics->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
	pGraphics->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

	pGraphics->GetDevice()->SetTexture(0, NULL);
	SAFE_DELETEV(piCurrentSprite);
	return true;
}
 
And not to forget the function that fills a sprite`s vertices into a VB:
void CSprite::FillVertexBuffer(BYTE * pBuffer, CGraphics3D * pGraphics) {
	D3DXVECTOR3 Position, Normal;
	SPRITEVERTEX pVertices[6];

	m_Position.Update(pGraphics);
	memcpy(pVertices, m_Vertices, 6 * sizeof(SPRITEVERTEX));
	for(int i = 0; i < 6; i++) {
		Position.x = pVertices[i].x;
		Position.y = pVertices[i].y;
		Position.z = pVertices[i].z;
		Normal.x = pVertices[i].nx;
		Normal.y = pVertices[i].ny;
		Normal.z = pVertices[i].nz;
		MultiplyMatrixAndVector(&Position, &Position, m_Position.GetMatrix(pGraphics));
		MultiplyMatrixAndVector(&Normal, &Normal, m_Position.GetMatrix(pGraphics));
		pVertices[i].x = Position.x;
		pVertices[i].y = Position.y;
		pVertices[i].z = Position.z;
		pVertices[i].nx = Normal.x;
		pVertices[i].ny = Normal.y;
		pVertices[i].nz = Normal.z;
		pVertices[i].color = D3DCOLOR_RGBA(255, 255, 255, m_byAlpha);
	}
	pVertices[0].tu = m_iCurrentStepX * m_fStepSizeX;
	pVertices[0].tv = m_iCurrentStepY * m_fStepSizeY + m_fStepSizeY;
	pVertices[1].tu = m_iCurrentStepX * m_fStepSizeX;
	pVertices[1].tv = m_iCurrentStepY * m_fStepSizeY;
	pVertices[2].tu = m_iCurrentStepX * m_fStepSizeX + m_fStepSizeX;
	pVertices[2].tv = m_iCurrentStepY * m_fStepSizeY + m_fStepSizeY;
	pVertices[3].tu = pVertices[2].tu;
	pVertices[3].tv = pVertices[2].tv;
	pVertices[4].tu = pVertices[1].tu;
	pVertices[4].tv = pVertices[1].tv;
	pVertices[5].tu = m_iCurrentStepX * m_fStepSizeX + m_fStepSizeX;
	pVertices[5].tv = m_iCurrentStepY * m_fStepSizeY;
	memcpy(pBuffer, pVertices, sizeof(SPRITEVERTEX) * 6);
}
 

Share this post


Link to post
Share on other sites