Skip to content

Commit

Permalink
Merge pull request #672 from PROP65/delta
Browse files Browse the repository at this point in the history
Add delta_time_seconds to X11 and SFML renderers
  • Loading branch information
RobLoach authored Aug 29, 2024
2 parents b3b4008 + 751d2bf commit 9eeb910
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 58 deletions.
1 change: 1 addition & 0 deletions demo/sdl_opengl2/nuklear_sdl_gl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ nk_sdl_init(SDL_Window *win)
sdl.ctx.clip.paste = nk_sdl_clipboard_paste;
sdl.ctx.clip.userdata = nk_handle_ptr(0);
nk_buffer_init_default(&sdl.ogl.cmds);
sdl.time_of_last_frame = ((float)SDL_GetTicks64()) / 1000;
return &sdl.ctx;
}

Expand Down
1 change: 1 addition & 0 deletions demo/sdl_opengl3/nuklear_sdl_gl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ nk_sdl_init(SDL_Window *win)
sdl.ctx.clip.paste = nk_sdl_clipboard_paste;
sdl.ctx.clip.userdata = nk_handle_ptr(0);
nk_sdl_device_create();
sdl.time_of_last_frame = ((float)SDL_GetTicks64()) / 1000;
return &sdl.ctx;
}

Expand Down
1 change: 1 addition & 0 deletions demo/sdl_opengles2/nuklear_sdl_gles2.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ nk_sdl_init(SDL_Window *win)
sdl.ctx.clip.paste = nk_sdl_clipboard_paste;
sdl.ctx.clip.userdata = nk_handle_ptr(0);
nk_sdl_device_create();
sdl.time_of_last_frame = ((float)SDL_GetTicks64()) / 1000;
return &sdl.ctx;
}

Expand Down
11 changes: 9 additions & 2 deletions demo/sfml_opengl2/nuklear_sfml_gl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define NK_SFML_GL2_H_

#include <SFML/Window.hpp>
#include <SFML/System/Clock.hpp>

NK_API struct nk_context* nk_sfml_init(sf::Window* window);
NK_API void nk_sfml_font_stash_begin(struct nk_font_atlas** atlas);
Expand Down Expand Up @@ -51,6 +52,7 @@ static struct nk_sfml {
struct nk_sfml_device ogl;
struct nk_context ctx;
struct nk_font_atlas atlas;
sf::Clock* frame_delta_clock;
} sfml;

NK_INTERN void
Expand All @@ -74,6 +76,9 @@ nk_sfml_render(enum nk_anti_aliasing AA)
int window_width = sfml.window->getSize().x;
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);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
Expand Down Expand Up @@ -229,6 +234,7 @@ nk_sfml_init(sf::Window* window)
sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
sfml.ctx.clip.userdata = nk_handle_ptr(0);
nk_buffer_init_default(&sfml.ogl.cmds);
sfml.frame_delta_clock = new sf::Clock();
return &sfml.ctx;
}

Expand Down Expand Up @@ -334,8 +340,8 @@ nk_sfml_handle_event(sf::Event* evt)
} else if(evt->type == sf::Event::TouchBegan || evt->type == sf::Event::TouchEnded) {
int down = evt->type == sf::Event::TouchBegan;
const int x = evt->touch.x, y = evt->touch.y;
ctx->input.mouse.pos.x = x;
ctx->input.mouse.pos.y = y;
ctx->input.mouse.pos.x = x;
ctx->input.mouse.pos.y = y;
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
return 1;
} else if(evt->type == sf::Event::TouchMoved) {
Expand Down Expand Up @@ -363,6 +369,7 @@ void nk_sfml_shutdown(void)
nk_free(&sfml.ctx);
glDeleteTextures(1, &dev->font_tex);
nk_buffer_free(&dev->cmds);
delete sfml.frame_delta_clock;
memset(&sfml, 0, sizeof(sfml));
}

Expand Down
23 changes: 15 additions & 8 deletions demo/sfml_opengl3/nuklear_sfml_gl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static struct nk_sfml {
struct nk_sfml_device ogl;
struct nk_context ctx;
struct nk_font_atlas atlas;
sf::Clock* frame_delta_clock;
} sfml;

#ifdef __APPLE__
Expand Down Expand Up @@ -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_height = sfml.window->getSize().y;
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 },
};

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[1][1] /= (GLfloat)window_height;

Expand Down Expand Up @@ -336,6 +341,7 @@ nk_sfml_init(sf::Window* window)
sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
sfml.ctx.clip.userdata = nk_handle_ptr(0);
nk_sfml_device_create();
sfml.frame_delta_clock = new sf::Clock();
return &sfml.ctx;
}

Expand Down Expand Up @@ -451,10 +457,10 @@ nk_sfml_handle_event(sf::Event* evt)
} else nk_input_motion(ctx, evt->touch.x, evt->touch.y);
return 1;
} else if(evt->type == sf::Event::TextEntered) {
/* 8 ~ backspace */
if (evt->text.unicode != 8) {
nk_input_unicode(ctx, evt->text.unicode);
}
/* 8 ~ backspace */
if (evt->text.unicode != 8) {
nk_input_unicode(ctx, evt->text.unicode);
}
return 1;
} else if(evt->type == sf::Event::MouseWheelScrolled) {
nk_input_scroll(ctx, nk_vec2(0,evt->mouseWheelScroll.delta));
Expand All @@ -469,6 +475,7 @@ void nk_sfml_shutdown()
nk_font_atlas_clear(&sfml.atlas);
nk_free(&sfml.ctx);
nk_sfml_device_destroy();
delete sfml.frame_delta_clock;
memset(&sfml, 0, sizeof(sfml));
}

Expand Down
22 changes: 14 additions & 8 deletions demo/x11/nuklear_xlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down
22 changes: 14 additions & 8 deletions demo/x11_opengl2/nuklear_xlib_gl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ NK_API void nk_x11_shutdown(void);
#include <GL/gl.h>

#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

struct nk_x11_vertex {
Expand All @@ -71,15 +71,16 @@ static struct nk_x11 {
Cursor cursor;
Display *dpy;
Window win;
long last_button_click;
double last_button_click;
double time_of_last_frame;
} x11;

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 void
Expand All @@ -102,6 +103,10 @@ nk_x11_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
int width, height;
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_element_buffer);

Expand Down Expand Up @@ -311,10 +316,10 @@ nk_x11_handle_event(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() - 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)
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);
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
} else if (evt->xbutton.button == Button2)
Expand Down Expand Up @@ -363,6 +368,7 @@ nk_x11_init(Display *dpy, Window win)

nk_buffer_init_default(&x11.ogl.cmds);
nk_init_default(&x11.ctx, 0);
x11.time_of_last_frame = nk_get_time();
return &x11.ctx;
}

Expand Down
30 changes: 18 additions & 12 deletions demo/x11_opengl3/nuklear_xlib_gl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ NK_API void nk_x11_device_destroy(void);
#include <GL/glx.h>

#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

#ifdef NK_XLIB_LOAD_OPENGL_EXTENSIONS
Expand Down Expand Up @@ -194,7 +194,8 @@ static struct nk_x11 {
Cursor cursor;
Display *dpy;
Window win;
long last_button_click;
double last_button_click;
double time_of_last_frame;
} x11;

#ifdef __APPLE__
Expand All @@ -206,12 +207,12 @@ static struct nk_x11 {
#ifdef NK_XLIB_LOAD_OPENGL_EXTENSIONS
#include <GL/glx.h>

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 int
Expand Down Expand Up @@ -480,11 +481,15 @@ nk_x11_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
XWindowAttributes attr;
struct nk_x11_device *dev = &x11.ogl;
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 },
};
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);
width = attr.width;
height = attr.height;
Expand Down Expand Up @@ -678,10 +683,10 @@ nk_x11_handle_event(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() - 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)
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);
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
} else if (evt->xbutton.button == Button2)
Expand Down Expand Up @@ -730,6 +735,7 @@ nk_x11_init(Display *dpy, Window win)
XFreePixmap(dpy, blank);}

nk_init_default(&x11.ctx, 0);
x11.time_of_last_frame = nk_get_time();
return &x11.ctx;
}

Expand Down
Loading

0 comments on commit 9eeb910

Please sign in to comment.