Untitled
4 years ago in C
void SpriteRenderer::UpdateBuffer(const GameContext& gameContext)
{
UNREFERENCED_PARAMETER(gameContext);
//TODO:
// if the vertex buffer does not exists, or the number of sprites is bigger then the buffer size
// release the buffer
// update the buffer size (if needed)
// Create a new buffer. Make sure the Usage flag is set to Dynamic, you bind to the vertex buffer
// and you set the cpu access flags to access_write
//
// Finally create the buffer. You can use the device in the game context. Be sure to log the HResult!
if (!m_pVertexBuffer)
{
if(m_pVertexBuffer != nullptr)
m_pVertexBuffer->Release();
D3D11_BUFFER_DESC buffDesc;
buffDesc.Usage = D3D11_USAGE_DYNAMIC;
buffDesc.ByteWidth = sizeof(SpriteVertex) * m_Sprites.size(); //update the size? this updates the size or am I not getting it right?
buffDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
buffDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
buffDesc.MiscFlags = 0;
const auto hr = gameContext.pDevice->CreateBuffer(&buffDesc, nullptr, &m_pVertexBuffer);
if (Logger::LogHResult(hr, L"SpriteRenderer::UpdateBuffer > m_pVertexBuffer creation failed!"))
return;
}
//------------------------
//Sort Sprites
//SORT BY TEXTURE
sort(m_Sprites.begin(), m_Sprites.end(), [](const SpriteVertex& v0, const SpriteVertex& v1)
{
return v0.TextureId < v1.TextureId;
});
//SORT BY DEPTH
sort(m_Sprites.begin(), m_Sprites.end(), SpriteSortByDepth);
sort(m_Sprites.begin(), m_Sprites.end(), [](const SpriteVertex& v0, const SpriteVertex& v1)
{
if (v0.TextureId == v1.TextureId)
{
return v0.TransformData.z < v1.TransformData.z;
}
return false;
});
//------------------------
//TODO: Fill Buffer
// Finally fill the buffer. You will need to create a D3D11_MAPPED_SUBRESOURCE
// Next you will need to use the device context to map the vertex buffer to the mapped resource
// use memcpy to copy all our sprites to the mapped resource
// unmap the vertex buffer
if (!m_pVertexBuffer)
{
D3D11_MAPPED_SUBRESOURCE mappedResource;
gameContext.pDeviceContext->Map(m_pVertexBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource);
memcpy(mappedResource.pData, &m_Sprites, sizeof(SpriteVertex) * m_Sprites.size());
gameContext.pDeviceContext->Unmap(m_pVertexBuffer, 0);
//m_ImmediateVertex = m_Sprites;
}
}