본문으로 바로가기

DX11_2020_0807_금_Camera

category DX11 2D Game Programming 2020. 8. 7. 17:28

200807_Camera.zip
2.31MB

 

 

 

1. Program

  a. SetViewBuffer 함수 수정

  b. Update함수 에서 SetViewBuffer함수 실행

더보기
void Program::Update()
{
	Camera::Get()->Update();
	SetViewBuffer();
	// 이하 생략
}

void Program::SetViewBuffer()
{
	Matrix view{};
    // Macro : CAM -> Camera::Get()
	view = XMMatrixTranslation(CAM->OffsetX(), CAM->OffsetY(), CAM->OffsetZ());
	viewBuffer->Set(view);
}

 

2. Camera

  a. Free Camera 기능 구현

  b. Follow Target Camera 기능 구현

더보기
#pragma once

class Camera
{
public:
	Vector2 camPos;
	Vector2 camOffset;
	//
	float freeCamSpeed;
	float followTargetSpeed;
	//
	Transform* target;
	//
	float left;
	float right;
	float top;
	float bottom;
	//
private:
	Camera();
	~Camera();

public:
	static Camera* Get()
	{
		static Camera instance;
		return &instance;
	}
	//
	float OffsetX() { return camPos.x; }
	float OffsetY() { return camPos.y; }
	float OffsetZ() { return 0; }
	//
	void Update();
	//
	void FollowTargetCamMove();
	void FreeCamMove();
	//
	void SetTarget(Transform* tf) { target = tf; }
	//
	void SetCamOffset(Vector2 offset) { camOffset = offset; }
	void SetCamBorder(float _left, float _right, float _top, float _bottom);
};
/////////////////////////////////////////////////////////////////////////////////////////////
#include "Framework.h"
#include "Camera.h"

Camera::Camera()
	: camPos{}, freeCamSpeed(500.0f), followTargetSpeed(5.0f),
	target(nullptr),
	left(0), top(0), right(0), bottom(0)
{
}

Camera::~Camera()
{
}

void Camera::Update()
{
	if (target != nullptr)
		FollowTargetCamMove();
	else
		FreeCamMove();
	//
}

void Camera::FollowTargetCamMove()
{
	/*
	Vector2 destPos = { target->pos.x, target->pos.y };
	Vector2 pos = LERP(camPos, destPos, followTargetSpeed * DELTA);
	camPos = pos;
	*/
	camPos.x = camOffset.x - (target->pos.x);
	camPos.y = camOffset.y - (target->pos.y);
}

void Camera::FreeCamMove()
{
	if (KEY_PRESS('A') || KEY_PRESS('a'))
	{
		camPos.x += freeCamSpeed * DELTA;
		//if (camPos.x < left)
		//	camPos.x = left;
	}
	if (KEY_PRESS('D') || KEY_PRESS('d'))
	{
		camPos.x -= freeCamSpeed * DELTA;
		//if (camPos.x > right)
		//	camPos.x = right;
	}
	if (KEY_PRESS('W') || KEY_PRESS('w'))
	{
		camPos.y -= freeCamSpeed * DELTA;
		//if (camPos.y < top)
		//	camPos.y = top;
	}
	if (KEY_PRESS('S') || KEY_PRESS('s'))
	{
		camPos.y += freeCamSpeed * DELTA;
		//if (camPos.y > bottom)
		//	camPos.y = bottom;
	}
}

void Camera::SetCamBorder(float _left, float _right, float _top, float _bottom)
{
	left = _left;
	right = _right;
	top = _top;
	bottom = _bottom;
}

 

3. Game Scene

  a. Scene 초기화시 Camera 초기화(pos, offset)

  b. Func 함수에서 'F1', 'F2' 입력으로 Camera Target 설정

더보기
#pragma once

#include "Object/GameObject/BackGround.h"

class EffectScene : public Scene
{
private:
	Knight* knight;
	BackGround* bg;
	//
public:
	EffectScene();
	~EffectScene();
	//
	virtual void Update() override;
	virtual void Render() override;
	virtual void PostRender() override;
	//
	void InitScene();
	//
	void Func();
	void Func1_SetTarget();
	void Func2_FreeTarget();
	//
};
///////////////////////////////////////////////////////////////////////////////////////////////
#include "Framework.h"
#include "EffectScene.h"

EffectScene::EffectScene()
{
	InitScene();
}

EffectScene::~EffectScene()
{
	delete knight;
	delete bg;
}

void EffectScene::Update()
{
	bg->Update();
	knight->Update();
	//
	Func();
}

void EffectScene::Render()
{
	bg->Render();
	knight->Render();
}

void EffectScene::PostRender()
{
	/* Cam Debug */
	ImGui::Text("[Cam]");
	if (CAM->target)
		ImGui::Text("Mode : Follow Target");
	else
		ImGui::Text("Mode : Free Camera");
	ImGui::Text("CamPos : %f, %f", CAM->camPos.x, CAM->camPos.y);
	//
	bg->PostRender();
	knight->PostRender();
}

void EffectScene::InitScene()
{
	/* Set Effects */
	EFFECT->Add("fire", 20, L"Textures/FX/fire_8x2.png", 8, 2, 0.05f, true);
	EFFECT->Add("whity", 20, L"Textures/FX/whity_4x4.png", 4, 4, 0.05f, true);
	/* Set Objects */
	knight = new Knight;
	bg = new BackGround;
	/* Set Camera */
	CAM->camPos = { 340.0f, 0.0f };
	Vector2 camOffset = { ((float)WIN_WIDTH * 0.5f), 200.0f };
	CAM->SetCamOffset(camOffset);
}

void EffectScene::Func()
{
	Func1_SetTarget();
	Func2_FreeTarget();
}

void EffectScene::Func1_SetTarget()
{
	if (KEY_DOWN(VK_F1))
		CAM->SetTarget(dynamic_cast<Transform*>(knight));
}

void EffectScene::Func2_FreeTarget()
{
	if (KEY_DOWN(VK_F2))
		CAM->SetTarget(nullptr);
}

 

'DX11 2D Game Programming' 카테고리의 다른 글

DX11_2020_0812_화_OutLine_PSShader  (0) 2020.08.12
DX11_2020_0810_월_Top-Down_Shooting  (0) 2020.08.10
DX11_2020_0806_목_FX  (0) 2020.08.05
DX11_2020_0730_목_Animation(XML)  (0) 2020.07.30
DX11_2020_0729_수_Animation  (0) 2020.07.29