This uses DirectXTex lib, so you must link that into your project. Can be found on GitHub
This function loads a sprite sheet of the .jpg format. I am currently working on handling other file formats
Code: Select all
bool SpriteSheet::LoadSpriteSheet(string filename)
{
if (!DXStringHandler::DXDoesFileExist(filename))
return false;
HRESULT result;
wstring fName;
DXStringHandler::DXConvertFromStrToWStr(filename, fName);
// Load the texture in.
TexMetadata imageMetadata;
ScratchImage* pScratchImage = new ScratchImage();
result = LoadFromWICFile((WCHAR*)fName.data(), WIC_FLAGS_NONE, &imageMetadata, *pScratchImage);
if (FAILED(result))
{
return false;
}
D3D11_TEXTURE2D_DESC textureDesc;
HRESULT hResult;
unsigned int rowPitch;
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
// Setup the description of the texture.
//m_clipWidth and m_clipHeight set during initialization of class
textureDesc.Height = m_clipHeight;
textureDesc.Width = m_clipWidth;
textureDesc.MipLevels = 0;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
// get how many images will be extracted from source
size_t rows = imageMetadata.height / m_clipHeight;
size_t cols = imageMetadata.width / m_clipWidth;
// std::vector of ID3D11ShaderResourceView
m_textures.resize(rows * cols);
// pointer to hold read data
uint8_t* destDataPtr = 0;
// main sprite sheet raw data
uint8_t* srcDataPtr = pScratchImage->GetPixels();
// used to create a texture
ID3D11Texture2D* m_Tex;
int width = m_clipWidth;
int height = m_clipHeight;
// size of destDataPtr array
size_t size = width * height * 4;
// index of ID3D11_srv
int texCount;
//counters
int i, j, srcIndex, index;
index = 0;
srcIndex = 0;
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
// reset destDataPtr index
index = 0;
// new array to hold data
destDataPtr = new uint8_t[size];
// index for texture container
texCount = y * cols + x;
for (j = y * height; j < (y * height) + height; j++)
{
for (i = x * width; i <(x * width) + width; i++)
{
// where in the source data are we
srcIndex = (j * (width * cols) + i) * 4;
//** NOTE *** some data files (.TGA ect) reads in upside down
// from bottom to top, this is top to bottom
destDataPtr[index + 0] = srcDataPtr[srcIndex + 0]; // Red.
destDataPtr[index + 1] = srcDataPtr[srcIndex + 1]; // Green.
destDataPtr[index + 2] = srcDataPtr[srcIndex + 2]; // Blue
destDataPtr[index + 3] = srcDataPtr[srcIndex + 3]; // Alpha
// index to next set of 4
index += 4;
}
}
// create the new texture to hold data
m_Tex = nullptr;
result = m_pDevice->CreateTexture2D(&textureDesc, NULL, &m_Tex);
if (FAILED(result))
{
delete pScratchImage;
delete destDataPtr;
return false;
}
// set pitch of row
rowPitch = (width * 4) * sizeof(unsigned char);
// Copy the image data into the texture.
m_pContext->UpdateSubresource(m_Tex, 0, NULL, destDataPtr, rowPitch, 0);
// release memory
delete[] destDataPtr;
// Setup the shader resource view description.
srvDesc.Format = textureDesc.Format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = -1;
// Create the shader resource view for the texture.
result = m_pDevice->CreateShaderResourceView(m_Tex, &srvDesc, &m_textures[texCount]);
if (FAILED(result))
{
delete pScratchImage;
return false;
}
m_pContext->GenerateMips(m_textures[texCount]);
if(m_Tex)
m_Tex->Release();
}
}
// clean up
delete pScratchImage;
return true;
};