From b3b400895d7c5d43c44f2d62fe4bdf2004de55d4 Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:18:48 +0000 Subject: [PATCH] Add `delta_time_seconds` to the SDL OpenGL renderers (#671) RE: #627 --- demo/sdl_opengl2/nuklear_sdl_gl2.h | 5 +++++ demo/sdl_opengl3/nuklear_sdl_gl3.h | 14 ++++++++++---- demo/sdl_opengles2/nuklear_sdl_gles2.h | 14 ++++++++++---- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/demo/sdl_opengl2/nuklear_sdl_gl2.h b/demo/sdl_opengl2/nuklear_sdl_gl2.h index 16e11c0..a74ab94 100644 --- a/demo/sdl_opengl2/nuklear_sdl_gl2.h +++ b/demo/sdl_opengl2/nuklear_sdl_gl2.h @@ -51,6 +51,7 @@ static struct nk_sdl { struct nk_sdl_device ogl; struct nk_context ctx; struct nk_font_atlas atlas; + float time_of_last_frame; } sdl; NK_INTERN void @@ -74,6 +75,10 @@ nk_sdl_render(enum nk_anti_aliasing AA) int display_width, display_height; struct nk_vec2 scale; + float now = ((float)SDL_GetTicks64()) / 1000; + sdl.ctx.delta_time_seconds = now - sdl.time_of_last_frame; + sdl.time_of_last_frame = now; + SDL_GetWindowSize(sdl.win, &width, &height); SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height); scale.x = (float)display_width/(float)width; diff --git a/demo/sdl_opengl3/nuklear_sdl_gl3.h b/demo/sdl_opengl3/nuklear_sdl_gl3.h index 4165369..a2e9775 100644 --- a/demo/sdl_opengl3/nuklear_sdl_gl3.h +++ b/demo/sdl_opengl3/nuklear_sdl_gl3.h @@ -67,6 +67,7 @@ static struct nk_sdl { struct nk_sdl_device ogl; struct nk_context ctx; struct nk_font_atlas atlas; + float time_of_last_frame; } sdl; #ifdef __APPLE__ @@ -192,11 +193,16 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b int display_width, display_height; struct nk_vec2 scale; GLfloat ortho[4][4] = { - {2.0f, 0.0f, 0.0f, 0.0f}, - {0.0f,-2.0f, 0.0f, 0.0f}, - {0.0f, 0.0f,-1.0f, 0.0f}, - {-1.0f,1.0f, 0.0f, 1.0f}, + { 2.0f, 0.0f, 0.0f, 0.0f }, + { 0.0f, -2.0f, 0.0f, 0.0f }, + { 0.0f, 0.0f, -1.0f, 0.0f }, + { -1.0f, 1.0f, 0.0f, 1.0f }, }; + + float now = ((float)SDL_GetTicks64()) / 1000; + sdl.ctx.delta_time_seconds = now - sdl.time_of_last_frame; + sdl.time_of_last_frame = now; + SDL_GetWindowSize(sdl.win, &width, &height); SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height); ortho[0][0] /= (GLfloat)width; diff --git a/demo/sdl_opengles2/nuklear_sdl_gles2.h b/demo/sdl_opengles2/nuklear_sdl_gles2.h index 404d48a..128d263 100644 --- a/demo/sdl_opengles2/nuklear_sdl_gles2.h +++ b/demo/sdl_opengles2/nuklear_sdl_gles2.h @@ -71,6 +71,7 @@ static struct nk_sdl { struct nk_sdl_device ogl; struct nk_context ctx; struct nk_font_atlas atlas; + float time_of_last_frame; } sdl; @@ -180,11 +181,16 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b int display_width, display_height; struct nk_vec2 scale; GLfloat ortho[4][4] = { - {2.0f, 0.0f, 0.0f, 0.0f}, - {0.0f,-2.0f, 0.0f, 0.0f}, - {0.0f, 0.0f,-1.0f, 0.0f}, - {-1.0f,1.0f, 0.0f, 1.0f}, + { 2.0f, 0.0f, 0.0f, 0.0f }, + { 0.0f, -2.0f, 0.0f, 0.0f }, + { 0.0f, 0.0f, -1.0f, 0.0f }, + { -1.0f, 1.0f, 0.0f, 1.0f }, }; + + float now = ((float)SDL_GetTicks64()) / 1000; + sdl.ctx.delta_time_seconds = now - sdl.time_of_last_frame; + sdl.time_of_last_frame = now; + SDL_GetWindowSize(sdl.win, &width, &height); SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height); ortho[0][0] /= (GLfloat)width;