xlib_gl3: add delta_time_seconds

This commit is contained in:
PROP 65 2024-08-29 03:11:10 +00:00 committed by GitHub
parent d043803932
commit 42b9ea7943
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 12 deletions

View File

@ -50,10 +50,10 @@ NK_API void nk_x11_device_destroy(void);
#include <GL/glx.h> #include <GL/glx.h>
#ifndef NK_X11_DOUBLE_CLICK_LO #ifndef NK_X11_DOUBLE_CLICK_LO
#define NK_X11_DOUBLE_CLICK_LO 20 #define NK_X11_DOUBLE_CLICK_LO 0.02
#endif #endif
#ifndef NK_X11_DOUBLE_CLICK_HI #ifndef NK_X11_DOUBLE_CLICK_HI
#define NK_X11_DOUBLE_CLICK_HI 200 #define NK_X11_DOUBLE_CLICK_HI 0.20
#endif #endif
#ifdef NK_XLIB_LOAD_OPENGL_EXTENSIONS #ifdef NK_XLIB_LOAD_OPENGL_EXTENSIONS
@ -194,7 +194,8 @@ static struct nk_x11 {
Cursor cursor; Cursor cursor;
Display *dpy; Display *dpy;
Window win; Window win;
long last_button_click; double last_button_click;
double time_of_last_frame;
} x11; } x11;
#ifdef __APPLE__ #ifdef __APPLE__
@ -206,12 +207,12 @@ static struct nk_x11 {
#ifdef NK_XLIB_LOAD_OPENGL_EXTENSIONS #ifdef NK_XLIB_LOAD_OPENGL_EXTENSIONS
#include <GL/glx.h> #include <GL/glx.h>
NK_INTERN long NK_INTERN double
nk_timestamp(void) nk_get_time(void)
{ {
struct timeval tv; struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) return 0; 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 int NK_INTERN int
@ -480,11 +481,15 @@ nk_x11_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
XWindowAttributes attr; XWindowAttributes attr;
struct nk_x11_device *dev = &x11.ogl; struct nk_x11_device *dev = &x11.ogl;
GLfloat ortho[4][4] = { GLfloat ortho[4][4] = {
{2.0f, 0.0f, 0.0f, 0.0f}, { 2.0f, 0.0f, 0.0f, 0.0f },
{0.0f,-2.0f, 0.0f, 0.0f}, { 0.0f, -2.0f, 0.0f, 0.0f },
{0.0f, 0.0f,-1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f, 0.0f },
{-1.0f,1.0f, 0.0f, 1.0f}, { -1.0f, 1.0f, 0.0f, 1.0f },
}; };
double now = nk_get_time();
x11.ctx.delta_time_seconds = now - x11.time_of_last_frame;
x11.time_of_last_frame = now;
XGetWindowAttributes(x11.dpy, x11.win, &attr); XGetWindowAttributes(x11.dpy, x11.win, &attr);
width = attr.width; width = attr.width;
height = attr.height; height = attr.height;
@ -678,10 +683,10 @@ nk_x11_handle_event(XEvent *evt)
const int x = evt->xbutton.x, y = evt->xbutton.y; const int x = evt->xbutton.x, y = evt->xbutton.y;
if (evt->xbutton.button == Button1) { if (evt->xbutton.button == Button1) {
if (down) { /* Double-Click Button handler */ if (down) { /* Double-Click Button handler */
long dt = nk_timestamp() - x11.last_button_click; float dt = nk_get_time() - x11.last_button_click;
if (dt > NK_X11_DOUBLE_CLICK_LO && dt < NK_X11_DOUBLE_CLICK_HI) if (dt > NK_X11_DOUBLE_CLICK_LO && dt < NK_X11_DOUBLE_CLICK_HI)
nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_true); nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_true);
x11.last_button_click = nk_timestamp(); x11.last_button_click = nk_get_time();
} else nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_false); } else nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_false);
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
} else if (evt->xbutton.button == Button2) } else if (evt->xbutton.button == Button2)
@ -730,6 +735,7 @@ nk_x11_init(Display *dpy, Window win)
XFreePixmap(dpy, blank);} XFreePixmap(dpy, blank);}
nk_init_default(&x11.ctx, 0); nk_init_default(&x11.ctx, 0);
x11.time_of_last_frame = nk_get_time();
return &x11.ctx; return &x11.ctx;
} }