DirectX/DirectX 2D

DX2D_08 ImGui

컴맹학자 2022. 11. 27. 20:04
728x90

_Libraries안에 있는 ImGui 클래스는 c++ 버젼에 GUI /Widget 관련 만들때 사용되는 라이브러리 이다.

구글에서도 Github 통해서 다운 받을 수 있고 해당 ImGui 관련 기능을 ImGui.lib 정적 라이브러리로 만들어서 사용.

 

1. ImGui

생성

※ Device.cpp에 WPARAM Running()함수 영역에 키보드 관련 클래스 위에 선언 (싱글톤 방식)

더보기
WPARAM Running()
{
	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));
	
	// 싱글톤
	ImGui::Create(Hwnd, Device, DeviceContext);
	ImGui::StyleColorsDark(); //테마 (안해도됨)

	Key = new Keyboard();

	InitScene();
	while (true)
	{...}
	DestroyScene();

	SafeDelete(Key);
	ImGui::Delete();
    return msg.wParam;
}

 

값 셋팅

※ ImGui도 Window 관련 값을 받기위해 WndProc() 함수에 값을 받게 셋팅

/*------------------Msg Event (Win32API)*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	if (ImGui::WndProc(hwnd, msg, wParam, lParam))
		return true;

	switch (msg){ 
		case WM_DESTROY: PostQuitMessage(0); break; 
	
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}

 

Update, Render 셋팅

※ Device.cpp -> Running() 함수에 Update 셋팅  / Scene.cpp -> Render() 함수에 BackBuffer 출력전에 Render 셋팅


2. 사용

Scene.cpp -> void Update() 함수 안에서 사용

 

코드

※ ImGui 관련 코드 작성

shader 클래스 통해서 gup에 값을 넣어주는 응용방식 사용

bool Wirte = false;
Vector3 color = {0.0f, 0.0f, 0.0f}; //D3DXCOLOR 구조체 사용해도 상관없음
void Update() 
{	
	//ImGui
	{
		ImGui::Text("%s", "Imgui Setting");				// 텍스트
		ImGui::Checkbox("Frame", &Wirte);				// 체크 박스
		ImGui::ColorEdit3("Color", (float*)&color, 0);			// Color Edit
		ImGui::Text("%f, %f, %f", color.x, color.y, color.z);
	}

	// cpu에서 Semantic이름을 통해서 해당값 접근
	// 1. AsVector로 접근 2. SetFloatVector 통해서 셋팅
	shader->AsVector("Color")->SetFloatVector(color);;

	//RS
	if(Key->Toggle('1')) Wirte = !Wirte;

	//Key
	{...}

	// Subresouce (CPU방식)
	D3D11_MAPPED_SUBRESOURCE subResouce;
	DeviceContext->Map(vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &subResouce);
	{...}
	DeviceContext->Unmap(vertexBuffer, 0);
}

실행


3. 정리

1. ImGui 통해서 c++ 코드 상으로 UI 만들어서 사용 할 수 있다.

2. Shader 클래스 통해서 gpu의 Semantic 이름으로 접근해서 값을 넣고 ImGui로 조절 할 수도 있다.

 

https://github.com/ascher8159/DX2D

DX2D_08

'DirectX > DirectX 2D' 카테고리의 다른 글

DX2D_9 WVP2  (1) 2022.12.04
DX2D_09 WVP  (0) 2022.12.01
정리 & Plug in  (0) 2022.11.26
DX2D_07 Librarie 및 Utilitie  (0) 2022.11.24
DX2D_06 IndexBuffer  (2) 2022.11.22