#include "IndexBuffer.h"

IndexBuffer::IndexBuffer(Graphics& gfx, const std::vector<unsigned short>& indices, Logger& logger)
	:
	count((UINT)indices.size())
{
	D3D11_BUFFER_DESC ibd = {};
	ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
	ibd.Usage = D3D11_USAGE_DEFAULT;
	ibd.CPUAccessFlags = 0u;
	ibd.MiscFlags = 0u;
	ibd.ByteWidth = UINT(count * sizeof(unsigned short));
	ibd.StructureByteStride = sizeof(unsigned short);
	D3D11_SUBRESOURCE_DATA isd = {};
	isd.pSysMem = indices.data();
	GetDevice(gfx)->CreateBuffer(&ibd, &isd, &pIndexBuffer);

	logger.PutLog("IndexBuffer::IndexBuffer", "size of unsigned short: " + std::to_string(sizeof(unsigned short)));
	logger.PutLog("IndexBuffer::IndexBuffer", "ByteWidth: " + std::to_string(ibd.ByteWidth));
	logger.PutLog("IndexBuffer::IndexBuffer", "StructureByteStride: " + std::to_string(ibd.StructureByteStride));
}

void IndexBuffer::Bind(Graphics& gfx, Logger& logger) noexcept
{
	GetContext(gfx)->IASetIndexBuffer(pIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0u);
}

UINT IndexBuffer::GetCount() const noexcept
{
	return count;
}
