365 lines
6.7 KiB
C++
365 lines
6.7 KiB
C++
#include "..\enodes.h"
|
||
#include "..\Win\winbase.h"
|
||
#include <map>
|
||
|
||
|
||
struct ResKey
|
||
{
|
||
ResKey() { resNameHash = 0; resTypeHash = 0; }
|
||
|
||
bool operator < (ResKey const& key) const
|
||
{
|
||
if (resNameHash > key.resNameHash)
|
||
return true;
|
||
else if (resNameHash == key.resNameHash)
|
||
return resTypeHash > key.resTypeHash;
|
||
else
|
||
return false;
|
||
}
|
||
|
||
size_t resNameHash;
|
||
size_t resTypeHash;
|
||
};
|
||
|
||
static std::map<size_t, ID2D1Bitmap*> s_mBitmapsFromFile;
|
||
static std::map<ResKey, ID2D1Bitmap*> s_mBitmapsFromResource;
|
||
|
||
|
||
e2d::ETexture::ETexture()
|
||
: m_pBitmap(nullptr)
|
||
{
|
||
}
|
||
|
||
e2d::ETexture::ETexture(const EString & fileName)
|
||
{
|
||
this->loadFromFile(fileName);
|
||
}
|
||
|
||
e2d::ETexture::ETexture(LPCTSTR resourceName, LPCTSTR resourceType)
|
||
{
|
||
this->loadFromResource(resourceName, resourceType);
|
||
}
|
||
|
||
e2d::ETexture::~ETexture()
|
||
{
|
||
}
|
||
|
||
void e2d::ETexture::loadFromFile(const EString & fileName)
|
||
{
|
||
WARN_IF(fileName.empty(), "ETexture cannot load bitmap from NULL file name.");
|
||
|
||
if (fileName.empty())
|
||
return;
|
||
|
||
if (!e2d::ETexture::preload(fileName))
|
||
{
|
||
WARN_IF(true, "Load ETexture from file failed!");
|
||
return;
|
||
}
|
||
|
||
std::hash<e2d::EString> h;
|
||
size_t hash = h(fileName);
|
||
|
||
m_pBitmap = s_mBitmapsFromFile.at(hash);
|
||
}
|
||
|
||
void e2d::ETexture::loadFromResource(LPCTSTR resourceName, LPCTSTR resourceType)
|
||
{
|
||
WARN_IF(!resourceName || !resourceType, "ETexture cannot load bitmap from NULL resource.");
|
||
|
||
if (!resourceName || !resourceType)
|
||
return;
|
||
|
||
if (!e2d::ETexture::preload(resourceName, resourceType))
|
||
{
|
||
WARN_IF(true, "Load ETexture from resource failed!");
|
||
return;
|
||
}
|
||
|
||
ResKey key;
|
||
std::hash<LPCTSTR> h;
|
||
key.resNameHash = h(resourceName);
|
||
key.resTypeHash = h(resourceType);
|
||
|
||
m_pBitmap = s_mBitmapsFromResource.at(key);
|
||
}
|
||
|
||
float e2d::ETexture::getSourceWidth() const
|
||
{
|
||
if (m_pBitmap)
|
||
{
|
||
return m_pBitmap->GetSize().width;
|
||
}
|
||
else
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
float e2d::ETexture::getSourceHeight() const
|
||
{
|
||
if (m_pBitmap)
|
||
{
|
||
return m_pBitmap->GetSize().height;
|
||
}
|
||
else
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
e2d::ESize e2d::ETexture::getSourceSize() const
|
||
{
|
||
if (m_pBitmap)
|
||
{
|
||
return ESize(getSourceWidth(), getSourceHeight());
|
||
}
|
||
else
|
||
{
|
||
return ESize();
|
||
}
|
||
}
|
||
|
||
bool e2d::ETexture::preload(const EString & fileName)
|
||
{
|
||
std::hash<e2d::EString> h;
|
||
size_t hash = h(fileName);
|
||
|
||
if (s_mBitmapsFromFile.find(hash) != s_mBitmapsFromFile.end())
|
||
{
|
||
return true;
|
||
}
|
||
|
||
HRESULT hr = S_OK;
|
||
|
||
IWICBitmapDecoder *pDecoder = nullptr;
|
||
IWICBitmapFrameDecode *pSource = nullptr;
|
||
IWICStream *pStream = nullptr;
|
||
IWICFormatConverter *pConverter = nullptr;
|
||
ID2D1Bitmap *pBitmap = nullptr;
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
hr = GetImagingFactory()->CreateDecoderFromFilename(
|
||
fileName.c_str(),
|
||
NULL,
|
||
GENERIC_READ,
|
||
WICDecodeMetadataCacheOnLoad,
|
||
&pDecoder
|
||
);
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
hr = pDecoder->GetFrame(0, &pSource);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD>
|
||
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
|
||
hr = GetImagingFactory()->CreateFormatConverter(&pConverter);
|
||
}
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD> 32bbpPBGRA
|
||
hr = pConverter->Initialize(
|
||
pSource,
|
||
GUID_WICPixelFormat32bppPBGRA,
|
||
WICBitmapDitherTypeNone,
|
||
NULL,
|
||
0.f,
|
||
WICBitmapPaletteTypeMedianCut
|
||
);
|
||
}
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><> WIC λͼ<CEBB><CDBC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Direct2D λͼ
|
||
hr = GetRenderTarget()->CreateBitmapFromWicBitmap(
|
||
pConverter,
|
||
NULL,
|
||
&pBitmap
|
||
);
|
||
}
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><><EFBFBD><EFBFBD>ͼƬָ<C6AC><D6B8><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC> Hash <20><>
|
||
std::hash<e2d::EString> h;
|
||
size_t hash = h(fileName);
|
||
|
||
s_mBitmapsFromFile.insert(
|
||
std::map<size_t, ID2D1Bitmap*>::value_type(
|
||
hash,
|
||
pBitmap)
|
||
);
|
||
}
|
||
|
||
// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
||
SafeReleaseInterface(&pDecoder);
|
||
SafeReleaseInterface(&pSource);
|
||
SafeReleaseInterface(&pStream);
|
||
SafeReleaseInterface(&pConverter);
|
||
|
||
return SUCCEEDED(hr);
|
||
}
|
||
|
||
bool e2d::ETexture::preload(LPCTSTR resourceName, LPCTSTR resourceType)
|
||
{
|
||
std::hash<LPCTSTR> h;
|
||
|
||
ResKey key;
|
||
key.resNameHash = h(resourceName);
|
||
key.resTypeHash = h(resourceType);
|
||
|
||
if (s_mBitmapsFromResource.find(key) != s_mBitmapsFromResource.end())
|
||
{
|
||
return true;
|
||
}
|
||
|
||
HRESULT hr = S_OK;
|
||
|
||
IWICBitmapDecoder *pDecoder = nullptr;
|
||
IWICBitmapFrameDecode *pSource = nullptr;
|
||
IWICStream *pStream = nullptr;
|
||
IWICFormatConverter *pConverter = nullptr;
|
||
IWICBitmapScaler *pScaler = nullptr;
|
||
ID2D1Bitmap *pBitmap = nullptr;
|
||
|
||
HRSRC imageResHandle = nullptr;
|
||
HGLOBAL imageResDataHandle = nullptr;
|
||
void *pImageFile = nullptr;
|
||
DWORD imageFileSize = 0;
|
||
|
||
// <20><>λ<EFBFBD><CEBB>Դ
|
||
imageResHandle = ::FindResourceW(HINST_THISCOMPONENT, resourceName, resourceType);
|
||
|
||
hr = imageResHandle ? S_OK : E_FAIL;
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
||
imageResDataHandle = ::LoadResource(HINST_THISCOMPONENT, imageResHandle);
|
||
|
||
hr = imageResDataHandle ? S_OK : E_FAIL;
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><>ȡ<EFBFBD>ļ<EFBFBD>ָ<EFBFBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
||
pImageFile = ::LockResource(imageResDataHandle);
|
||
|
||
hr = pImageFile ? S_OK : E_FAIL;
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
|
||
imageFileSize = SizeofResource(HINST_THISCOMPONENT, imageResHandle);
|
||
|
||
hr = imageFileSize ? S_OK : E_FAIL;
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><><EFBFBD><EFBFBD> WIC <20><>
|
||
hr = GetImagingFactory()->CreateStream(&pStream);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
||
hr = pStream->InitializeFromMemory(
|
||
reinterpret_cast<BYTE*>(pImageFile),
|
||
imageFileSize
|
||
);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>
|
||
hr = GetImagingFactory()->CreateDecoderFromStream(
|
||
pStream,
|
||
NULL,
|
||
WICDecodeMetadataCacheOnLoad,
|
||
&pDecoder
|
||
);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
hr = pDecoder->GetFrame(0, &pSource);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD>
|
||
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
|
||
hr = GetImagingFactory()->CreateFormatConverter(&pConverter);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD> 32bppPBGRA
|
||
hr = pConverter->Initialize(
|
||
pSource,
|
||
GUID_WICPixelFormat32bppPBGRA,
|
||
WICBitmapDitherTypeNone,
|
||
NULL,
|
||
0.f,
|
||
WICBitmapPaletteTypeMedianCut
|
||
);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><> WIC λͼ<CEBB><CDBC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Direct2D λͼ
|
||
hr = GetRenderTarget()->CreateBitmapFromWicBitmap(
|
||
pConverter,
|
||
NULL,
|
||
&pBitmap
|
||
);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
std::hash<LPCTSTR> h;
|
||
|
||
ResKey key;
|
||
key.resNameHash = h(resourceName);
|
||
key.resTypeHash = h(resourceType);
|
||
|
||
s_mBitmapsFromResource.insert(
|
||
std::map<ResKey, ID2D1Bitmap*>::value_type(
|
||
key,
|
||
pBitmap)
|
||
);
|
||
}
|
||
|
||
// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
||
SafeReleaseInterface(&pDecoder);
|
||
SafeReleaseInterface(&pSource);
|
||
SafeReleaseInterface(&pStream);
|
||
SafeReleaseInterface(&pConverter);
|
||
SafeReleaseInterface(&pScaler);
|
||
|
||
return SUCCEEDED(hr);
|
||
}
|
||
|
||
void e2d::ETexture::clearCache()
|
||
{
|
||
for (auto child : s_mBitmapsFromFile)
|
||
{
|
||
SafeReleaseInterface(&child.second);
|
||
}
|
||
for (auto child : s_mBitmapsFromResource)
|
||
{
|
||
SafeReleaseInterface(&child.second);
|
||
}
|
||
s_mBitmapsFromFile.clear();
|
||
s_mBitmapsFromResource.clear();
|
||
}
|
||
|
||
ID2D1Bitmap * e2d::ETexture::_getBitmap()
|
||
{
|
||
return m_pBitmap;
|
||
}
|