
신기술 & 미래
Game Loop게임 루프
게임 루프는 게임 애플리케이션의 핵심 실행 패턴으로, 사용자 입력 처리 → 게임 상태 업데이트 → 화면 렌더링을 반복한다. 프레임 레이트 안정성과 물리 시뮬레이션 일관성이 핵심 과제다.
기본 게임 루프 패턴
while (게임 실행 중):
입력 처리 (Input)
게임 로직 업데이트 (Update)
물리 시뮬레이션 (Physics)
렌더링 (Render)
프레임 제한 (Frame Cap)고정 타임스텝 루프 (권장)
cpp
const double FIXED_TIMESTEP = 1.0 / 60.0; // 60 Hz 물리
double previousTime = getCurrentTime();
double accumulator = 0.0;
while (running) {
double currentTime = getCurrentTime();
double frameTime = currentTime - previousTime;
previousTime = currentTime;
// 프레임이 너무 길면 자르기 (스파이크 방지)
if (frameTime > 0.25) frameTime = 0.25;
accumulator += frameTime;
// 고정 타임스텝으로 물리 업데이트
while (accumulator >= FIXED_TIMESTEP) {
processInput();
updatePhysics(FIXED_TIMESTEP);
accumulator -= FIXED_TIMESTEP;
}
// 렌더링은 가능한 빠르게 (보간 포함)
double alpha = accumulator / FIXED_TIMESTEP;
render(alpha); // 이전-현재 상태 보간
}JavaScript/Canvas 게임 루프
javascript
class GameLoop {
constructor() {
this.lastTime = 0;
this.accumulator = 0;
this.FIXED_DT = 1000 / 60; // 60fps
this.running = false;
}
start() {
this.running = true;
requestAnimationFrame(this.loop.bind(this));
}
loop(timestamp) {
if (!this.running) return;
const deltaTime = timestamp - this.lastTime;
this.lastTime = timestamp;
this.accumulator += Math.min(deltaTime, 250);
while (this.accumulator >= this.FIXED_DT) {
this.update(this.FIXED_DT / 1000); // 초 단위로 변환
this.accumulator -= this.FIXED_DT;
}
const alpha = this.accumulator / this.FIXED_DT;
this.render(alpha);
requestAnimationFrame(this.loop.bind(this));
}
update(dt) {
// 게임 상태 업데이트
this.player.update(dt);
this.physics.step(dt);
}
render(alpha) {
// 보간된 위치로 렌더링
this.renderer.draw(alpha);
}
}