League Of Legends - Link Select [winAPI 11] Resource Manager
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿค“ Study/winAPI

[winAPI 11] Resource Manager

by GAMEMING 2024. 6. 6.
728x90

 

๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

 

// 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๋กœ ์‹คํ–‰ํ•˜๊ณ  ์ข…๋ฃŒํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์ถœ๋ ฅ์ฐฝ์— ๋œฌ๋‹ค.

 

๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ๋ฐœ์ƒ ( new int )
4๋ฐ”์ดํŠธ ๋ฐœ์ƒํ–ˆ๋‹ค.

 

 

์ด๋•Œ ์•ž์˜ ์ˆซ์ž๋ฅผ ์ฃผ์„ ์ฒ˜๋ฆฌ ๋œ  _CrtSetBreakAllocํ•จ์ˆ˜์— ์ธ์ž๋กœ ๋„ฃ๋Š”๋‹ค๋ฉด ๊ทธ ๋ถ€๋ถ„์— ์ค‘๋‹จ์ ์ด ๊ฑธ๋ฆฐ๋‹ค. ์ค‘๋‹จ์  ๊ฑธ๋ฆฐ ํ›„ ํ˜ธ์ถœ ์Šคํƒ์„ ํŒŒ๋ณด๋ฉด ์–ด๋”” ๋ถ€๋ถ„์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

_CrtSetBreakAlloc(217); ์ค‘๋‹จ์ 

 

 

์ด๋ฒˆ์ฃผ ์ฝ”๋“œ

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