4 years ago in C
void SpriteRenderer::UpdateBuffer(const GameContext& gameContext)
// 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_BufferSize < m_Sprites.size() || !m_pVertexBuffer)
//if(m_pVertexBuffer != nullptr)
// m_pVertexBuffer->Release();
SafeRelease(m_pVertexBuffer); //resolves? -
if (m_BufferSize < m_Sprites.size())
m_BufferSize = int(m_Sprites.size());
D3D11_BUFFER_DESC buffDesc;
buffDesc.Usage = D3D11_USAGE_DYNAMIC;
buffDesc.ByteWidth = sizeof(SpriteVertex) * uint32_t(m_BufferSize); //update the size | no ifs o
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!"))
//Sort Sprites
sort(m_Sprites.begin(), m_Sprites.end(), [](const SpriteVertex& v0, const SpriteVertex& v1)
return v0.TextureId < v1.TextureId;
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.data(), sizeof(SpriteVertex) * uint32_t(m_BufferSize));//m_Sprites.size());
gameContext.pDeviceContext->Unmap(m_pVertexBuffer, 0);
//m_ImmediateVertex = m_Sprites;
void SpriteComponent::Draw(const GameContext& )
if (!m_pTexture)
//TODO: Here you need to draw the SpriteComponent using the Draw of the sprite renderer
// The sprite renderer is a singleton
// you will need to position, the rotation and the scale
// You can use the QuaternionToEuler function to help you with the z rotation
//void SpriteRenderer::Draw(TextureData * pTexture, DirectX::XMFLOAT2 position, DirectX::XMFLOAT4 color,
// DirectX::XMFLOAT2 pivot, DirectX::XMFLOAT2 scale, float rotation, float depth)
, DirectX::XMFLOAT2(GetGameObject()->GetTransform()->GetPosition().x, GetGameObject()->GetTransform()->GetPosition().y)
, m_Color
, m_Pivot
, DirectX::XMFLOAT2(GetGameObject()->GetTransform()->GetScale().x, GetGameObject()->GetTransform()->GetScale().y)
, QuaternionToEuler(GetGameObject()->GetTransform()->GetRotation()).z
, GetGameObject()->GetTransform()->GetPosition().z);