๋ฆฌ์์ค ๋งค๋์ ๋ฅผ ๋ง๋ค์ด๋ณด์.
// Resource.h
#pragma once
class Texture;
class ResManager {
SINGLE(ResManager);
private:
map<wstring, Texture*> mapTex;
public:
Texture* LoadTexture(const wstring& _key, const wstring& _path);
Texture* FindTexture(const wstring& _key);
};
// Resource.cpp
#include "pch.h"
#include "ResManager.h"
#include "PathManager.h"
#include "Texture.h"
ResManager::ResManager()
{
}
ResManager::~ResManager()
{
map<wstring, Texture*>::iterator iter = mapTex.begin();
for (; iter != mapTex.end(); ++iter) {
delete iter->second;
}
}
Texture* ResManager::LoadTexture(const wstring& _key, const wstring& _path)
{
Texture* tex = FindTexture(_key);
if (nullptr != tex)
return tex;
wstring strPath = PathManager::Instance()->GetContentPath();
strPath += _path;
tex = new Texture;
tex->Load(strPath);
tex->SetKey(_key);
tex->SetRelativePath(_path);
mapTex.insert(make_pair(_key, tex));
return tex;
}
Texture* ResManager::FindTexture(const wstring& _key)
{
map<wstring, Texture*>::iterator iter = mapTex.find(_key);
if (iter == mapTex.end()) {
return nullptr;
}
return iter->second;
}
// pch.h
#include <map>
using std::map;
using std::make_pair;
๋ฆฌ์์ค๋ ํค์ ๊ฒฝ๋ก๋ฅผ ์ง์ผ๋ก ์ ์ฅํ๊ธฐ ์ํด map์ ์ฌ์ฉํ๋๋ก ํ๋ค. map์ ์ค๋ณต์ ํ์ฉํ์ง ์๊ณ first๋ก ํค๋ฅผ, second๋ก ๊ฒฝ๋ก๋ฅผ ์ ๊ทผํ ์ ์๋ค. map์ ๋ํ ์ง์์ด ํ์ํจ์ผ๋ก ์์ธํ ๊ฒ์ ์๋์ ํฌ์คํ ์ ์ฐธ๊ณ ํ๋๋ก ํ๋ค.
https://life-with-coding.tistory.com/305
[C++][STL] map ์ฌ์ฉ๋ฒ ์ ๋ฆฌ
์ธํธ๋ก ์๋ ํ์ธ์. ์ค๋์ C++ STL ์ฐ๊ด ์ปจํ ์ด๋ ์ค ํ๋์ธ map์ ๋ํด ์๋ ค๋๋ฆฌ๊ฒ ์ต๋๋ค. ๋ชฉ์ฐจ 1) Map์ด๋? 2) Map ๊ธฐ๋ณธ ํํ 3) Map ์ ๋ ฌ 4) Map ์ฌ์ฉ๋ฐฉ๋ฒ - ํค๋ ํฌํจ - map ์ ์ธ - search : map์์ ๋ฐ์ดํฐ
life-with-coding.tistory.com
๋ฆฌ์์ค ๋งค๋์ ๋ก ํ์ฌ๊ธ ํ ์ค์ฒ๋ฅผ ํ๋ฒ๋ง ๋ก๋ฉํ๊ณ (๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฌ๊ณ ) ๊ทธ ํ๋ก ๋์ผํ ํ ์ค์ฒ๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ์ ํค ๊ฐ์ผ๋ก ๋ถ๋ฌ์ค๋ ํํ๋ก ์ฌ์ฉํ๋ค.
๋ฆฌ์์ค ๋ก๋ ํจ์๋ map์ ๊ธฐ์กด ๋์ผํ ๋ฆฌ์์ค๊ฐ ์ด๋ฏธ ์๋์ง๋ฅผ ํ์ธํ๊ณ , ์๋ค๋ฉด ํค ๊ฐ๊ณผ ์๋ ๊ฒฝ๋ก๋ฅผ ๋ฐ์ map์ ์ฝ์ ํ๋ค. ์ฝ์ ์ ์๋ ๋ฆฌ์์ค class๋ฅผ ๊ตฌํํ ๋ ๊ณ ์ ID์ ์๋ ๊ฒฝ๋ก๊ฐ ํ์ํ๋ค๊ณ ํ์์์ผ๋ก ์ค์ ํ๋ ๋ถ๋ถ๋ ํ์ํ๋ค.
map์ ์ด๋ฏธ ์๋ ๋ฆฌ์์ค์ธ์ง๋ฅผ ํ์ธํ๊ธฐ ์ํด์๋ find ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋๋ฐ iterator๋ก ์ ๊ทผํ๊ธฐ์ ์๋ ์ฒ๋ผ ์ฝ๋ฉํด์ผ ํ๋ค.
๊ทธ๋ฆฌ๊ณ ์์ง๋ง๊ณ ์๋ฉธ์์์ ๋ง๋ ๊ฒ๋ค์ ํด์ ํ๋ ์์ ์ ํด์ผ ํ๋ค.
๋ง๋ค์ด์ง ๋ฆฌ์์ค ๋งค๋์ ๋ฅผ ํตํด ์ ์ ๊ตฌํํ๋ ๊ฒ์ ์์ ํด๋ณด์.
// Player.cpp ์์
#include "ResManager.h"
#include "Texture.h"
Player::Player()
: tex(nullptr)
{
// Texture Loading
tex = ResManager::Instance()->LoadTexture(L"PlayerTex", L"Texture\\Player.bmp");
}
Player::~Player()
{
}
// Texture.h ์์
class Texture : public Res {
private:
HDC dc;
HBITMAP bit;
BITMAP info;
private: // private ์ฒ๋ฆฌ
Texture();
~Texture();
public:
void Load(const wstring& _str);
UINT Width() { return info.bmWidth; }
UINT Height() { return info.bmHeight; }
HDC GetDC() { return dc; }
friend class ResManager; // ๋ฆฌ์์ค ๋งค๋์ ๋ง ํ
์ค์ฒ ์์ฑ์ด ๊ฐ๋ฅํ๋๋ก ์ ํ
};
๋์ ์ผ๋ก ์์ฑ๋๋ ์ฝ๋๊ฐ ๋ง์์ง๋ ์ด์ ์ํ ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ๋์ง๋ฅผ ์ฒดํฌํ๊ธฐ ์ํด WinMain ํจ์ ๋งจ ์์ ์๋์ ์ฝ๋๋ฅผ ์ฝ์ ํ๋๋ก ํ๋ค.
// ๋ฉ๋ชจ๋ฆฌ ๋ฆญ(๋์) ์ฒดํฌ
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
// _CrtSetBreakAlloc();
๋ง์ฝ ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ์ ๊ฒฝ์ฐ F5๋ก ์คํํ๊ณ ์ข ๋ฃํ๋ฉด ์๋์ฒ๋ผ ์ถ๋ ฅ์ฐฝ์ ๋ฌ๋ค.
์ด๋ ์์ ์ซ์๋ฅผ ์ฃผ์ ์ฒ๋ฆฌ ๋ _CrtSetBreakAllocํจ์์ ์ธ์๋ก ๋ฃ๋๋ค๋ฉด ๊ทธ ๋ถ๋ถ์ ์ค๋จ์ ์ด ๊ฑธ๋ฆฐ๋ค. ์ค๋จ์ ๊ฑธ๋ฆฐ ํ ํธ์ถ ์คํ์ ํ๋ณด๋ฉด ์ด๋ ๋ถ๋ถ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋์ง ์ ์ ์๋ค.
์ด๋ฒ์ฃผ ์ฝ๋
https://github.com/MiyeongEom/GameBasic/commit/525777f2091c07353218d34a1911a9f101e07ed1
~Resource Manager · MiyeongEom/GameBasic@525777f
MiyeongEom committed Jun 6, 2024
github.com
'๐ค Study > winAPI' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[winAPI 13] Collider(2) (1) | 2024.06.08 |
---|---|
[winAPI 12] Collider(1) (0) | 2024.06.06 |
[winAPI 10] Resource & Path Manager (1) | 2024.06.06 |
[winAPI 09] ๊ธฐ์ด ์ํ (0) | 2024.06.05 |
[winAPI 08] Object (0) | 2024.06.05 |