GameProgramming:MainLoop
게임 엔진의 메인(렌더링) 루프에 대한 내용.
Categories
- Rendering Loop
- DOM Event Architecture - UI 이벤트 전파 구현시 사용 가능한 DOM 이벤트 전파에 대한 아키텍처 참조.
Modern C++ Game Loop
#include <chrono>
using namespace std::chrono_literals;
// we use a fixed timestep of 1 / (60 fps) = 16 milliseconds
constexpr std::chrono::nanoseconds timestep(16ms);
struct game_state {
// this contains the state of your game, such as positions and velocities
};
bool handle_events() {
// poll for events
return false; // true if the user wants to quit the game
}
void update(game_state *) {
// update game logic here
}
void render(game_state const &) {
// render stuff here
}
game_state interpolate(game_state const & current, game_state const & previous, float alpha) {
game_state interpolated_state;
// interpolate between previous and current by alpha here
return interpolated_state;
}
int main() {
using clock = std::chrono::high_resolution_clock;
std::chrono::nanoseconds lag(0ns);
auto time_start = clock::now();
bool quit_game = false;
game_state current_state;
game_state previous_state;
while(!quit_game) {
auto delta_time = clock::now() - time_start;
time_start = clock::now();
lag += std::chrono::duration_cast<std::chrono::nanoseconds>(delta_time);
quit_game = handle_events();
// update game logic as lag permits
while(lag >= timestep) {
lag -= timestep;
previous_state = current_state;
update(¤t_state); // update at a fixed rate each time
}
// calculate how close or far we are from the next timestep
auto alpha = (float) lag.count() / timestep.count();
auto interpolated_state = interpolate(current_state, previous_state, alpha);
render(interpolated_state);
}
}
See also
Favorite site
- Multithreading C++ loop
- Index listing for doc/html/boost_asio/example/http/
- How game servers with Boost:Asio work asynchronously?
- Multithreaded Game Loop Rendering/Updating (boost-asio)
- GPG Study: 게임 루프와 메세지 루프의 분리
- GPG Study: 윈도우즈 기반 게임을 위한 선형적 프로그래밍 모델
- [추천] deWiTTERS Game Loop