nuklear_glfw_gl2.h
Go to the documentation of this file.
1 /*
2  * Nuklear - v1.32.0 - public domain
3  * no warrenty implied; use at your own risk.
4  * authored from 2015-2017 by Micha Mettke
5  */
6 /*
7  * ==============================================================
8  *
9  * API
10  *
11  * ===============================================================
12  */
13 #ifndef NK_GLFW_GL2_H_
14 #define NK_GLFW_GL2_H_
15 
16 #include <GLFW/glfw3.h>
17 
21 };
23 NK_API void nk_glfw3_font_stash_begin(struct nk_font_atlas **atlas);
25 
26 NK_API void nk_glfw3_new_frame(void);
28 NK_API void nk_glfw3_shutdown(void);
29 
30 NK_API void nk_glfw3_char_callback(GLFWwindow *win, unsigned int codepoint);
31 NK_API void nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff);
32 
33 #endif
34 
35 /*
36  * ==============================================================
37  *
38  * IMPLEMENTATION
39  *
40  * ===============================================================
41  */
42 #ifdef NK_GLFW_GL2_IMPLEMENTATION
43 
44 #ifndef NK_GLFW_TEXT_MAX
45 #define NK_GLFW_TEXT_MAX 256
46 #endif
47 #ifndef NK_GLFW_DOUBLE_CLICK_LO
48 #define NK_GLFW_DOUBLE_CLICK_LO 0.02
49 #endif
50 #ifndef NK_GLFW_DOUBLE_CLICK_HI
51 #define NK_GLFW_DOUBLE_CLICK_HI 0.2
52 #endif
53 
54 struct nk_glfw_device {
55  struct nk_buffer cmds;
56  struct nk_draw_null_texture null;
57  GLuint font_tex;
58 };
59 
60 struct nk_glfw_vertex {
61  float position[2];
62  float uv[2];
63  nk_byte col[4];
64 };
65 
66 static struct nk_glfw {
67  GLFWwindow *win;
68  int width, height;
69  int display_width, display_height;
70  struct nk_glfw_device ogl;
71  struct nk_context ctx;
72  struct nk_font_atlas atlas;
73  struct nk_vec2 fb_scale;
74  unsigned int text[NK_GLFW_TEXT_MAX];
75  int text_len;
76  struct nk_vec2 scroll;
77  double last_button_click;
78  int is_double_click_down;
79  struct nk_vec2 double_click_pos;
80 } glfw;
81 
82 NK_INTERN void
83 nk_glfw3_device_upload_atlas(const void *image, int width, int height)
84 {
85  struct nk_glfw_device *dev = &glfw.ogl;
86  glGenTextures(1, &dev->font_tex);
87  glBindTexture(GL_TEXTURE_2D, dev->font_tex);
90  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
91  GL_RGBA, GL_UNSIGNED_BYTE, image);
92 }
93 
94 NK_API void
96 {
97  /* setup global state */
98  struct nk_glfw_device *dev = &glfw.ogl;
106 
107  /* setup viewport/project */
108  glViewport(0,0,(GLsizei)glfw.display_width,(GLsizei)glfw.display_height);
110  glPushMatrix();
111  glLoadIdentity();
112  glOrtho(0.0f, glfw.width, glfw.height, 0.0f, -1.0f, 1.0f);
114  glPushMatrix();
115  glLoadIdentity();
116 
120  {
121  GLsizei vs = sizeof(struct nk_glfw_vertex);
122  size_t vp = offsetof(struct nk_glfw_vertex, position);
123  size_t vt = offsetof(struct nk_glfw_vertex, uv);
124  size_t vc = offsetof(struct nk_glfw_vertex, col);
125 
126  /* convert from command queue into draw list and draw to screen */
127  const struct nk_draw_command *cmd;
128  const nk_draw_index *offset = NULL;
129  struct nk_buffer vbuf, ebuf;
130 
131  /* fill convert configuration */
132  struct nk_convert_config config;
133  static const struct nk_draw_vertex_layout_element vertex_layout[] = {
134  {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_glfw_vertex, position)},
135  {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_glfw_vertex, uv)},
136  {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_glfw_vertex, col)},
137  {NK_VERTEX_LAYOUT_END}
138  };
139  NK_MEMSET(&config, 0, sizeof(config));
140  config.vertex_layout = vertex_layout;
141  config.vertex_size = sizeof(struct nk_glfw_vertex);
142  config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
143  config.null = dev->null;
144  config.circle_segment_count = 22;
145  config.curve_segment_count = 22;
146  config.arc_segment_count = 22;
147  config.global_alpha = 1.0f;
148  config.shape_AA = AA;
149  config.line_AA = AA;
150 
151  /* convert shapes into vertexes */
152  nk_buffer_init_default(&vbuf);
153  nk_buffer_init_default(&ebuf);
154  nk_convert(&glfw.ctx, &dev->cmds, &vbuf, &ebuf, &config);
155 
156  /* setup vertex buffer pointer */
157  {const void *vertices = nk_buffer_memory_const(&vbuf);
158  glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp));
159  glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt));
160  glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));}
161 
162  /* iterate over and execute each draw command */
163  offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf);
164  nk_draw_foreach(cmd, &glfw.ctx, &dev->cmds)
165  {
166  if (!cmd->elem_count) continue;
167  glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
168  glScissor(
169  (GLint)(cmd->clip_rect.x * glfw.fb_scale.x),
170  (GLint)((glfw.height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * glfw.fb_scale.y),
171  (GLint)(cmd->clip_rect.w * glfw.fb_scale.x),
172  (GLint)(cmd->clip_rect.h * glfw.fb_scale.y));
173  glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
174  offset += cmd->elem_count;
175  }
176  nk_clear(&glfw.ctx);
177  nk_buffer_free(&vbuf);
178  nk_buffer_free(&ebuf);
179  }
180 
181  /* default OpenGL state */
185 
191 
194  glPopMatrix();
196  glPopMatrix();
197  glPopAttrib();
198 }
199 
200 NK_API void
201 nk_glfw3_char_callback(GLFWwindow *win, unsigned int codepoint)
202 {
203  (void)win;
204  if (glfw.text_len < NK_GLFW_TEXT_MAX)
205  glfw.text[glfw.text_len++] = codepoint;
206 }
207 
208 NK_API void
209 nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff)
210 {
211  (void)win; (void)xoff;
212  glfw.scroll.x += (float)xoff;
213  glfw.scroll.y += (float)yoff;
214 }
215 
216 NK_API void
217 nk_glfw3_mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
218 {
219  double x, y;
220  if (button != GLFW_MOUSE_BUTTON_LEFT) return;
221  glfwGetCursorPos(window, &x, &y);
222  if (action == GLFW_PRESS) {
223  double dt = glfwGetTime() - glfw.last_button_click;
224  if (dt > NK_GLFW_DOUBLE_CLICK_LO && dt < NK_GLFW_DOUBLE_CLICK_HI) {
225  glfw.is_double_click_down = nk_true;
226  glfw.double_click_pos = nk_vec2((float)x, (float)y);
227  }
228  glfw.last_button_click = glfwGetTime();
229  } else glfw.is_double_click_down = nk_false;
230 }
231 
232 NK_INTERN void
233 nk_glfw3_clipbard_paste(nk_handle usr, struct nk_text_edit *edit)
234 {
235  const char *text = glfwGetClipboardString(glfw.win);
236  if (text) nk_textedit_paste(edit, text, nk_strlen(text));
237  (void)usr;
238 }
239 
240 NK_INTERN void
241 nk_glfw3_clipbard_copy(nk_handle usr, const char *text, int len)
242 {
243  char *str = 0;
244  (void)usr;
245  if (!len) return;
246  str = (char*)malloc((size_t)len+1);
247  if (!str) return;
248  NK_MEMCPY(str, text, (size_t)len);
249  str[len] = '\0';
250  glfwSetClipboardString(glfw.win, str);
251  free(str);
252 }
253 
254 NK_API struct nk_context*
255 nk_glfw3_init(GLFWwindow *win, enum nk_glfw_init_state init_state)
256 {
257  glfw.win = win;
258  if (init_state == NK_GLFW3_INSTALL_CALLBACKS) {
261  glfwSetMouseButtonCallback(win, nk_glfw3_mouse_button_callback);
262  }
263  nk_init_default(&glfw.ctx, 0);
264  glfw.ctx.clip.copy = nk_glfw3_clipbard_copy;
265  glfw.ctx.clip.paste = nk_glfw3_clipbard_paste;
266  glfw.ctx.clip.userdata = nk_handle_ptr(0);
267  nk_buffer_init_default(&glfw.ogl.cmds);
268 
269  glfw.is_double_click_down = nk_false;
270  glfw.double_click_pos = nk_vec2(0, 0);
271 
272  return &glfw.ctx;
273 }
274 
275 NK_API void
276 nk_glfw3_font_stash_begin(struct nk_font_atlas **atlas)
277 {
278  nk_font_atlas_init_default(&glfw.atlas);
279  nk_font_atlas_begin(&glfw.atlas);
280  *atlas = &glfw.atlas;
281 }
282 
283 NK_API void
285 {
286  const void *image; int w, h;
287  image = nk_font_atlas_bake(&glfw.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
288  nk_glfw3_device_upload_atlas(image, w, h);
289  nk_font_atlas_end(&glfw.atlas, nk_handle_id((int)glfw.ogl.font_tex), &glfw.ogl.null);
290  if (glfw.atlas.default_font)
291  nk_style_set_font(&glfw.ctx, &glfw.atlas.default_font->handle);
292 }
293 
294 NK_API void
295 nk_glfw3_new_frame(void)
296 {
297  int i;
298  double x, y;
299  struct nk_context *ctx = &glfw.ctx;
300  struct GLFWwindow *win = glfw.win;
301 
302  glfwGetWindowSize(win, &glfw.width, &glfw.height);
303  glfwGetFramebufferSize(win, &glfw.display_width, &glfw.display_height);
304  glfw.fb_scale.x = (float)glfw.display_width/(float)glfw.width;
305  glfw.fb_scale.y = (float)glfw.display_height/(float)glfw.height;
306 
307  nk_input_begin(ctx);
308  for (i = 0; i < glfw.text_len; ++i)
309  nk_input_unicode(ctx, glfw.text[i]);
310 
311  /* optional grabbing behavior */
312  if (ctx->input.mouse.grab)
314  else if (ctx->input.mouse.ungrab)
316 
331 
343  } else {
346  nk_input_key(ctx, NK_KEY_COPY, 0);
347  nk_input_key(ctx, NK_KEY_PASTE, 0);
348  nk_input_key(ctx, NK_KEY_CUT, 0);
349  nk_input_key(ctx, NK_KEY_SHIFT, 0);
350  }
351 
352  glfwGetCursorPos(win, &x, &y);
353  nk_input_motion(ctx, (int)x, (int)y);
354  if (ctx->input.mouse.grabbed) {
355  glfwSetCursorPos(glfw.win, (double)ctx->input.mouse.prev.x, (double)ctx->input.mouse.prev.y);
356  ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
357  ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
358  }
359 
363  nk_input_button(ctx, NK_BUTTON_DOUBLE, (int)glfw.double_click_pos.x, (int)glfw.double_click_pos.y, glfw.is_double_click_down);
364  nk_input_scroll(ctx, glfw.scroll);
365  nk_input_end(&glfw.ctx);
366  glfw.text_len = 0;
367  glfw.scroll = nk_vec2(0,0);
368 }
369 
370 NK_API
371 void nk_glfw3_shutdown(void)
372 {
373  struct nk_glfw_device *dev = &glfw.ogl;
374  nk_font_atlas_clear(&glfw.atlas);
375  nk_free(&glfw.ctx);
376  glDeleteTextures(1, &dev->font_tex);
377  nk_buffer_free(&dev->cmds);
378  NK_MEMSET(&glfw, 0, sizeof(glfw));
379 }
380 
381 #endif
#define glDisableClientState
GLFWAPI void glfwSetInputMode(GLFWwindow *window, int mode, int value)
Sets an input option for the specified window.
Definition: input.c:484
#define GL_TEXTURE_MAG_FILTER
nk_glfw_init_state
#define glEnableClientState
#define GLFW_KEY_RIGHT_CONTROL
Definition: glfw3.h:478
GLint y
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:544
#define GLFW_KEY_LEFT
Definition: glfw3.h:419
typedef void(APIENTRY *GLDEBUGPROC)(GLenum source
GLFWAPI void glfwSetCursorPos(GLFWwindow *window, double xpos, double ypos)
Sets the position of the cursor, relative to the client area of the window.
Definition: input.c:660
#define GL_VERTEX_ARRAY
The header of the GLFW 3 API.
#define GL_TEXTURE_2D
#define GL_RGBA
#define glVertexPointer
#define GL_SCISSOR_TEST
#define glOrtho
unsigned char ungrab
Definition: nuklear.h:4538
nk_anti_aliasing
Definition: nuklear.h:1134
#define GL_UNSIGNED_BYTE
#define GLFW_KEY_UP
Definition: glfw3.h:421
double dt
Definition: boing.c:106
#define glPopMatrix
#define GLFW_MOUSE_BUTTON_RIGHT
Definition: glfw3.h:547
NK_API void nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff)
#define GL_PROJECTION
#define GLFW_KEY_X
Definition: glfw3.h:401
#define NK_ALIGNOF(t)
Definition: nuklear.h:5601
unsigned char grab
Definition: nuklear.h:4536
#define glPushAttrib
#define GLFW_CURSOR
Definition: glfw3.h:1001
NK_API void nk_glfw3_shutdown(void)
GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow *window, GLFWmousebuttonfun cbfun)
Sets the mouse button callback.
Definition: input.c:821
int GLsizei
#define GLFW_KEY_LEFT_SHIFT
Definition: glfw3.h:473
struct nk_vec2 prev
Definition: nuklear.h:4533
#define GLFW_MOUSE_BUTTON_LEFT
Definition: glfw3.h:546
#define GLFW_KEY_PAGE_DOWN
Definition: glfw3.h:423
GLdouble GLdouble GLdouble w
#define GL_BLEND
#define GLFW_KEY_HOME
Definition: glfw3.h:424
#define GL_LINEAR
#define glLoadIdentity
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
#define GLFW_KEY_E
Definition: glfw3.h:382
#define GLFW_KEY_PAGE_UP
Definition: glfw3.h:422
#define glTexImage2D
GLFWAPI int glfwGetMouseButton(GLFWwindow *window, int button)
Returns the last reported state of a mouse button for the specified window.
Definition: input.c:614
#define GL_SRC_ALPHA
NK_API void nk_input_scroll(struct nk_context *, struct nk_vec2 val)
NK_API void nk_free(struct nk_context *)
#define GLFW_KEY_Z
Definition: glfw3.h:403
#define glEnable
NK_API void nk_glfw3_render(enum nk_anti_aliasing)
#define GLFW_KEY_B
Definition: glfw3.h:379
#define GLFW_KEY_BACKSPACE
Definition: glfw3.h:415
GLenum GLenum GLsizei void * image
#define GLFW_KEY_V
Definition: glfw3.h:399
GLenum GLsizei len
Definition: glext.h:3285
GLFWAPI const char * glfwGetClipboardString(GLFWwindow *window)
Returns the contents of the clipboard as a string.
Definition: input.c:1269
NK_API struct nk_vec2 nk_vec2(float x, float y)
GLdouble f
NK_API int nk_strlen(const char *str)
#define GLFW_MOUSE_BUTTON_MIDDLE
Definition: glfw3.h:548
#define glTexCoordPointer
#define glGenTextures
#define GL_FLOAT
#define GL_COLOR_BUFFER_BIT
NK_API void nk_glfw3_new_frame(void)
#define GLFW_CURSOR_NORMAL
Definition: glfw3.h:1006
#define GL_TRANSFORM_BIT
#define GLFW_KEY_END
Definition: glfw3.h:425
NK_API const void * nk_buffer_memory_const(const struct nk_buffer *)
#define glTexParameteri
int GLint
#define glScissor
GLdouble x
NK_API struct nk_context * nk_glfw3_init(GLFWwindow *win, enum nk_glfw_init_state)
#define glPushMatrix
#define GLFW_KEY_DELETE
Definition: glfw3.h:417
NK_API void nk_input_begin(struct nk_context *)
GLint GLsizei GLsizei height
static const textual_icon edit
Definition: model-views.h:221
#define GL_UNSIGNED_SHORT
#define GLFW_KEY_TAB
Definition: glfw3.h:414
#define GL_TEXTURE_MIN_FILTER
GLFWAPI GLFWcharfun glfwSetCharCallback(GLFWwindow *window, GLFWcharfun cbfun)
Sets the Unicode character callback.
Definition: input.c:801
float y
Definition: nuklear.h:457
#define GLFW_KEY_ENTER
Definition: glfw3.h:413
#define GL_ENABLE_BIT
NK_API nk_handle nk_handle_id(int)
float x
Definition: nuklear.h:457
struct nk_mouse mouse
Definition: nuklear.h:4553
action
Definition: enums.py:62
#define glViewport
NK_API void nk_input_motion(struct nk_context *, int x, int y)
NK_API int nk_textedit_paste(struct nk_text_edit *, char const *, int len)
#define GL_MODELVIEW
#define GL_ONE_MINUS_SRC_ALPHA
#define glDeleteTextures
GLFWAPI void glfwSetClipboardString(GLFWwindow *window, const char *string)
Sets the clipboard to the specified string.
Definition: input.c:1261
#define NK_INTERN
Definition: nuklear.h:270
NK_API void nk_input_button(struct nk_context *, enum nk_buttons, int x, int y, int down)
NK_API void nk_input_key(struct nk_context *, enum nk_keys, int down)
NK_API nk_handle nk_handle_ptr(void *)
GLFWAPI void glfwGetCursorPos(GLFWwindow *window, double *xpos, double *ypos)
Retrieves the position of the cursor relative to the client area of the window.
Definition: input.c:637
#define GL_CULL_FACE
#define GLFW_KEY_DOWN
Definition: glfw3.h:420
GLFWAPI double glfwGetTime(void)
Returns the value of the GLFW timer.
Definition: input.c:1275
#define GLFW_CURSOR_HIDDEN
Definition: glfw3.h:1007
#define NK_API
Definition: nuklear.h:259
static const struct @18 vertices[3]
unsigned int GLuint
struct nk_vec2 pos
Definition: nuklear.h:4532
NK_API void nk_buffer_free(struct nk_buffer *)
GLFWAPI GLFWscrollfun glfwSetScrollCallback(GLFWwindow *window, GLFWscrollfun cbfun)
Sets the scroll callback.
Definition: input.c:854
#define glPopAttrib
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
#define glDrawElements
#define glBindTexture
#define GLFW_KEY_LEFT_CONTROL
Definition: glfw3.h:474
NK_API void nk_clear(struct nk_context *)
NK_API void nk_glfw3_char_callback(GLFWwindow *win, unsigned int codepoint)
#define GL_COLOR_ARRAY
NK_API void nk_glfw3_font_stash_end(void)
NK_API void nk_input_end(struct nk_context *)
#define NULL
Definition: tinycthread.c:47
NK_API void nk_glfw3_font_stash_begin(struct nk_font_atlas **atlas)
int i
#define offsetof(STRUCTURE, FIELD)
Definition: sqlite3.c:11372
NK_API void nk_style_set_font(struct nk_context *, const struct nk_user_font *)
#define glBlendFunc
NK_UINT8 nk_byte
Definition: nuklear.h:398
#define GLFW_KEY_RIGHT_SHIFT
Definition: glfw3.h:477
#define GLFW_KEY_R
Definition: glfw3.h:395
#define glColorPointer
#define GL_TEXTURE_COORD_ARRAY
#define GLFW_KEY_RIGHT
Definition: glfw3.h:418
struct nk_input input
Definition: nuklear.h:5493
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
GLFWAPI int glfwGetKey(GLFWwindow *window, int key)
Returns the last reported state of a keyboard key for the specified window.
Definition: input.c:591
unsigned char grabbed
Definition: nuklear.h:4537
#define glMatrixMode
NK_API void nk_input_unicode(struct nk_context *, nk_rune)
struct GLFWwindow GLFWwindow
GLint GLsizei width
#define NK_OFFSETOF(st, m)
Definition: nuklear.h:5583
#define GL_TRIANGLES
#define GL_DEPTH_TEST
GLintptr offset
#define GLFW_KEY_C
Definition: glfw3.h:380
#define glDisable


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:38