xlib: add delta_time_seconds

This commit is contained in:
PROP 65 2024-08-29 03:13:45 +00:00 committed by GitHub
parent c3fe64a487
commit 4bfc027e08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -68,10 +68,10 @@ NK_API void nk_xfont_del(Display *dpy, XFont *font);
#ifndef NK_X11_DOUBLE_CLICK_LO
#define NK_X11_DOUBLE_CLICK_LO 20
#define NK_X11_DOUBLE_CLICK_LO 0.02
#endif
#ifndef NK_X11_DOUBLE_CLICK_HI
#define NK_X11_DOUBLE_CLICK_HI 200
#define NK_X11_DOUBLE_CLICK_HI 0.20
#endif
typedef struct XSurface XSurface;
@ -112,15 +112,16 @@ static struct {
Cursor cursor;
Display *dpy;
Window root;
long last_button_click;
double last_button_click;
double time_of_last_frame;
} xlib;
NK_INTERN long
nk_timestamp(void)
NK_INTERN double
nk_get_time(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) return 0;
return (long)((long)tv.tv_sec * 1000 + (long)tv.tv_usec/1000);
return ((double)tv.tv_sec + (double)tv.tv_usec/1000000);
}
NK_INTERN unsigned long
@ -662,6 +663,7 @@ nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
xlib.surf = nk_xsurf_create(screen, w, h);
nk_init_default(&xlib.ctx, font);
xlib.time_of_last_frame = nk_get_time();
return &xlib.ctx;
}
@ -793,10 +795,10 @@ nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt)
const int x = evt->xbutton.x, y = evt->xbutton.y;
if (evt->xbutton.button == Button1) {
if (down) { /* Double-Click Button handler */
long dt = nk_timestamp() - xlib.last_button_click;
float dt = nk_get_time() - xlib.last_button_click;
if (dt > NK_X11_DOUBLE_CLICK_LO && dt < NK_X11_DOUBLE_CLICK_HI)
nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_true);
xlib.last_button_click = nk_timestamp();
xlib.last_button_click = nk_get_time();
} else nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_false);
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
} else if (evt->xbutton.button == Button2)
@ -905,6 +907,10 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
struct nk_context *ctx = &xlib.ctx;
XSurface *surf = xlib.surf;
double now = nk_get_time();
xlib.ctx.delta_time_seconds = now - xlib.time_of_last_frame;
xlib.time_of_last_frame = now;
nk_xsurf_clear(xlib.surf, nk_color_from_byte(&clear.r));
nk_foreach(cmd, &xlib.ctx)
{