์์ :)
https://github.com/eazuooz/WindowAPI/commit/47fc7c2d8eba0b94d89daa121c7de9a8168b6db4
Path Resource Load · eazuooz/WindowAPI@47fc7c2
Show file tree Showing 12 changed files with 223 additions and 50 deletions.
github.com
ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ๋ ์ฝ๊ฒ ๊ฐ์ ธ์์ ์ฌ์ฉ ํ ์ ์๋๋ก ๋์์ฃผ๋ ํด๋์ค์ด๋ค.
์์ง ๊ตฌํ์ ๋ ๋์ด์์ง๋ง ์์ผ๋ก ๊ฒฝ๋ก๊ด๋ จํ ์ฒ๋ฆฌ๋ ์ด์ชฝ ํด๋์ค๋ฅผ ์ด์ฉํด ์ฒ๋ฆฌ ํ ๊ฒ์ด๋ค.
class Paths
{
public:
Paths();
~Paths();
private:
static wchar_t mPath[256];
};
๋ฆฌ์์ค๋ฅผ ์ฐพ๊ฑฐ๋ ๋ก๋ํ๊ฑฐ๋
์ฌ๋ฌ๊ฐ์ง ๋ฆฌ์์ค๋ค์ ๊ด๋ฆฌ ํด์ฃผ๋ ํด๋์ค์ด๋ค.
์ฌ์ด๋, ์ด๋ฏธ์ง ๋ฑ๋ฑ ๋ค์ํ ๋ฆฌ์์ค ์ ํ์ ๋ก๋ํ ์ ์๋๋ก template์ ์ด์ฉํด์ ๋ง๋ค์ด ์ฃผ์๋ค.
ํ ํ๋ฆฟ์ ์ฐ์ง ์๊ณ ์ผ์ผ์ด ๋ง๋ค์ด์ฃผ๋ ๊ฒฝ์ฐ๋ ์์ง๋ง ํ์๋ ํ์ฅ๊ฐ๋ฅ์ฑ์ ๊ณ ๋ คํด์ ํ ํ๋ฆฟ์ ์ฌ์ฉํ๋๋ก ํ๊ฒ ๋ค.
class Resource;
class Image;
template <typename T>
class Resources
{
public:
static T* Find(const std::wstring& key)
{
std::map<std::wstring, Resource*>::iterator iter = mResources.find(key);
// ์ด๋ฏธ ๋์ผํ ํค๊ฐ์ผ๋ก ๋ค๋ฅธ ๋ฆฌ์์ค๊ฐ ๋จผ์ ๋ฑ๋ก๋์ด ์์๋ค.
if (iter != mResources.end())
{
return dynamic_cast<T*>(iter->second);
}
return nullptr;
}
static T* Load(const std::wstring& key, const std::wstring& path)
{
// ํค๊ฐ์ผ๋ก ํ์
T* resource = Find(key);
if (nullptr != resource)
{
// ํด๋นํค๋ก ์ด๋ฏธ ๋ก๋ฉ๋๊ฒ ์์ผ๋ฉด ํด๋น ๋ฆฌ์์ค๋ฅผ ๋ฐํ
return resource;
}
// ํด๋น ํค๋ก ๋ก๋ฉ๋ ๋ฆฌ์์ค๊ฐ ์๋ค.
resource = new T();
if (FAILED(resource->Load(path)))
{
MessageBox(nullptr, L"Image Load Failed!", L"Error", MB_OK);
return nullptr;
}
resource->SetKey(key);
resource->SetPath(path);
mResources.insert(make_pair(key, resource));
return dynamic_cast<T*>(resource);
}
private:
Resources()
{
}
~Resources()
{
Release();
}
void Release()
{
std::map<std::wstring, Resource*>::iterator iter = mResources.begin();
for (; iter != mResources.end(); ++iter)
{
delete(iter->second);
}
}
private:
static std::map<std::wstring, Resource*> mResources;
};
std::map์ key๊ฐ์ผ๋ก wstring์ผ๋ก ๊ด๋ฆฌ๋ฅผํ๊ณ ํด๋น ๋ฐ์ดํฐ๋ Resource*๋ก ๋ถ๋ชจํํ๋ก ๋ค๊ณ ์๋ค.
๋ฆฌ์์คํด๋์ค๋ Loadํจ์๋ฅผ ์์๊ฐ์ํจ์๋ก ์ ์ธํ์ฌ ํญ์ ์ด๋ฅผ ์์๋ฐ์ ์์ด๋ค์ ๊ตฌํ์ ํด์ฃผ์ด์ผํ๋ค.
class Resource : public Entity
{
public:
Resource();
virtual ~Resource();
virtual HRESULT Load(const std::wstring& path) = 0;
const std::wstring& GetKey() { return mKey; }
const std::wstring& GetPath() { return mPath; }
void SetKey(const std::wstring& key) { mKey = key; }
void SetPath(const std::wstring& path) { mPath = path; }
private:
std::wstring mKey;
std::wstring mPath;
};
class Image : public Resource
{
public:
Image();
~Image();
virtual HRESULT Load(const std::wstring& path) override;
public:
HDC GetDC() { return mHdc; }
UINT GetWidth() { return mWidth; }
UINT GetHeight() { return mHeight; }
private:
HBITMAP mHBitmap;
HDC mHdc;
UINT mWidth;
UINT mHeight;
};
์ด์ ๋ง์ง๋ง์ผ๋ก Player์ ์ด๋ฏธ์ง๋ฅผ ํด๋น ๋ฆฌ์์ค ํด๋์ค๋ฅผ ์ด์ฉํ์ฌ ๋ก๋ํด์ ์ฌ์ฉํ๋๋ก ๋ฐ๊พธ์ด ๋ณด์.
void Player::Render(HDC hdc)
{
Image* pImage = Resources<Image>::Load(L"PlayerImage", L"..\\Resources\\Images\\Fighter.bmp");
if (nullptr == pImage)
return;
Vector2 vPos = GetPos();
BitBlt(hdc, (int)vPos.x - 61, (int)vPos.y - 62, 123, 124, pImage->GetDC(), 0, 0, SRCCOPY);
}
'๐ Development Study > ๐ป Win32API' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Collider (0) | 2022.09.27 |
---|---|
Component (0) | 2022.09.26 |
Resources, Brush, Pen (0) | 2022.09.20 |
Double Buffering (0) | 2022.09.19 |
Input (0) | 2022.09.15 |
๋๊ธ