Merge pull request #672 from PROP65/delta

Add delta_time_seconds to X11 and SFML renderers
This commit is contained in:
Rob Loach 2024-08-29 10:50:22 -04:00 committed by GitHub
commit 9eeb910d01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 99 additions and 58 deletions

View File

@ -217,6 +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;
return &sdl.ctx; return &sdl.ctx;
} }

View File

@ -326,6 +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;
return &sdl.ctx; return &sdl.ctx;
} }

View File

@ -326,6 +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;
return &sdl.ctx; return &sdl.ctx;
} }

View File

@ -14,6 +14,7 @@
#define NK_SFML_GL2_H_ #define NK_SFML_GL2_H_
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#include <SFML/System/Clock.hpp>
NK_API struct nk_context* nk_sfml_init(sf::Window* window); NK_API struct nk_context* nk_sfml_init(sf::Window* window);
NK_API void nk_sfml_font_stash_begin(struct nk_font_atlas** atlas); NK_API void nk_sfml_font_stash_begin(struct nk_font_atlas** atlas);
@ -51,6 +52,7 @@ static struct nk_sfml {
struct nk_sfml_device ogl; struct nk_sfml_device ogl;
struct nk_context ctx; struct nk_context ctx;
struct nk_font_atlas atlas; struct nk_font_atlas atlas;
sf::Clock* frame_delta_clock;
} sfml; } sfml;
NK_INTERN void NK_INTERN void
@ -74,6 +76,9 @@ nk_sfml_render(enum nk_anti_aliasing AA)
int window_width = sfml.window->getSize().x; int window_width = sfml.window->getSize().x;
int window_height = sfml.window->getSize().y; int window_height = sfml.window->getSize().y;
sfml.ctx.delta_time_seconds = (float)((double)sfml.frame_delta_clock->getElapsedTime().asMicroseconds() / 1000000);
sfml.frame_delta_clock->restart();
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT); glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
@ -229,6 +234,7 @@ nk_sfml_init(sf::Window* window)
sfml.ctx.clip.paste = nk_sfml_clipboard_paste; sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
sfml.ctx.clip.userdata = nk_handle_ptr(0); sfml.ctx.clip.userdata = nk_handle_ptr(0);
nk_buffer_init_default(&sfml.ogl.cmds); nk_buffer_init_default(&sfml.ogl.cmds);
sfml.frame_delta_clock = new sf::Clock();
return &sfml.ctx; return &sfml.ctx;
} }
@ -334,8 +340,8 @@ nk_sfml_handle_event(sf::Event* evt)
} else if(evt->type == sf::Event::TouchBegan || evt->type == sf::Event::TouchEnded) { } else if(evt->type == sf::Event::TouchBegan || evt->type == sf::Event::TouchEnded) {
int down = evt->type == sf::Event::TouchBegan; int down = evt->type == sf::Event::TouchBegan;
const int x = evt->touch.x, y = evt->touch.y; const int x = evt->touch.x, y = evt->touch.y;
ctx->input.mouse.pos.x = x; ctx->input.mouse.pos.x = x;
ctx->input.mouse.pos.y = y; ctx->input.mouse.pos.y = y;
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
return 1; return 1;
} else if(evt->type == sf::Event::TouchMoved) { } else if(evt->type == sf::Event::TouchMoved) {
@ -363,6 +369,7 @@ void nk_sfml_shutdown(void)
nk_free(&sfml.ctx); nk_free(&sfml.ctx);
glDeleteTextures(1, &dev->font_tex); glDeleteTextures(1, &dev->font_tex);
nk_buffer_free(&dev->cmds); nk_buffer_free(&dev->cmds);
delete sfml.frame_delta_clock;
memset(&sfml, 0, sizeof(sfml)); memset(&sfml, 0, sizeof(sfml));
} }

View File

@ -66,6 +66,7 @@ static struct nk_sfml {
struct nk_sfml_device ogl; struct nk_sfml_device ogl;
struct nk_context ctx; struct nk_context ctx;
struct nk_font_atlas atlas; struct nk_font_atlas atlas;
sf::Clock* frame_delta_clock;
} sfml; } sfml;
#ifdef __APPLE__ #ifdef __APPLE__
@ -197,11 +198,15 @@ nk_sfml_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_
int window_width = sfml.window->getSize().x; int window_width = sfml.window->getSize().x;
int window_height = sfml.window->getSize().y; int window_height = sfml.window->getSize().y;
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 },
}; };
sfml.ctx.delta_time_seconds = (float)((double)sfml.frame_delta_clock->getElapsedTime().asMicroseconds() / 1000000);
sfml.frame_delta_clock->restart();
ortho[0][0] /= (GLfloat)window_width; ortho[0][0] /= (GLfloat)window_width;
ortho[1][1] /= (GLfloat)window_height; ortho[1][1] /= (GLfloat)window_height;
@ -336,6 +341,7 @@ nk_sfml_init(sf::Window* window)
sfml.ctx.clip.paste = nk_sfml_clipboard_paste; sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
sfml.ctx.clip.userdata = nk_handle_ptr(0); sfml.ctx.clip.userdata = nk_handle_ptr(0);
nk_sfml_device_create(); nk_sfml_device_create();
sfml.frame_delta_clock = new sf::Clock();
return &sfml.ctx; return &sfml.ctx;
} }
@ -451,10 +457,10 @@ nk_sfml_handle_event(sf::Event* evt)
} else nk_input_motion(ctx, evt->touch.x, evt->touch.y); } else nk_input_motion(ctx, evt->touch.x, evt->touch.y);
return 1; return 1;
} else if(evt->type == sf::Event::TextEntered) { } else if(evt->type == sf::Event::TextEntered) {
/* 8 ~ backspace */ /* 8 ~ backspace */
if (evt->text.unicode != 8) { if (evt->text.unicode != 8) {
nk_input_unicode(ctx, evt->text.unicode); nk_input_unicode(ctx, evt->text.unicode);
} }
return 1; return 1;
} else if(evt->type == sf::Event::MouseWheelScrolled) { } else if(evt->type == sf::Event::MouseWheelScrolled) {
nk_input_scroll(ctx, nk_vec2(0,evt->mouseWheelScroll.delta)); nk_input_scroll(ctx, nk_vec2(0,evt->mouseWheelScroll.delta));
@ -469,6 +475,7 @@ void nk_sfml_shutdown()
nk_font_atlas_clear(&sfml.atlas); nk_font_atlas_clear(&sfml.atlas);
nk_free(&sfml.ctx); nk_free(&sfml.ctx);
nk_sfml_device_destroy(); nk_sfml_device_destroy();
delete sfml.frame_delta_clock;
memset(&sfml, 0, sizeof(sfml)); memset(&sfml, 0, sizeof(sfml));
} }

View File

@ -68,10 +68,10 @@ NK_API void nk_xfont_del(Display *dpy, XFont *font);
#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
typedef struct XSurface XSurface; typedef struct XSurface XSurface;
@ -112,15 +112,16 @@ static struct {
Cursor cursor; Cursor cursor;
Display *dpy; Display *dpy;
Window root; Window root;
long last_button_click; double last_button_click;
double time_of_last_frame;
} xlib; } xlib;
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 unsigned long 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); xlib.surf = nk_xsurf_create(screen, w, h);
nk_init_default(&xlib.ctx, font); nk_init_default(&xlib.ctx, font);
xlib.time_of_last_frame = nk_get_time();
return &xlib.ctx; 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; 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() - 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) 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);
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); } 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)
@ -905,6 +907,10 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
struct nk_context *ctx = &xlib.ctx; struct nk_context *ctx = &xlib.ctx;
XSurface *surf = xlib.surf; 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_xsurf_clear(xlib.surf, nk_color_from_byte(&clear.r));
nk_foreach(cmd, &xlib.ctx) nk_foreach(cmd, &xlib.ctx)
{ {

View File

@ -46,10 +46,10 @@ NK_API void nk_x11_shutdown(void);
#include <GL/gl.h> #include <GL/gl.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
struct nk_x11_vertex { struct nk_x11_vertex {
@ -71,15 +71,16 @@ 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;
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 void NK_INTERN void
@ -102,6 +103,10 @@ nk_x11_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
int width, height; int width, height;
XWindowAttributes attr; XWindowAttributes attr;
double now = nk_get_time();
x11.ctx.delta_time_seconds = now - x11.time_of_last_frame;
x11.time_of_last_frame = now;
NK_UNUSED(max_vertex_buffer); NK_UNUSED(max_vertex_buffer);
NK_UNUSED(max_element_buffer); NK_UNUSED(max_element_buffer);
@ -311,10 +316,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)
@ -363,6 +368,7 @@ nk_x11_init(Display *dpy, Window win)
nk_buffer_init_default(&x11.ogl.cmds); nk_buffer_init_default(&x11.ogl.cmds);
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;
} }

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;
} }

View File

@ -76,10 +76,10 @@ NK_API void nk_xfont_del(Display *dpy, XFont *font);
#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
typedef struct XSurface XSurface; typedef struct XSurface XSurface;
@ -131,15 +131,16 @@ static struct {
Visual *vis; Visual *vis;
Colormap cmap; Colormap cmap;
#endif #endif
long last_button_click; double last_button_click;
double time_of_last_frame;
} xlib; } xlib;
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 unsigned long NK_INTERN unsigned long
@ -182,9 +183,9 @@ nk_xsurf_resize(XSurface *surf, unsigned int w, unsigned int h)
surf->drawable = XCreatePixmap(surf->dpy, surf->root, w, h, surf->drawable = XCreatePixmap(surf->dpy, surf->root, w, h,
(unsigned int)DefaultDepth(surf->dpy, surf->screen)); (unsigned int)DefaultDepth(surf->dpy, surf->screen));
#ifdef NK_XLIB_USE_XFT #ifdef NK_XLIB_USE_XFT
XftDrawChange(surf->ftdraw, surf->drawable); XftDrawChange(surf->ftdraw, surf->drawable);
#endif #endif
return; return;
} }
NK_INTERN void NK_INTERN void
@ -198,9 +199,9 @@ nk_xsurf_scissor(XSurface *surf, float x, float y, float w, float h)
XSetClipRectangles(surf->dpy, surf->gc, 0, 0, &clip_rect, 1, Unsorted); XSetClipRectangles(surf->dpy, surf->gc, 0, 0, &clip_rect, 1, Unsorted);
#ifdef NK_XLIB_USE_XFT #ifdef NK_XLIB_USE_XFT
XftDrawSetClipRectangles(surf->ftdraw, 0, 0, &clip_rect, 1); XftDrawSetClipRectangles(surf->ftdraw, 0, 0, &clip_rect, 1);
#endif #endif
return; return;
} }
NK_INTERN void NK_INTERN void
@ -465,13 +466,13 @@ nk_xsurf_draw_text(XSurface *surf, short x, short y, const char *text, int len,
xrc.alpha = cfg.a * 257; xrc.alpha = cfg.a * 257;
XftColorAllocValue(surf->dpy, xlib.vis, xlib.cmap, &xrc, &color); XftColorAllocValue(surf->dpy, xlib.vis, xlib.cmap, &xrc, &color);
XftDrawStringUtf8(surf->ftdraw, &color, font->ft, tx, ty, (FcChar8*)text, len); XftDrawStringUtf8(surf->ftdraw, &color, font->ft, tx, ty, (FcChar8*)text, len);
XftColorFree(surf->dpy, xlib.vis, xlib.cmap, &color); XftColorFree(surf->dpy, xlib.vis, xlib.cmap, &color);
#else #else
XSetForeground(surf->dpy, surf->gc, fg); XSetForeground(surf->dpy, surf->gc, fg);
if(font->set) XmbDrawString(surf->dpy,surf->drawable, font->set, surf->gc, tx, ty, (const char*)text, (int)len); if(font->set) XmbDrawString(surf->dpy,surf->drawable, font->set, surf->gc, tx, ty, (const char*)text, (int)len);
else XDrawString(surf->dpy, surf->drawable, surf->gc, tx, ty, (const char*)text, (int)len); else XDrawString(surf->dpy, surf->drawable, surf->gc, tx, ty, (const char*)text, (int)len);
#endif #endif
return; return;
} }
@ -610,7 +611,7 @@ NK_INTERN void
nk_xsurf_del(XSurface *surf) nk_xsurf_del(XSurface *surf)
{ {
#ifdef NK_XLIB_USE_XFT #ifdef NK_XLIB_USE_XFT
XftDrawDestroy(surf->ftdraw); XftDrawDestroy(surf->ftdraw);
#endif #endif
XFreePixmap(surf->dpy, surf->drawable); XFreePixmap(surf->dpy, surf->drawable);
XFreeGC(surf->dpy, surf->gc); XFreeGC(surf->dpy, surf->gc);
@ -674,8 +675,8 @@ nk_xfont_get_text_width(nk_handle handle, float height, const char *text, int le
NK_UNUSED(height); NK_UNUSED(height);
if(!font || !text) if(!font || !text)
return 0; return 0;
XftTextExtentsUtf8(xlib.dpy, font->ft, (FcChar8*)text, len, &g); XftTextExtentsUtf8(xlib.dpy, font->ft, (FcChar8*)text, len, &g);
return g.xOff; return g.xOff;
@ -684,8 +685,8 @@ nk_xfont_get_text_width(nk_handle handle, float height, const char *text, int le
NK_UNUSED(height); NK_UNUSED(height);
if(!font || !text) if(!font || !text)
return 0; return 0;
if(font->set) { if(font->set) {
XmbTextExtents(font->set, (const char*)text, len, NULL, &r); XmbTextExtents(font->set, (const char*)text, len, NULL, &r);
@ -702,7 +703,7 @@ nk_xfont_del(Display *dpy, XFont *font)
{ {
if(!font) return; if(!font) return;
#ifdef NK_XLIB_USE_XFT #ifdef NK_XLIB_USE_XFT
XftFontClose(dpy, font->ft); XftFontClose(dpy, font->ft);
#else #else
if(font->set) if(font->set)
XFreeFontSet(dpy, font->set); XFreeFontSet(dpy, font->set);
@ -747,6 +748,7 @@ nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
XFreePixmap(dpy, blank);} XFreePixmap(dpy, blank);}
xlib.surf = nk_xsurf_create(screen, w, h); xlib.surf = nk_xsurf_create(screen, w, h);
nk_init_default(&xlib.ctx, font); nk_init_default(&xlib.ctx, font);
xlib.time_of_last_frame = nk_get_time();
return &xlib.ctx; return &xlib.ctx;
} }
@ -878,10 +880,10 @@ nk_xlib_handle_event(Display *dpy, int screen, Window win, 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() - 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) 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);
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); } 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)
@ -990,6 +992,10 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
struct nk_context *ctx = &xlib.ctx; struct nk_context *ctx = &xlib.ctx;
XSurface *surf = xlib.surf; 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_xsurf_clear(xlib.surf, nk_color_from_byte(&clear.r));
nk_foreach(cmd, &xlib.ctx) nk_foreach(cmd, &xlib.ctx)
{ {