// Sprite.cpp: implementation of the CSprite class. // ////////////////////////////////////////////////////////////////////// #include #include "Sprite.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CSprite::CSprite() { m_bShow = TRUE; m_bHorizon = TRUE; // m_lpVB = NULL; m_fRate = 1.0f; } CSprite::~CSprite() { // if(m_lpVB) { m_lpVB->Release(); m_lpVB = NULL; } } BOOL CSprite::Create(long nPutX, long nPutY, long nTexX1, long nTexY1, long nTexX2, long nTexY2, CTexture *lpTexture) { /* if(m_lpVB) { m_lpVB->Release(); m_lpVB = NULL; } if(FAILED(lpD3DDevice->CreateVertexBuffer(4 * sizeof(TLVertex), 0, TLVERTEXFVF, D3DPOOL_DEFAULT, &m_lpVB))) { return FALSE; } TLVertex *pVertices; if(FAILED(lpD3DDevice->Lock(0, 4 * sizeof(TLVertex), 0, (unsigned char **)&pVertex, 0))) { return FALSE; }*/ m_nPutX = nPutX; m_nPutY = nPutY; m_rcRender.left = nTexX1; m_rcRender.top = nTexY1; m_rcRender.right = nTexX2; m_rcRender.bottom = nTexY2; m_lpTexture = lpTexture; return TRUE; } void CSprite::Render(LPDIRECT3DDEVICE8 lpD3DDevice, unsigned char Alpha, long fx, long fy) { if(!m_bShow) return; TLVertex pVertices[4]; pVertices[0].Diffuse.c = pVertices[1].Diffuse.c = pVertices[2].Diffuse.c = pVertices[3].Diffuse.c = 0xFFFFFFFF; pVertices[0].Diffuse.a = pVertices[1].Diffuse.a = pVertices[2].Diffuse.a = pVertices[3].Diffuse.a = Alpha; pVertices[0].Specular.c = pVertices[1].Specular.c = pVertices[2].Specular.c = pVertices[3].Specular.c = 0; pVertices[0].w = pVertices[1].w = pVertices[2].w = pVertices[3].w = 0.1f; pVertices[0].v.z = pVertices[1].v.z = pVertices[2].v.z = pVertices[3].v.z = 0.1f; if(m_bHorizon) { pVertices[0].v.x = pVertices[1].v.x = m_nPutX + fx - 0.5f; pVertices[2].v.x = pVertices[3].v.x = m_nPutX + fx + ((m_rcRender.right - m_rcRender.left) * m_fRate) - 0.5f; pVertices[0].v.y = pVertices[2].v.y = m_nPutY + fy - 0.5f; pVertices[1].v.y = pVertices[3].v.y = m_nPutY + fy + (m_rcRender.bottom - m_rcRender.top) - 0.5f; pVertices[0].tu = pVertices[1].tu = (float)m_rcRender.left / m_lpTexture->m_dwWidth; pVertices[2].tu = pVertices[3].tu = (float)(m_rcRender.left + ((m_rcRender.right - m_rcRender.left) * m_fRate)) / m_lpTexture->m_dwWidth; pVertices[0].tv = pVertices[2].tv = (float)m_rcRender.top / m_lpTexture->m_dwHeight; pVertices[1].tv = pVertices[3].tv = (float)m_rcRender.bottom / m_lpTexture->m_dwHeight; } else { pVertices[0].v.x = pVertices[1].v.x = m_nPutX + fx - 0.5f; pVertices[2].v.x = pVertices[3].v.x = m_nPutX + fx + (m_rcRender.right - m_rcRender.left) - 0.5f; pVertices[0].v.y = pVertices[2].v.y = m_nPutY + fy + ((m_rcRender.bottom - m_rcRender.top) * (1.0f - m_fRate)) - 0.5f; pVertices[1].v.y = pVertices[3].v.y = m_nPutY + fy + (m_rcRender.bottom - m_rcRender.top) - 0.5f; pVertices[0].tu = pVertices[1].tu = (float)m_rcRender.left / m_lpTexture->m_dwWidth; pVertices[2].tu = pVertices[3].tu = (float)m_rcRender.right / m_lpTexture->m_dwWidth; pVertices[0].tv = pVertices[2].tv = (float)(m_rcRender.top + ((m_rcRender.bottom - m_rcRender.top) * (1.0f - m_fRate))) / m_lpTexture->m_dwHeight; pVertices[1].tv = pVertices[3].tv = (float)m_rcRender.bottom / m_lpTexture->m_dwHeight; } lpD3DDevice->SetVertexShader(TLVERTEXFVF); lpD3DDevice->SetTexture(0, m_lpTexture->GetTexture()); lpD3DDevice->SetTexture(1, NULL); lpD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, pVertices, sizeof(TLVertex)); } void CSprite::Render(LPDIRECT3DDEVICE8 lpD3DDevice, float fDirection, unsigned char Alpha) { if(!m_bShow) return; TLVertex pVertices[4]; pVertices[0].Diffuse.c = pVertices[1].Diffuse.c = pVertices[2].Diffuse.c = pVertices[3].Diffuse.c = 0xFFFFFFFF; pVertices[0].Diffuse.a = pVertices[1].Diffuse.a = pVertices[2].Diffuse.a = pVertices[3].Diffuse.a = Alpha; pVertices[0].Specular.c = pVertices[1].Specular.c = pVertices[2].Specular.c = pVertices[3].Specular.c = 0; pVertices[0].w = pVertices[1].w = pVertices[2].w = pVertices[3].w = 0.1f; pVertices[0].v.z = pVertices[1].v.z = pVertices[2].v.z = pVertices[3].v.z = 0.1f; int CenterX = m_nPutX + (m_rcRender.right - m_rcRender.left) / 2.0f; int CenterY = m_nPutY + (m_rcRender.bottom - m_rcRender.top) / 2.0f; vector3 vecRot[4]; vecRot[0].x = vecRot[1].x = m_nPutX - CenterX; vecRot[2].x = vecRot[3].x = m_nPutX - CenterX + (m_rcRender.right - m_rcRender.left); vecRot[0].y = vecRot[2].y = m_nPutY - CenterY; vecRot[1].y = vecRot[3].y = m_nPutY - CenterY + (m_rcRender.bottom - m_rcRender.top); pVertices[0].v.x = vecRot[0].x * cosf(fDirection) - vecRot[0].y * sinf(fDirection); pVertices[0].v.y = vecRot[0].x * sinf(fDirection) + vecRot[0].y * cosf(fDirection); pVertices[1].v.x = vecRot[1].x * cosf(fDirection) - vecRot[1].y * sinf(fDirection); pVertices[1].v.y = vecRot[1].x * sinf(fDirection) + vecRot[1].y * cosf(fDirection); pVertices[2].v.x = vecRot[2].x * cosf(fDirection) - vecRot[2].y * sinf(fDirection); pVertices[2].v.y = vecRot[2].x * sinf(fDirection) + vecRot[2].y * cosf(fDirection); pVertices[3].v.x = vecRot[3].x * cosf(fDirection) - vecRot[3].y * sinf(fDirection); pVertices[3].v.y = vecRot[3].x * sinf(fDirection) + vecRot[3].y * cosf(fDirection); pVertices[0].v.x += CenterX - 0.5f; pVertices[0].v.y += CenterY - 0.5f; pVertices[1].v.x += CenterX - 0.5f; pVertices[1].v.y += CenterY - 0.5f; pVertices[2].v.x += CenterX - 0.5f; pVertices[2].v.y += CenterY - 0.5f; pVertices[3].v.x += CenterX - 0.5f; pVertices[3].v.y += CenterY - 0.5f; pVertices[0].tu = pVertices[1].tu = (float)m_rcRender.left / m_lpTexture->m_dwWidth; pVertices[2].tu = pVertices[3].tu = (float)m_rcRender.right / m_lpTexture->m_dwWidth; pVertices[0].tv = pVertices[2].tv = (float)m_rcRender.top / m_lpTexture->m_dwHeight; pVertices[1].tv = pVertices[3].tv = (float)m_rcRender.bottom / m_lpTexture->m_dwHeight; lpD3DDevice->SetVertexShader(TLVERTEXFVF); lpD3DDevice->SetTexture(0, m_lpTexture->GetTexture()); lpD3DDevice->SetTexture(1, NULL); lpD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, pVertices, sizeof(TLVertex)); }