Add delta_time_seconds to a few renderers (#628)

This commit is contained in:
Rob Loach 2024-08-29 22:22:55 -04:00 committed by GitHub
parent 9eeb910d01
commit f7847e6024
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 51 additions and 16 deletions

View File

@ -66,6 +66,7 @@ static struct nk_allegro5 {
int touch_down_id; int touch_down_id;
struct nk_context ctx; struct nk_context ctx;
struct nk_buffer cmds; struct nk_buffer cmds;
float delta_time_seconds_last;
} allegro5; } allegro5;
@ -177,6 +178,11 @@ nk_allegro5_render()
{ {
const struct nk_command *cmd; const struct nk_command *cmd;
/* Update the timer */
float now = (float)al_get_time();
allegro5.ctx.delta_time_seconds = now - allegro5.delta_time_seconds_last;
allegro5.delta_time_seconds_last = now;
al_set_target_backbuffer(allegro5.dsp); al_set_target_backbuffer(allegro5.dsp);
nk_foreach(cmd, &allegro5.ctx) nk_foreach(cmd, &allegro5.ctx)
@ -498,6 +504,7 @@ nk_allegro5_init(NkAllegro5Font *allegro5font, ALLEGRO_DISPLAY *dsp,
allegro5.height = height; allegro5.height = height;
allegro5.is_touch_down = 0; allegro5.is_touch_down = 0;
allegro5.touch_down_id = -1; allegro5.touch_down_id = -1;
allegro5.delta_time_seconds_last = (float)al_get_time();
nk_init_default(&allegro5.ctx, font); nk_init_default(&allegro5.ctx, font);
allegro5.ctx.clip.copy = nk_allegro5_clipboard_copy; allegro5.ctx.clip.copy = nk_allegro5_clipboard_copy;

View File

@ -3,11 +3,11 @@ overview(struct nk_context *ctx)
{ {
/* window flags */ /* window flags */
static nk_bool show_menu = nk_true; static nk_bool show_menu = nk_true;
static nk_flags window_flags = NK_WINDOW_TITLE|NK_WINDOW_BORDER|NK_WINDOW_SCALABLE|NK_WINDOW_MOVABLE|NK_WINDOW_MINIMIZABLE; static nk_flags window_flags = NK_WINDOW_TITLE|NK_WINDOW_BORDER|NK_WINDOW_SCALABLE|NK_WINDOW_MOVABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_SCROLL_AUTO_HIDE;
nk_flags actual_window_flags = 0; nk_flags actual_window_flags = 0;
/* widget flags */ /* widget flags */
static nk_bool disable_widgets = nk_false; static nk_bool disable_widgets = nk_false;
/* popups */ /* popups */
static enum nk_style_header_align header_align = NK_HEADER_RIGHT; static enum nk_style_header_align header_align = NK_HEADER_RIGHT;

View File

@ -79,6 +79,7 @@ static struct nk_glfw {
double last_button_click; double last_button_click;
int is_double_click_down; int is_double_click_down;
struct nk_vec2 double_click_pos; struct nk_vec2 double_click_pos;
float delta_time_seconds_last;
} glfw; } glfw;
NK_INTERN void NK_INTERN void
@ -273,6 +274,8 @@ nk_glfw3_init(GLFWwindow *win, enum nk_glfw_init_state init_state)
glfw.is_double_click_down = nk_false; glfw.is_double_click_down = nk_false;
glfw.double_click_pos = nk_vec2(0, 0); glfw.double_click_pos = nk_vec2(0, 0);
glfw.delta_time_seconds_last = (float)glfwGetTime();
return &glfw.ctx; return &glfw.ctx;
} }
@ -303,6 +306,11 @@ nk_glfw3_new_frame(void)
struct nk_context *ctx = &glfw.ctx; struct nk_context *ctx = &glfw.ctx;
struct GLFWwindow *win = glfw.win; struct GLFWwindow *win = glfw.win;
/* update the timer */
float delta_time_now = (float)glfwGetTime();
glfw.ctx.delta_time_seconds = delta_time_now - glfw.delta_time_seconds_last;
glfw.delta_time_seconds_last = delta_time_now;
glfwGetWindowSize(win, &glfw.width, &glfw.height); glfwGetWindowSize(win, &glfw.width, &glfw.height);
glfwGetFramebufferSize(win, &glfw.display_width, &glfw.display_height); glfwGetFramebufferSize(win, &glfw.display_width, &glfw.display_height);
glfw.fb_scale.x = (float)glfw.display_width/(float)glfw.width; glfw.fb_scale.x = (float)glfw.display_width/(float)glfw.width;

View File

@ -53,6 +53,7 @@ struct nk_glfw {
double last_button_click; double last_button_click;
int is_double_click_down; int is_double_click_down;
struct nk_vec2 double_click_pos; struct nk_vec2 double_click_pos;
float delta_time_seconds_last;
}; };
NK_API struct nk_context* nk_glfw3_init(struct nk_glfw* glfw, GLFWwindow *win, enum nk_glfw_init_state); NK_API struct nk_context* nk_glfw3_init(struct nk_glfw* glfw, GLFWwindow *win, enum nk_glfw_init_state);
@ -389,6 +390,8 @@ nk_glfw3_init(struct nk_glfw* glfw, GLFWwindow *win, enum nk_glfw_init_state ini
glfw->is_double_click_down = nk_false; glfw->is_double_click_down = nk_false;
glfw->double_click_pos = nk_vec2(0, 0); glfw->double_click_pos = nk_vec2(0, 0);
glfw->delta_time_seconds_last = (float)glfwGetTime();
return &glfw->ctx; return &glfw->ctx;
} }
@ -419,6 +422,11 @@ nk_glfw3_new_frame(struct nk_glfw* glfw)
struct nk_context *ctx = &glfw->ctx; struct nk_context *ctx = &glfw->ctx;
struct GLFWwindow *win = glfw->win; struct GLFWwindow *win = glfw->win;
/* update the timer */
float delta_time_now = (float)glfwGetTime();
glfw->ctx.delta_time_seconds = delta_time_now - glfw->delta_time_seconds_last;
glfw->delta_time_seconds_last = delta_time_now;
glfwGetWindowSize(win, &glfw->width, &glfw->height); glfwGetWindowSize(win, &glfw->width, &glfw->height);
glfwGetFramebufferSize(win, &glfw->display_width, &glfw->display_height); glfwGetFramebufferSize(win, &glfw->display_width, &glfw->display_height);
glfw->fb_scale.x = (float)glfw->display_width/(float)glfw->width; glfw->fb_scale.x = (float)glfw->display_width/(float)glfw->width;

View File

@ -379,6 +379,7 @@ static struct nk_glfw {
double last_button_click; double last_button_click;
int is_double_click_down; int is_double_click_down;
struct nk_vec2 double_click_pos; struct nk_vec2 double_click_pos;
float delta_time_seconds_last;
} glfw; } glfw;
struct Mat4f { struct Mat4f {
@ -1254,6 +1255,11 @@ NK_API void nk_glfw3_new_frame(void) {
struct nk_context *ctx = &glfw.ctx; struct nk_context *ctx = &glfw.ctx;
struct GLFWwindow *win = glfw.win; struct GLFWwindow *win = glfw.win;
/* update the timer */
float delta_time_now = (float)glfwGetTime();
glfw.ctx.delta_time_seconds = delta_time_now - glfw.delta_time_seconds_last;
glfw.delta_time_seconds_last = delta_time_now;
nk_input_begin(ctx); nk_input_begin(ctx);
for (i = 0; i < glfw.text_len; ++i) for (i = 0; i < glfw.text_len; ++i)
nk_input_unicode(ctx, glfw.text[i]); nk_input_unicode(ctx, glfw.text[i]);

View File

@ -1033,6 +1033,7 @@ nk_rawfb_render(const struct rawfb_context *rawfb,
const unsigned char enable_clear) const unsigned char enable_clear)
{ {
const struct nk_command *cmd; const struct nk_command *cmd;
if (enable_clear) if (enable_clear)
nk_rawfb_clear(rawfb, clear); nk_rawfb_clear(rawfb, clear);

View File

@ -51,7 +51,7 @@ static struct nk_sdl {
struct nk_sdl_device ogl; struct nk_sdl_device ogl;
struct nk_context ctx; struct nk_context ctx;
struct nk_font_atlas atlas; struct nk_font_atlas atlas;
float time_of_last_frame; Uint64 time_of_last_frame;
} sdl; } sdl;
NK_INTERN void NK_INTERN void
@ -75,8 +75,8 @@ nk_sdl_render(enum nk_anti_aliasing AA)
int display_width, display_height; int display_width, display_height;
struct nk_vec2 scale; struct nk_vec2 scale;
float now = ((float)SDL_GetTicks64()) / 1000; Uint64 now = SDL_GetTicks64();
sdl.ctx.delta_time_seconds = now - sdl.time_of_last_frame; sdl.ctx.delta_time_seconds = (float)(now - sdl.time_of_last_frame) / 1000;
sdl.time_of_last_frame = now; sdl.time_of_last_frame = now;
SDL_GetWindowSize(sdl.win, &width, &height); SDL_GetWindowSize(sdl.win, &width, &height);
@ -217,7 +217,7 @@ nk_sdl_init(SDL_Window *win)
sdl.ctx.clip.paste = nk_sdl_clipboard_paste; sdl.ctx.clip.paste = nk_sdl_clipboard_paste;
sdl.ctx.clip.userdata = nk_handle_ptr(0); sdl.ctx.clip.userdata = nk_handle_ptr(0);
nk_buffer_init_default(&sdl.ogl.cmds); nk_buffer_init_default(&sdl.ogl.cmds);
sdl.time_of_last_frame = ((float)SDL_GetTicks64()) / 1000; sdl.time_of_last_frame = SDL_GetTicks64();
return &sdl.ctx; return &sdl.ctx;
} }

View File

@ -67,7 +67,7 @@ static struct nk_sdl {
struct nk_sdl_device ogl; struct nk_sdl_device ogl;
struct nk_context ctx; struct nk_context ctx;
struct nk_font_atlas atlas; struct nk_font_atlas atlas;
float time_of_last_frame; Uint64 time_of_last_frame;
} sdl; } sdl;
#ifdef __APPLE__ #ifdef __APPLE__
@ -199,8 +199,8 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
{ -1.0f, 1.0f, 0.0f, 1.0f }, { -1.0f, 1.0f, 0.0f, 1.0f },
}; };
float now = ((float)SDL_GetTicks64()) / 1000; Uint64 now = SDL_GetTicks64();
sdl.ctx.delta_time_seconds = now - sdl.time_of_last_frame; sdl.ctx.delta_time_seconds = (float)(now - sdl.time_of_last_frame) / 1000;
sdl.time_of_last_frame = now; sdl.time_of_last_frame = now;
SDL_GetWindowSize(sdl.win, &width, &height); SDL_GetWindowSize(sdl.win, &width, &height);
@ -326,7 +326,7 @@ nk_sdl_init(SDL_Window *win)
sdl.ctx.clip.paste = nk_sdl_clipboard_paste; sdl.ctx.clip.paste = nk_sdl_clipboard_paste;
sdl.ctx.clip.userdata = nk_handle_ptr(0); sdl.ctx.clip.userdata = nk_handle_ptr(0);
nk_sdl_device_create(); nk_sdl_device_create();
sdl.time_of_last_frame = ((float)SDL_GetTicks64()) / 1000; sdl.time_of_last_frame = SDL_GetTicks64();
return &sdl.ctx; return &sdl.ctx;
} }

View File

@ -71,7 +71,7 @@ static struct nk_sdl {
struct nk_sdl_device ogl; struct nk_sdl_device ogl;
struct nk_context ctx; struct nk_context ctx;
struct nk_font_atlas atlas; struct nk_font_atlas atlas;
float time_of_last_frame; Uint64 time_of_last_frame;
} sdl; } sdl;
@ -187,8 +187,8 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
{ -1.0f, 1.0f, 0.0f, 1.0f }, { -1.0f, 1.0f, 0.0f, 1.0f },
}; };
float now = ((float)SDL_GetTicks64()) / 1000; Uint64 now = SDL_GetTicks64();
sdl.ctx.delta_time_seconds = now - sdl.time_of_last_frame; sdl.ctx.delta_time_seconds = (float)(now - sdl.time_of_last_frame) / 1000;
sdl.time_of_last_frame = now; sdl.time_of_last_frame = now;
SDL_GetWindowSize(sdl.win, &width, &height); SDL_GetWindowSize(sdl.win, &width, &height);
@ -326,7 +326,7 @@ nk_sdl_init(SDL_Window *win)
sdl.ctx.clip.paste = nk_sdl_clipboard_paste; sdl.ctx.clip.paste = nk_sdl_clipboard_paste;
sdl.ctx.clip.userdata = nk_handle_ptr(0); sdl.ctx.clip.userdata = nk_handle_ptr(0);
nk_sdl_device_create(); nk_sdl_device_create();
sdl.time_of_last_frame = ((float)SDL_GetTicks64()) / 1000; sdl.time_of_last_frame = SDL_GetTicks64();
return &sdl.ctx; return &sdl.ctx;
} }

View File

@ -64,10 +64,9 @@ static struct nk_sdl {
struct nk_sdl_device ogl; struct nk_sdl_device ogl;
struct nk_context ctx; struct nk_context ctx;
struct nk_font_atlas atlas; struct nk_font_atlas atlas;
Uint64 time_of_last_frame;
} sdl; } sdl;
NK_INTERN void NK_INTERN void
nk_sdl_device_upload_atlas(const void *image, int width, int height) nk_sdl_device_upload_atlas(const void *image, int width, int height)
{ {
@ -113,6 +112,11 @@ nk_sdl_render(enum nk_anti_aliasing AA)
{NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_sdl_vertex, col)}, {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_sdl_vertex, col)},
{NK_VERTEX_LAYOUT_END} {NK_VERTEX_LAYOUT_END}
}; };
Uint64 now = SDL_GetTicks64();
sdl.ctx.delta_time_seconds = (float)(now - sdl.time_of_last_frame) / 1000;
sdl.time_of_last_frame = now;
NK_MEMSET(&config, 0, sizeof(config)); NK_MEMSET(&config, 0, sizeof(config));
config.vertex_layout = vertex_layout; config.vertex_layout = vertex_layout;
config.vertex_size = sizeof(struct nk_sdl_vertex); config.vertex_size = sizeof(struct nk_sdl_vertex);
@ -241,6 +245,7 @@ nk_sdl_init(SDL_Window *win, SDL_Renderer *renderer)
#endif #endif
sdl.win = win; sdl.win = win;
sdl.renderer = renderer; sdl.renderer = renderer;
sdl.time_of_last_frame = SDL_GetTicks64();
nk_init_default(&sdl.ctx, 0); nk_init_default(&sdl.ctx, 0);
sdl.ctx.clip.copy = nk_sdl_clipboard_copy; sdl.ctx.clip.copy = nk_sdl_clipboard_copy;
sdl.ctx.clip.paste = nk_sdl_clipboard_paste; sdl.ctx.clip.paste = nk_sdl_clipboard_paste;