게임 엔진/Window API

[Window API] Resource

Henzee 2025. 2. 18. 00:34

 

게임 프로그래머 입문 올인원 강의 수강 후 복습용으로 작성

 

 

 

1. EditScene : 사용자가 화면에 그린 그림을 리소스로 저장

1) 사용자가 그린 선을 담아둘 vector 추가

2) 마우스 왼쪽 버튼과 오른쪽 버튼을 사용해서 선을 그리는 로직 추가

3) 그린 선을 화면에 렌더링하는 로직 추가

4) 그림을 리소스 파일로 저장 : 파일 스트림 방식

5) 저장한 리소스 파일을 로드

// EditScene.h

private:
	vector<pair<POINT, POINT>> _lines;

	bool _setOrigin = true;
	POINT _lastPos = {};
    
    
// EditScene.cpp

#include <fstream>

void EditScene::Update()
{
	if (GET_SINGLE(InputManager)->GetButtonDown(KeyType::LeftMouse))
	{
		POINT mousePos = GET_SINGLE(InputManager)->GetMousePos();

		if (_setOrigin)
		{
			_lastPos = mousePos;
			_setOrigin = false;
		}
		else
		{
			_lines.push_back(make_pair(_lastPos, mousePos));
			_lastPos = mousePos;
		}
	}

	if (GET_SINGLE(InputManager)->GetButtonDown(KeyType::RightMouse))
		_setOrigin = true;

	// Save
	if (GET_SINGLE(InputManager)->GetButtonDown(KeyType::S))
	{
		wofstream file;
		file.open(L"Unit.txt"); // 파일 이름 저장

		int32 minX = INT32_MAX;
		int32 maxX = INT32_MIN;
		int32 minY = INT32_MAX;
		int32 maxY = INT32_MIN;

		for (auto& line : _lines)
		{
			POINT from = line.first;
			POINT to = line.second;

			minX = min(min(minX, from.x), to.x);
			maxX = max(max(maxX, from.x), to.x);
			minY = min(min(minY, from.y), to.y);
			maxY = max(max(maxY, from.y), to.y);
		}

		int32 midX = (maxX + minX) / 2;
		int32 midY = (maxY + minY) / 2;

		// 라인 개수
		file << static_cast<int32>(_lines.size()) << endl;

		// 파일 입출력
		for (auto& line : _lines)
		{
			POINT from = line.first;
			from.x -= midX;
			from.y -= midY;

			POINT to = line.second;
			to.x -= midX;
			to.y -= midY;

			wstring str = std::format(L"({0},{1})->({2},{3})", from.x, from.y, to.x, to.y);
			file << str << endl;
		}
		
		file.close();
	}

	// Load
	if (GET_SINGLE(InputManager)->GetButtonDown(KeyType::D))
	{
		wifstream file;
		file.open(L"Unit.txt");

		// 라인 개수
		int32 count;
		file >> count;

		_lines.clear();

		int32 midX = 400;
		int32 midY = 300;

		for (int32 i = 0; i < count; i++)
		{
			POINT p1;
			POINT p2;

			wstring str;
			file >> str;
			::swscanf_s(str.c_str(), L"(%d,%d)->(%d,%d)", &p1.x, &p1.y, &p2.x, &p2.y); // POINT에 저장

			p1.x += midX;
			p1.y += midY;
			p2.x += midX;
			p2.y += midY;

			_lines.push_back(make_pair(p1, p2));
			_setOrigin = true;
		}

		file.close();
	}
}

void EditScene::Render(HDC hdc)
{
	for (auto& line : _lines)
	{
		POINT p1 = line.first;
		POINT p2 = line.second;

		Pos pos1;
		pos1.x = (float)p1.x;
		pos1.y = (float)p1.y;

		Pos pos2;
		pos2.x = (float)p2.x;
		pos2.y = (float)p2.y;

		Utils::DrawLine(hdc, pos1, pos2);
	}
}

 

 

 

2. Resource 관련 클래스

1) ResourceBase

// ResourceBase.h

#pragma once

class ResourceBase
{
public:
	ResourceBase();
	virtual ~ResourceBase();
};

 

2) LineMesh : 사용자가 그린 그림을 저장하고 로드하는 기능

// LineMesh.h

#pragma once
#include "ResourceBase.h"
class LineMesh :
    public ResourceBase
{
public:
    void Save(wstring path);
    void Load(wstring path);
    
    void Render(HDC hdc, Pos pos) const;

protected:
    vector<pair<POINT, POINT>> _lines;
};


// LineMesh.cpp

void LineMesh::Render(HDC hdc, Pos pos) const
{
	for (auto& line : _lines)
	{
		POINT p1 = line.first;
		POINT p2 = line.second;

		Pos pos1;
		pos1.x = pos.x + (float)p1.x;
		pos1.y = pos.y + (float)p1.y;

		Pos pos2;
		pos2.x = pos.x + (float)p2.x;
		pos2.y = pos.y + (float)p2.y;

		Utils::DrawLine(hdc, pos1, pos2);
	}
}

 

3) ResourceManager

// ResourceManager.h

#pragma once

class ResourceBase;
class LineMesh;

class ResourceManager
{
public:
	DECLARE_SINGLE(ResourceManager);

	~ResourceManager();

public:
	void Init();
	void Clear();

	const LineMesh* GetLineMesh(wstring key);

private:
	unordered_map<wstring, LineMesh*> _lineMeshes;
};

 

 

 

3. Game과 Player 수정

1) GameScene : ResourceManager->Init(), Clear() 추가

2) Player : DrawCircle → ResourceManager->GetLineMesh() 로 변경

// Player.cpp

void Player::Render(HDC hdc)
{
	const LineMesh* mesh = GET_SINGLE(ResourceManager)->GetLineMesh(L"Player");
	if (mesh)
	{
		mesh->Render(hdc, _pos);
	}
}

 

 

'게임 엔진 > Window API' 카테고리의 다른 글

[Window API] Texture, Sprite  (1) 2025.03.03
[Window API] 포트리스 모작  (0) 2025.03.03
[Window API] Object 설계  (0) 2025.02.17
[Window API] 프레임워크 제작 2  (0) 2025.02.15
[Window API] 프레임워크 제작  (0) 2025.02.14