Examples: SDL: Using SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency() to handle framerate over 1000 fps properly. Noticed bad inputs artefacts in Nav branch at 2000 fps without this. (#996)

This commit is contained in:
omar 2018-02-05 23:09:14 +01:00
parent 60fb7d31fd
commit fcf1c9d151
2 changed files with 10 additions and 10 deletions

View File

@ -22,7 +22,7 @@
#include "imgui_impl_sdl_gl2.h" #include "imgui_impl_sdl_gl2.h"
// Data // Data
static double g_Time = 0.0f; static Uint64 g_Time = 0;
static bool g_MousePressed[3] = { false, false, false }; static bool g_MousePressed[3] = { false, false, false };
static GLuint g_FontTexture = 0; static GLuint g_FontTexture = 0;
@ -265,10 +265,10 @@ void ImGui_ImplSdlGL2_NewFrame(SDL_Window *window)
io.DisplaySize = ImVec2((float)w, (float)h); io.DisplaySize = ImVec2((float)w, (float)h);
io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0); io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
// Setup time step // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
Uint32 time = SDL_GetTicks(); static Uint64 frequency = SDL_GetPerformanceFrequency();
double current_time = time / 1000.0; Uint64 current_time = SDL_GetPerformanceCounter();
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f); io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f);
g_Time = current_time; g_Time = current_time;
// Setup mouse inputs (we already got mouse wheel, keyboard keys & characters from our event handler) // Setup mouse inputs (we already got mouse wheel, keyboard keys & characters from our event handler)

View File

@ -17,7 +17,7 @@
#include <GL/gl3w.h> // This example is using gl3w to access OpenGL functions (because it is small). You may use glew/glad/glLoadGen/etc. whatever already works for you. #include <GL/gl3w.h> // This example is using gl3w to access OpenGL functions (because it is small). You may use glew/glad/glLoadGen/etc. whatever already works for you.
// Data // Data
static double g_Time = 0.0f; static Uint64 g_Time = 0.0f;
static bool g_MousePressed[3] = { false, false, false }; static bool g_MousePressed[3] = { false, false, false };
static GLuint g_FontTexture = 0; static GLuint g_FontTexture = 0;
static int g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0; static int g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
@ -376,10 +376,10 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window* window)
io.DisplaySize = ImVec2((float)w, (float)h); io.DisplaySize = ImVec2((float)w, (float)h);
io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0); io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
// Setup time step // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
Uint32 time = SDL_GetTicks(); static Uint64 frequency = SDL_GetPerformanceFrequency();
double current_time = time / 1000.0; Uint64 current_time = SDL_GetPerformanceCounter();
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f); io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f);
g_Time = current_time; g_Time = current_time;
// Setup mouse inputs (we already got mouse wheel, keyboard keys & characters from our event handler) // Setup mouse inputs (we already got mouse wheel, keyboard keys & characters from our event handler)