기본 프레임워크

Jaemyeong Lee·2024년 8월 14일
0

수업


주제

DirectX 개발을 위한 Visual Studio 프로젝트 초기 세팅과 프레임워크 구성 (PCH 설정, 외부 라이브러리 연동, 메시지 루프, Game 클래스 설계)


개념

● Visual Studio 프로젝트 생성과 구조 설계

  • Visual Studio → Windows 데스크톱 애플리케이션(C++) 템플릿 선택
  • 프로젝트 이름 예: GameCoding, 01.Start
  • 생성 직후 실행 시 기본 윈도우 창이 열리는 WinAPI 기반 프로젝트 구조 제공

🔹 필터(가상 폴더) 구성

  • 01. Main: GameCoding.cpp, Game.h 등 메인 실행/게임 로직 관련 파일 관리
  • 99. Headers: 공용 헤더 파일, 구조체, 상수 정의 등 포함
  • Andromeda: pch.h, pch.cpp 등 미리 컴파일 헤더 관리 전용 폴더

● 출력 디렉토리 설정

🔹 속성 → 일반 → 출력 디렉터리 설정

$(SolutionDir)Binaries\
  • 빌드 시 .exe.pdb 파일이 Binaries 폴더에 자동 생성
  • 예시: 01.Start.exe, 01.Start.pdb
  • 디버깅 및 실행 파일 관리가 쉬워짐

● 외부 라이브러리 연동 설정

🔹 Include 및 Lib 경로 지정

  • C/C++ → 일반 → 추가 포함 디렉터리
$(SolutionDir)Libraries/Include/
  • Linker → 일반 → 추가 라이브러리 디렉터리
$(SolutionDir)Libraries/Lib/
  • 헤더 파일은 Include/ 폴더, .lib 파일은 Lib/ 폴더에 배치

🔹 코드 내 라이브러리 명시

#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dcompiler.lib")

#ifdef _DEBUG
#pragma comment(lib, "DirectXTex\\DirectXTex_Debug.lib")
#else
#pragma comment(lib, "DirectXTex\\DirectXTex.lib")
#endif

→ Debug/Release 모드에 따라 다른 라이브러리를 자동 적용


● 미리 컴파일된 헤더(PCH) 설정

🔹 pch.h 내용

#pragma once

#include "Types.h"
#include "Values.h"
#include "Struct.h"

// STL
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
using namespace std;

// WIN
#include <Windows.h>
#include <assert.h>

// DX
#include <d3d11.h>
#include <d3dcompiler.h>
#include <wrl.h>
#include <DirectXMath.h>
#include <DirectXTex/DirectXTex.h>
#include <DirectXTex/DirectXTex.inl>

using namespace DirectX;
using namespace Microsoft::WRL;

🔹 프로젝트 속성 설정

  • pch.cpp:
    • C/C++ → Precompiled Headers → Create (/Yc)
  • 나머지 .cpp 파일들:
    • Use (/Yu)
  • .cpp 상단에는 반드시 #include "pch.h" 포함
  • 누락 시 에러:
    error C1010: unexpected end of file while looking for precompiled header

● 공용 헤더 구성

파일명내용
Types.h사용자 정의 정수 타입, DirectX 벡터 별칭 (XMFLOAT2, XMFLOAT3 등)
Values.h전역 상수 정의 (예: GWinSizeX, GWinSizeY)
Struct.h공용 구조체 정의 예정
pch.h자주 사용하는 헤더 및 라이브러리 포함 (컴파일 효율 목적)

● 메시지 루프: WinAPI → 게임 루프 구조로 변경

기존 메시지 루프 (WinAPI 기본 구조)

while (GetMessage(&msg, nullptr, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

변경된 구조 (게임 루프 방식)

while (msg.message != WM_QUIT)
{
    if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    else {
        game.Update();
        game.Render();
    }
}

입력이 없을 때도 게임 상태를 계속 업데이트 및 렌더링할 수 있음


용어 정리

용어설명
출력 디렉터리빌드 후 생성 파일(.exe, .pdb 등)을 저장하는 경로
추가 포함 디렉터리.h, .inl 파일이 있는 외부 헤더 경로
추가 라이브러리 디렉터리.lib 파일들이 존재하는 경로
PCH자주 쓰이는 헤더를 미리 컴파일해 빌드 속도를 높이는 시스템
pragma comment(lib)특정 라이브러리를 코드 상에서 직접 연결하는 방식

코드 분석

✅ Game.h

#pragma once
class Game
{
public:
    Game(); ~Game();

    void Init(HWND hwnd);
    void Update();
    void Render();

private:
    HWND _hwnd;
    uint32 _width = 0;
    uint32 _height = 0;
};

✅ Game.cpp

#include "pch.h"
#include "Game.h"

Game::Game() {}
Game::~Game() {}

void Game::Init(HWND hwnd)
{
    _hwnd = hwnd;
    _width = GWinSizeX;
    _height = GWinSizeY;
}

void Game::Update() {}
void Game::Render() {}

✅ GameCoding.cpp

#include "pch.h"
#include "framework.h"
#include "GameCoding.h"
#include "Game.h"

HINSTANCE hInst;
HWND hWnd;

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int nCmdShow)
{
    MyRegisterClass(hInstance);
    if (!InitInstance(hInstance, nCmdShow)) return FALSE;

    Game game;
    game.Init(hWnd);

    MSG msg = {};
    while (msg.message != WM_QUIT)
    {
        if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else {
            game.Update();
            game.Render();
        }
    }

    return (int) msg.wParam;
}

→ 핵심: 게임 객체를 생성하고 초기화한 뒤, 메시지 루프 안에서 계속 Update/Render 호출


핵심

  • 미리 컴파일 헤더 설정은 필수: 모든 cpp에 #include "pch.h" 반드시 포함, 설정 안 하면 컴파일 에러 발생
  • 외부 라이브러리는 Include/Lib 경로를 분리해서 관리하고, pragma comment(lib)로 연결
  • 출력 디렉토리는 Binaries 폴더로 통일해 결과물 관리
  • Game 클래스 구조화는 게임 루프 기반 렌더링 시스템 설계의 시작점
  • PeekMessage 기반 루프는 실시간 처리에 필수
  • ✅ 이 구조는 이후 DirectX11 렌더링 파이프라인(삼각형 그리기, 셰이더 구성 등)을 위해 반드시 필요한 기본기

profile
李家네_공부방

0개의 댓글