🔧 1. ConsoleHelper 클래스 구성
📁 ConsoleHelper.h (헤더 파일)
#pragma once
#include <windows.h>
#include <iostream>
#include <vector>
#include "Types.h"
using namespace std;
✅ 헤더 포함 설명
#pragma once: 중복 포함 방지 (헤더가 여러 번 include되어도 한 번만 처리됨)
<windows.h>: 윈도우 전용 API 함수 사용을 위한 필수 헤더
<iostream>: cout 등을 위한 입출력 스트림
<vector>: 향후 확장을 위한 표준 컨테이너
"Types.h": 사용자 정의 타입 (ex. int32, uint64 등)
using namespace std;: C++ 표준 네임스페이스 축약
🎨 콘솔 색상 정의
enum class ConsoleColor
{
Black = 0,
RED = FOREGROUND_RED,
GREEN = FOREGROUND_GREEN,
BLUE = FOREGROUND_BLUE,
YELLOW = RED | GREEN,
WHITE = RED | GREEN | BLUE,
};
enum class: 안전한 열거형 타입 (범위 충돌 방지)
ConsoleColor::RED: 콘솔에서 빨간색 출력 (윈도우 API 매크로 사용)
🛠️ 콘솔 제어 함수 선언
class ConsoleHelper
{
public:
static void SetCursorPosition(int32 x, int32 y);
static void SetCursorColor(ConsoleColor color);
static void ShowConsoleCursor(bool flag);
};
- 정적 함수(Static)로 설계해 어디서든 쉽게 호출 가능
- 커서 위치 설정, 색상 변경, 커서 보이기/숨기기 기능 제공
⚙️ 2. ConsoleHelper.cpp (구현 파일)
#include "pch.h"
#include "ConsoleHelper.h"
pch.h: 미리 컴파일된 헤더 (컴파일 속도 향상)
ConsoleHelper.h: 선언부 포함
🧭 커서 위치 설정
void ConsoleHelper::SetCursorPosition(int32 x, int32 y)
{
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = { static_cast<SHORT>(x), static_cast<SHORT>(y) };
SetConsoleCursorPosition(output, pos);
}
HANDLE output: 콘솔 핸들 획득
COORD: 커서 위치 좌표 구조체
SetConsoleCursorPosition: 실제 커서 이동 함수
🎨 색상 설정
void ConsoleHelper::SetCursorColor(ConsoleColor color)
{
HANDLE output = ::GetStdHandle(STD_OUTPUT_HANDLE);
::SetConsoleTextAttribute(output, static_cast<int16>(color));
}
SetConsoleTextAttribute: 텍스트 색상 변경 함수
static_cast<int16>: enum 값을 정수로 변환
👀 커서 보이기/숨기기
void ConsoleHelper::ShowConsoleCursor(bool flag)
{
HANDLE output = ::GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
::GetConsoleCursorInfo(output, &cursorInfo);
cursorInfo.bVisible = flag;
::SetConsoleCursorInfo(output, &cursorInfo);
}
GetConsoleCursorInfo: 현재 커서 정보 조회
bVisible: 커서 표시 여부 설정
SetConsoleCursorInfo: 수정된 커서 정보 적용
🧪 3. Maze.cpp - 메인 함수와 콘솔 출력 예제
#include "pch.h"
#include <iostream>
#include "ConsoleHelper.h"
🕒 시간 측정 및 프레임 관리
uint64 lastTick = 0;
#pragma region 프레임관리
const uint64 currentTick = ::GetTickCount64();
const uint64 deltaTick = currentTick - lastTick;
lastTick = currentTick;
#pragma endregion
GetTickCount64(): 시스템 부팅 이후 경과 시간(ms)
deltaTick: 프레임 간 경과 시간 측정 (프레임 기반 게임에 사용)
🖼️ 콘솔에 25x25 타일 맵 출력
ConsoleHelper::SetCursorPosition(0, 0);
ConsoleHelper::ShowConsoleCursor(false);
ConsoleHelper::SetCursorColor(ConsoleColor::RED);
const char* TILE = " ■";
for (int32 y = 0; y < 25; y++)
{
for (int32 x = 0; x < 25; x++)
{
cout << TILE;
}
cout << endl;
}
- 커서 이동 → 커서 숨김 → 색상 설정 → 타일 반복 출력
TILE 문자 하나로 간단한 맵 시각화 구현
🧱 4. 미로 준비: 맵 생성 코드
01-2 맵 만들기: 외곽 벽 설정
for (int32 y = 0; y < _size; y++)
{
for (int32 x = 0; x < _size; x++)
{
if (x == 0 || x == _size - 1 || y == 0 || y == _size - 1)
_tile[y][x] = TileType::WALL;
else
_tile[y][x] = TileType::EMPTY;
}
}
- 맵 외곽 벽 설정: 가장자리만
WALL, 나머지는 EMPTY
맵 격자 기반 벽 생성
for (int32 y = 0; y < _size; y++)
{
for (int32 x = 0; x < _size; x++)
{
if (x % 2 == 0 || y % 2 == 0)
_tile[y][x] = TileType::WALL;
else
_tile[y][x] = TileType::EMPTY;
}
}
- 격자 형태로 벽 생성: 홀수 위치만 뚫려있는 상태
if (x % 2 == 0 || y % 2 == 0)
continue;
- 벽 위치 건너뛰기: 나중에 미로 생성 알고리즘이 통로만 뚫게 유도
📖 참고 도서
- Mazes for Programmers
다양한 미로 생성 알고리즘(DFS, Kruskal, Prim 등)을 소개한 필독서
🚪 01-3 오른손 법칙 구현 힌트
int32 newDir = (_dir - 1 + DIR_COUNT) % DIR_COUNT;
- 오른손 법칙: 현재 방향에서 오른쪽 방향 탐색
DIR_COUNT: 총 방향 수 (보통 4)