๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ Development Study/๐Ÿ’ป Win32API

Path, Resource Load

by eazuooz 2022. 9. 22.

์˜ˆ์ œ :)

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

๋Œ“๊ธ€