Input 제작
#pragma once
enum class KEY_TYPE
{
UP = VK_UP,
DOWN = VK_DOWN,
LEFT = VK_LEFT,
RIGHT = VK_RIGHT,
W = 'W',
A = 'A',
S = 'S',
D = 'D',
};
enum class KEY_STATE
{
NONE,
PRESS,
DOWN,
UP,
END
};
enum
{
KEY_TYPE_COUNT = static_cast<int32>(UINT8_MAX),
KEY_STATE_COUNT = static_cast<int32>(KEY_STATE::END),
};
class Input
{
public:
void Init(HWND hwnd);
void Update();
// 누르고 있을 때
bool GetButton(KEY_TYPE key) { return GetState(key) == KEY_STATE::PRESS; }
// 맨 처음 눌렀을 때
bool GetButtonDown(KEY_TYPE key) { return GetState(key) == KEY_STATE::DOWN; }
// 맨 처음 눌렀다 뗐을 때
bool GetButtonUp(KEY_TYPE key) { return GetState(key) == KEY_STATE::UP; }
private:
inline KEY_STATE GetState(KEY_TYPE key) { return _states[static_cast<uint8>(key)]; }
private:
HWND _hwnd;
vector<KEY_STATE> _states;
};
#include "pch.h"
#include "Input.h"
void Input::Init(HWND hwnd)
{
_hwnd = hwnd;
_states.resize(KEY_TYPE_COUNT, KEY_STATE::NONE);
}
void Input::Update()
{
HWND hwnd = ::GetActiveWindow();
if (_hwnd != hwnd)
{
for (uint32 key = 0; key < KEY_TYPE_COUNT; key++)
_states[key] = KEY_STATE::NONE;
return;
}
for (uint32 key = 0; key < KEY_TYPE_COUNT; key++)
{
// 키가 눌려 있으면 true
if (::GetAsyncKeyState(key) & 0x8000)
{
KEY_STATE& state = _states[key];
// 이전 프레임에 키를 누른 상태라면 PRESS
if (state == KEY_STATE::PRESS || state == KEY_STATE::DOWN)
state = KEY_STATE::PRESS;
else
state = KEY_STATE::DOWN;
}
else
{
KEY_STATE& state = _states[key];
// 이전 프레임에 키를 누른 상태라면 UP
if (state == KEY_STATE::PRESS || state == KEY_STATE::DOWN)
state = KEY_STATE::UP;
else
state = KEY_STATE::NONE;
}
}
}
// 키보드 입력을 받아서 이동시키기
void Game::Update()
{
GEngine->Update();
GEngine->RenderBegin();
shader->Update();
{
static Transform t = {};
if (INPUT->GetButton(KEY_TYPE::W))
t.offset.y += 1.f * DELTA_TIME;
if (INPUT->GetButton(KEY_TYPE::S))
t.offset.y -= 1.f * DELTA_TIME;
if (INPUT->GetButton(KEY_TYPE::A))
t.offset.x -= 1.f * DELTA_TIME;
if (INPUT->GetButton(KEY_TYPE::D))
t.offset.x += 1.f * DELTA_TIME;
mesh->SetTransform(t);
mesh->SetTexture(texture);
mesh->Render();
}
Timer 클래스
#pragma once
class Timer
{
public:
void Init();
void Update();
uint32 GetFps() { return _fps; }
float GetDeltaTime() { return _deltaTime; }
private:
uint64 _frequency = 0;
uint64 _prevCount = 0;
float _deltaTime = 0.f;
private:
uint32 _frameCount = 0;
float _frameTime = 0.f;
uint32 _fps = 0;
};
#include "pch.h"
#include "Timer.h"
void Timer::Init()
{
// GetTickCount64()를 안쓰는 이유?? -> 정밀도가 부족함.
::QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&_frequency));
::QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&_prevCount)); // CPU 클럭
}
void Timer::Update()
{
uint64 currentCount;
::QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(¤tCount));
_deltaTime = (currentCount - _prevCount) / static_cast<float>(_frequency);
_prevCount = currentCount;
_frameCount++;
_frameTime += _deltaTime;
// 1초마다 갱신해주세요
if (_frameTime > 1.f)
{
_fps = static_cast<uint32>(_frameCount / _frameTime);
_frameTime = 0.f;
_frameCount = 0;
}
}
성능 개선 사항
void Input::Update()
{
// ...
for (uint32 key = 0; key < KEY_TYPE_COUNT; key++)
{
// 매 프레임마다 255번씩 key체크를 하는것이 부담.
if (::GetAsyncKeyState(key) & 0x8000)
{
KEY_STATE& state = _states[key];
수정해보자
void Input::Update()
{
HWND hwnd = ::GetActiveWindow();
if (_hwnd != hwnd)
{
for (uint32 key = 0; key < KEY_TYPE_COUNT; key++)
_states[key] = KEY_STATE::NONE;
return;
}