while (!glfwWindowShouldClose(window)) {
updateFunc(); // 2. update
drawFunc(); // 3. draw
glfwSwapBuffers(window);
glfwPollEvents(); // 1. input processing
}
GLFW의 타이머가 높은 정밀도를 보장하는 건 아님!
(보통 수십 ms 정도의 정밀도는 보장)
float theta = 0.0F;
void updateFunc(void) {
float elapsedTime = (float)glfwGetTime(); // double로 반환되는 시간
theta = elapsedTime * (float)M_PI_2; // in <math.h>, M_PI_2 = pi/2
}
// R키 입력 시 타이머 리셋
void keyFunc(GLFWwindow* window, int key, int scancode, int action, int mods) {
switch (key) {
case GLFW_KEY_R:
if (action == GLFW_PRESS) {
glfwSetTime(0.0); // reset the timer
}
break;
}
}
ns 단위의 고정밀도 시간 제공 (wall-clock time)
ms 뿐만 아니라 us, ns까지도 사용 가능
system_clock::time_point lastTime = system_clock::now();
// do something
system_clock::time_point curTime = system_clock::now();
milliseconds duration = duration_cast<milliseconds>(curTime-lastTime);
해당 thread를 일정 시간동안 sleep 시킴, CPU는 idle 상태
#include <thread>
// 100ms동안 시스템 sleep
std::this_thread::sleep_for(std::chrono::milliseconds(100));
lastTime을 기준으로 각도 회전
float theta = 0.0F;
system_clock::time_point lastTime = system_clock::now();
void updateFunc(void) {
system_clock::time_point curTime = system_clock::now();
milliseconds elapsedTimeMSEC = duration_cast<milliseconds>(curTime - lastTime); // in millisecond
theta = (elapsedTimeMSEC.count() / 1000.0F) * (float)M_PI_2; // in <math.h>, M_PI_2 = pi/2
}
void keyFunc(GLFWwindow* window, int key, int scancode, int action, int mods) {
switch (key) {
case GLFW_KEY_R:
if (action == GLFW_PRESS) {
lastTime = system_clock::now();
}
break;
}
}