DOTY

DirectX11. 1) 창 띄우기 본문

DirectX

DirectX11. 1) 창 띄우기

증식세포 2023. 6. 7. 14:23
728x90
반응형

1. WinMain

#include "Systemclass.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
{
	SystemClass* System;
	bool result;


	// Create the system object.
	System = new SystemClass;

	// Initialize and run the system object.
	result = System->Initialize();
	if (result)
	{
		System->Run();
	}

	// Shutdown and release the system object.
	System->Shutdown();
	delete System;
	System = 0;

	return 0;
}

SystemClass를 초기화하고 문제가 없을 경우 Run()을 실행하게 된다.

 

2. SystemClass.h

#ifndef _SYSTEMCLASS_H_
#define _SYSTEMCLASS_H_

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include "inputclass.h"
#include "applicationclass.h"

class SystemClass
{
public:
	SystemClass();
	SystemClass(const SystemClass&);
	~SystemClass();

	bool Initialize();
	void Shutdown();
	void Run();

	LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM);

private:
	bool Frame();
	void InitializeWindows(int&, int&);
	void ShutdownWindows();

private:
	LPCWSTR m_applicationName;
	HINSTANCE m_hinstance;
	HWND m_hwnd;

	InputClass* m_Input;
	ApplicationClass* m_Application;
};

static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static SystemClass* ApplicationHandle = 0;


#endif

여기서 주목할 점은 [ #define WIN32_LEAN_AND_MEAN ] 이다.

속도를 높이기 위해서 수행하는 작업이며 덜 사용되는 API 중 일부를 제외하여 Win32 헤더파일의 크기를 줄여준다.

 

정의된 함수로는 Initialize, Shutdown, Run 함수가 있으며 Windows에서 시스템 메세지를 처리할 MessageHandler함수도 있다. m_Input은 입력을, m_Application은 렌더링을 처리하게 된다.

※ m_Input은 Input Class 변수를, m_Application은 Application Class 변수를 나타낸다. m_Application의 경우 Application.h의 변수 FULL_SCREEN을 SystemClass에서 처리한다. 

 

2-1. SystemClass()

m_Input = 0;
m_Application = 0;

모든 변수화 포인터를 0으로 초기화 시켜주는 함수

 

2-2. Initialize

InitializeWindows(screenWidth, screenHeight);

Application에서 사용할 창을 설정하게 된다. 

 

2-3. Shutdown

모든것을 종료하고 초기화 시키는 함수

 

2-4. Run

각 루프에 해당하는 Frame을 기준으로 처리가 된다. 종료할 때 까지 계속 While문(bool done이 true일때 끝난다.)이 돌아가게 된다.

 

2-4-1. Frame

bool SystemClass::Frame()
{
	bool result;


	// Check if the user pressed escape and wants to exit the application.
	if (m_Input->IsKeyDown(VK_ESCAPE))
	{
		return false;
	}

	// Do the frame processing for the application class object.
	result = m_Application->Frame();
	if (!result)
	{
		return false;
	}

	return true;
}

Run함수 내에서 계속 불려오는 함수로 bool 형태를 반환한다.

ESC 키를 누를 경우 false를 반환하게 되고, Run함수 내에서 While문의 변수 donetrue로 바뀌며 Run함수가 끝나게 된다.

 

2-5. MessageHandler

Windows 메세지를 보내는 곳으로 특정 정보를 들을 수 있게 한다. switch - case 문을 활용하여 키가 눌렸는지 떼졌는지 체크하게 된다.

 

2-6. Initialize Windows

screenHeight와 screenWidth를 통해 창의 크기를 정할 수 있고, 애플리케이션 전체에서도 사용할 수 있다. Application.h의 변수 FULL_SCREEN에 따라서 작은 창과 전체 창을 만들 수 있다.

 

2-6-1. ShutdownWindows

Initialize Windows에서 한 세팅을 해제하여 원래 상태로 돌린다.

 

2-7. WinProc

Windows에서 메세지를 보내는 위치를 설정한다. InitializeWindows 함수에서 wc.IpfnWndProc = WndProc를 사용하여 창 클래스를 초기화할 때 창의 이름을 알려준다.

 

3. Input.h

#ifndef _INPUTCLASS_H_
#define _INPUTCLASS_H_

class InputClass
{
public:
	InputClass();
	InputClass(const InputClass&);
	~InputClass();

	void Initialize();

	void KeyDown(unsigned int);
	void KeyUp(unsigned int);

	bool IsKeyDown(unsigned int);

private:
	bool m_keys[256];
};

#endif

사용자의 키보드 입력을 받아 처리한다.  MessageHandler함수에서 사용된다.

InputClass *m_Input;
m_Input = 0;
m_Input = new InputCalss;

m_Input을 선언한 뒤에 NULL로  초기화를 해준다. 그 후에 MessageHandler에서는 전역변수로 된 m_Input을 활용하여 KeyDownKeyUp 등을 사용하게 된다. 

 

3-1. Initialize

void InputClass::Initialize()
{
	for(int i=0; i<256; i++)
	{
		m_keys[i] = false;
	}

	return;
}

m_keys[256]의 모든 원소를 false로 초기화 시켜주는 과정.

 

3-2. KeyUP / KeyDown / IsKeyDown

다음 ASCII값에 따라 입력을 받을 경우(KeyDown) true로 바뀌게 된다. 키보드를 떼면(KeyUp) 다시 false로 바꿔준다. IsKeyDown을 통해서 키가 눌렸는지 체크할 수도 있다.

 

4. Application.h

#ifndef _APPLICATIONCLASS_H_
#define _APPLICATIONCLASS_H_

#include <windows.h>

const bool FULL_SCREEN = false;
const bool VSYNC_ENABLED = true;
const float SCREEN_DEPTH = 1000.0f;
const float SCREEN_NEAR = 0.3f;

class ApplicationClass
{
public:
	ApplicationClass();
	ApplicationClass(const ApplicationClass&);
	~ApplicationClass();

	bool Initialize(int, int, HWND);
	void Shutdown();
	bool Frame();

private:
	bool Render();

private:

};

#endif
  • FULL_SCREEN : 실행시 전체화면으로 실행되는지에 대한 변수
  • VSYNC_ENABLED : 수직 동기화를 활성화 하는 변수
  • SCREEN_DEPTH : 가장 먼 거리(깊이)
  • SCREEN_NEAR : 가장 가까운 거리(깊이)

SystemClass에 의해 만들어지는 클래스로 모든 그래픽 기능이 Application 클래스에 캡슐화된다. 

 

※수직 동기화 : 컴퓨터의 그래픽 처리를 화면의 주사율과 동기화하여 플리커(깜빡임)과 이미지 깨짐을 방지하게 된다. 활성화할 경우 그래픽 카드의 프레임 속도와 모니터의 주사율을 동기화 하여 플리커와 이미지 깨짐을 방지하지만 그래픽 연산이 더 늘어날 수 있다. 

 

결과 화면

 

 


DirectX 11 on Windows 10 Tutorials (rastertek.com)

 

DirectX 11 on Windows 10 Tutorials

Tutorial 8: Scaling, Rotation, and Translation

www.rastertek.com

 

728x90
반응형

'DirectX' 카테고리의 다른 글

DirectX11. 3) Buffer, Shader I  (0) 2023.06.11
DirectX11. 2) D3D11 Initialize  (1) 2023.06.09
4. DirectXMath - Transform  (1) 2023.05.02
3. DirectXMath - Matrix  (0) 2023.04.24
2. DirectXMath - Vector  (0) 2023.04.21