nuklear_glfw_gl2.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include <GLFW/glfw3.h>
  16. enum nk_glfw_init_state{
  17. NK_GLFW3_DEFAULT = 0,
  18. NK_GLFW3_INSTALL_CALLBACKS
  19. };
  20. NK_API struct nk_context* nk_glfw3_init(GLFWwindow *win, enum nk_glfw_init_state);
  21. NK_API void nk_glfw3_font_stash_begin(struct nk_font_atlas **atlas);
  22. NK_API void nk_glfw3_font_stash_end(void);
  23. NK_API void nk_glfw3_new_frame(void);
  24. NK_API void nk_glfw3_render(enum nk_anti_aliasing);
  25. NK_API void nk_glfw3_shutdown(void);
  26. NK_API void nk_glfw3_char_callback(GLFWwindow *win, unsigned int codepoint);
  27. NK_API void nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff);
  28. #endif
  29. /*
  30. * ==============================================================
  31. *
  32. * IMPLEMENTATION
  33. *
  34. * ===============================================================
  35. */
  36. #ifdef NK_GLFW_GL2_IMPLEMENTATION
  37. #ifndef NK_GLFW_TEXT_MAX
  38. #define NK_GLFW_TEXT_MAX 256
  39. #endif
  40. #ifndef NK_GLFW_DOUBLE_CLICK_LO
  41. #define NK_GLFW_DOUBLE_CLICK_LO 0.02
  42. #endif
  43. #ifndef NK_GLFW_DOUBLE_CLICK_HI
  44. #define NK_GLFW_DOUBLE_CLICK_HI 0.2
  45. #endif
  46. struct nk_glfw_device {
  47. struct nk_buffer cmds;
  48. struct nk_draw_null_texture null;
  49. GLuint font_tex;
  50. };
  51. struct nk_glfw_vertex {
  52. float position[2];
  53. float uv[2];
  54. nk_byte col[4];
  55. };
  56. static struct nk_glfw {
  57. GLFWwindow *win;
  58. int width, height;
  59. int display_width, display_height;
  60. struct nk_glfw_device ogl;
  61. struct nk_context ctx;
  62. struct nk_font_atlas atlas;
  63. struct nk_vec2 fb_scale;
  64. unsigned int text[NK_GLFW_TEXT_MAX];
  65. int text_len;
  66. struct nk_vec2 scroll;
  67. double last_button_click;
  68. int is_double_click_down;
  69. struct nk_vec2 double_click_pos;
  70. } glfw;
  71. NK_INTERN void
  72. nk_glfw3_device_upload_atlas(const void *image, int width, int height)
  73. {
  74. struct nk_glfw_device *dev = &glfw.ogl;
  75. glGenTextures(1, &dev->font_tex);
  76. glBindTexture(GL_TEXTURE_2D, dev->font_tex);
  77. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  78. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  79. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
  80. GL_RGBA, GL_UNSIGNED_BYTE, image);
  81. }
  82. NK_API void
  83. nk_glfw3_render(enum nk_anti_aliasing AA)
  84. {
  85. /* setup global state */
  86. struct nk_glfw_device *dev = &glfw.ogl;
  87. glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_TRANSFORM_BIT);
  88. glDisable(GL_CULL_FACE);
  89. glDisable(GL_DEPTH_TEST);
  90. glEnable(GL_SCISSOR_TEST);
  91. glEnable(GL_BLEND);
  92. glEnable(GL_TEXTURE_2D);
  93. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  94. /* setup viewport/project */
  95. glViewport(0,0,(GLsizei)glfw.display_width,(GLsizei)glfw.display_height);
  96. glMatrixMode(GL_PROJECTION);
  97. glPushMatrix();
  98. glLoadIdentity();
  99. glOrtho(0.0f, glfw.width, glfw.height, 0.0f, -1.0f, 1.0f);
  100. glMatrixMode(GL_MODELVIEW);
  101. glPushMatrix();
  102. glLoadIdentity();
  103. glEnableClientState(GL_VERTEX_ARRAY);
  104. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  105. glEnableClientState(GL_COLOR_ARRAY);
  106. {
  107. GLsizei vs = sizeof(struct nk_glfw_vertex);
  108. size_t vp = offsetof(struct nk_glfw_vertex, position);
  109. size_t vt = offsetof(struct nk_glfw_vertex, uv);
  110. size_t vc = offsetof(struct nk_glfw_vertex, col);
  111. /* convert from command queue into draw list and draw to screen */
  112. const struct nk_draw_command *cmd;
  113. const nk_draw_index *offset = NULL;
  114. struct nk_buffer vbuf, ebuf;
  115. /* fill convert configuration */
  116. struct nk_convert_config config;
  117. static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  118. {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_glfw_vertex, position)},
  119. {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_glfw_vertex, uv)},
  120. {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_glfw_vertex, col)},
  121. {NK_VERTEX_LAYOUT_END}
  122. };
  123. NK_MEMSET(&config, 0, sizeof(config));
  124. config.vertex_layout = vertex_layout;
  125. config.vertex_size = sizeof(struct nk_glfw_vertex);
  126. config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
  127. config.null = dev->null;
  128. config.circle_segment_count = 22;
  129. config.curve_segment_count = 22;
  130. config.arc_segment_count = 22;
  131. config.global_alpha = 1.0f;
  132. config.shape_AA = AA;
  133. config.line_AA = AA;
  134. /* convert shapes into vertexes */
  135. nk_buffer_init_default(&vbuf);
  136. nk_buffer_init_default(&ebuf);
  137. nk_convert(&glfw.ctx, &dev->cmds, &vbuf, &ebuf, &config);
  138. /* setup vertex buffer pointer */
  139. {const void *vertices = nk_buffer_memory_const(&vbuf);
  140. glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp));
  141. glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt));
  142. glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));}
  143. /* iterate over and execute each draw command */
  144. offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf);
  145. nk_draw_foreach(cmd, &glfw.ctx, &dev->cmds)
  146. {
  147. if (!cmd->elem_count) continue;
  148. glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
  149. glScissor(
  150. (GLint)(cmd->clip_rect.x * glfw.fb_scale.x),
  151. (GLint)((glfw.height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * glfw.fb_scale.y),
  152. (GLint)(cmd->clip_rect.w * glfw.fb_scale.x),
  153. (GLint)(cmd->clip_rect.h * glfw.fb_scale.y));
  154. glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
  155. offset += cmd->elem_count;
  156. }
  157. nk_clear(&glfw.ctx);
  158. nk_buffer_free(&vbuf);
  159. nk_buffer_free(&ebuf);
  160. }
  161. /* default OpenGL state */
  162. glDisableClientState(GL_VERTEX_ARRAY);
  163. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  164. glDisableClientState(GL_COLOR_ARRAY);
  165. glDisable(GL_CULL_FACE);
  166. glDisable(GL_DEPTH_TEST);
  167. glDisable(GL_SCISSOR_TEST);
  168. glDisable(GL_BLEND);
  169. glDisable(GL_TEXTURE_2D);
  170. glBindTexture(GL_TEXTURE_2D, 0);
  171. glMatrixMode(GL_MODELVIEW);
  172. glPopMatrix();
  173. glMatrixMode(GL_PROJECTION);
  174. glPopMatrix();
  175. glPopAttrib();
  176. }
  177. NK_API void
  178. nk_glfw3_char_callback(GLFWwindow *win, unsigned int codepoint)
  179. {
  180. (void)win;
  181. if (glfw.text_len < NK_GLFW_TEXT_MAX)
  182. glfw.text[glfw.text_len++] = codepoint;
  183. }
  184. NK_API void
  185. nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff)
  186. {
  187. (void)win; (void)xoff;
  188. glfw.scroll.x += (float)xoff;
  189. glfw.scroll.y += (float)yoff;
  190. }
  191. NK_API void
  192. nk_glfw3_mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
  193. {
  194. double x, y;
  195. if (button != GLFW_MOUSE_BUTTON_LEFT) return;
  196. glfwGetCursorPos(window, &x, &y);
  197. if (action == GLFW_PRESS) {
  198. double dt = glfwGetTime() - glfw.last_button_click;
  199. if (dt > NK_GLFW_DOUBLE_CLICK_LO && dt < NK_GLFW_DOUBLE_CLICK_HI) {
  200. glfw.is_double_click_down = nk_true;
  201. glfw.double_click_pos = nk_vec2((float)x, (float)y);
  202. }
  203. glfw.last_button_click = glfwGetTime();
  204. } else glfw.is_double_click_down = nk_false;
  205. }
  206. NK_INTERN void
  207. nk_glfw3_clipbard_paste(nk_handle usr, struct nk_text_edit *edit)
  208. {
  209. const char *text = glfwGetClipboardString(glfw.win);
  210. if (text) nk_textedit_paste(edit, text, nk_strlen(text));
  211. (void)usr;
  212. }
  213. NK_INTERN void
  214. nk_glfw3_clipbard_copy(nk_handle usr, const char *text, int len)
  215. {
  216. char *str = 0;
  217. (void)usr;
  218. if (!len) return;
  219. str = (char*)malloc((size_t)len+1);
  220. if (!str) return;
  221. NK_MEMCPY(str, text, (size_t)len);
  222. str[len] = '\0';
  223. glfwSetClipboardString(glfw.win, str);
  224. free(str);
  225. }
  226. NK_API struct nk_context*
  227. nk_glfw3_init(GLFWwindow *win, enum nk_glfw_init_state init_state)
  228. {
  229. glfw.win = win;
  230. if (init_state == NK_GLFW3_INSTALL_CALLBACKS) {
  231. glfwSetScrollCallback(win, nk_gflw3_scroll_callback);
  232. glfwSetCharCallback(win, nk_glfw3_char_callback);
  233. glfwSetMouseButtonCallback(win, nk_glfw3_mouse_button_callback);
  234. }
  235. nk_init_default(&glfw.ctx, 0);
  236. glfw.ctx.clip.copy = nk_glfw3_clipbard_copy;
  237. glfw.ctx.clip.paste = nk_glfw3_clipbard_paste;
  238. glfw.ctx.clip.userdata = nk_handle_ptr(0);
  239. nk_buffer_init_default(&glfw.ogl.cmds);
  240. glfw.is_double_click_down = nk_false;
  241. glfw.double_click_pos = nk_vec2(0, 0);
  242. return &glfw.ctx;
  243. }
  244. NK_API void
  245. nk_glfw3_font_stash_begin(struct nk_font_atlas **atlas)
  246. {
  247. nk_font_atlas_init_default(&glfw.atlas);
  248. nk_font_atlas_begin(&glfw.atlas);
  249. *atlas = &glfw.atlas;
  250. }
  251. NK_API void
  252. nk_glfw3_font_stash_end(void)
  253. {
  254. const void *image; int w, h;
  255. image = nk_font_atlas_bake(&glfw.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
  256. nk_glfw3_device_upload_atlas(image, w, h);
  257. nk_font_atlas_end(&glfw.atlas, nk_handle_id((int)glfw.ogl.font_tex), &glfw.ogl.null);
  258. if (glfw.atlas.default_font)
  259. nk_style_set_font(&glfw.ctx, &glfw.atlas.default_font->handle);
  260. }
  261. NK_API void
  262. nk_glfw3_new_frame(void)
  263. {
  264. int i;
  265. double x, y;
  266. struct nk_context *ctx = &glfw.ctx;
  267. struct GLFWwindow *win = glfw.win;
  268. glfwGetWindowSize(win, &glfw.width, &glfw.height);
  269. glfwGetFramebufferSize(win, &glfw.display_width, &glfw.display_height);
  270. glfw.fb_scale.x = (float)glfw.display_width/(float)glfw.width;
  271. glfw.fb_scale.y = (float)glfw.display_height/(float)glfw.height;
  272. nk_input_begin(ctx);
  273. for (i = 0; i < glfw.text_len; ++i)
  274. nk_input_unicode(ctx, glfw.text[i]);
  275. /* optional grabbing behavior */
  276. if (ctx->input.mouse.grab)
  277. glfwSetInputMode(glfw.win, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  278. else if (ctx->input.mouse.ungrab)
  279. glfwSetInputMode(glfw.win, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  280. nk_input_key(ctx, NK_KEY_DEL, glfwGetKey(win, GLFW_KEY_DELETE) == GLFW_PRESS);
  281. nk_input_key(ctx, NK_KEY_ENTER, glfwGetKey(win, GLFW_KEY_ENTER) == GLFW_PRESS);
  282. nk_input_key(ctx, NK_KEY_TAB, glfwGetKey(win, GLFW_KEY_TAB) == GLFW_PRESS);
  283. nk_input_key(ctx, NK_KEY_BACKSPACE, glfwGetKey(win, GLFW_KEY_BACKSPACE) == GLFW_PRESS);
  284. nk_input_key(ctx, NK_KEY_UP, glfwGetKey(win, GLFW_KEY_UP) == GLFW_PRESS);
  285. nk_input_key(ctx, NK_KEY_DOWN, glfwGetKey(win, GLFW_KEY_DOWN) == GLFW_PRESS);
  286. nk_input_key(ctx, NK_KEY_TEXT_START, glfwGetKey(win, GLFW_KEY_HOME) == GLFW_PRESS);
  287. nk_input_key(ctx, NK_KEY_TEXT_END, glfwGetKey(win, GLFW_KEY_END) == GLFW_PRESS);
  288. nk_input_key(ctx, NK_KEY_SCROLL_START, glfwGetKey(win, GLFW_KEY_HOME) == GLFW_PRESS);
  289. nk_input_key(ctx, NK_KEY_SCROLL_END, glfwGetKey(win, GLFW_KEY_END) == GLFW_PRESS);
  290. nk_input_key(ctx, NK_KEY_SCROLL_DOWN, glfwGetKey(win, GLFW_KEY_PAGE_DOWN) == GLFW_PRESS);
  291. nk_input_key(ctx, NK_KEY_SCROLL_UP, glfwGetKey(win, GLFW_KEY_PAGE_UP) == GLFW_PRESS);
  292. nk_input_key(ctx, NK_KEY_SHIFT, glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS||
  293. glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS);
  294. if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS ||
  295. glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS) {
  296. nk_input_key(ctx, NK_KEY_COPY, glfwGetKey(win, GLFW_KEY_C) == GLFW_PRESS);
  297. nk_input_key(ctx, NK_KEY_PASTE, glfwGetKey(win, GLFW_KEY_V) == GLFW_PRESS);
  298. nk_input_key(ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_X) == GLFW_PRESS);
  299. nk_input_key(ctx, NK_KEY_TEXT_UNDO, glfwGetKey(win, GLFW_KEY_Z) == GLFW_PRESS);
  300. nk_input_key(ctx, NK_KEY_TEXT_REDO, glfwGetKey(win, GLFW_KEY_R) == GLFW_PRESS);
  301. nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, glfwGetKey(win, GLFW_KEY_LEFT) == GLFW_PRESS);
  302. nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, glfwGetKey(win, GLFW_KEY_RIGHT) == GLFW_PRESS);
  303. nk_input_key(ctx, NK_KEY_TEXT_LINE_START, glfwGetKey(win, GLFW_KEY_B) == GLFW_PRESS);
  304. nk_input_key(ctx, NK_KEY_TEXT_LINE_END, glfwGetKey(win, GLFW_KEY_E) == GLFW_PRESS);
  305. } else {
  306. nk_input_key(ctx, NK_KEY_LEFT, glfwGetKey(win, GLFW_KEY_LEFT) == GLFW_PRESS);
  307. nk_input_key(ctx, NK_KEY_RIGHT, glfwGetKey(win, GLFW_KEY_RIGHT) == GLFW_PRESS);
  308. nk_input_key(ctx, NK_KEY_COPY, 0);
  309. nk_input_key(ctx, NK_KEY_PASTE, 0);
  310. nk_input_key(ctx, NK_KEY_CUT, 0);
  311. nk_input_key(ctx, NK_KEY_SHIFT, 0);
  312. }
  313. glfwGetCursorPos(win, &x, &y);
  314. nk_input_motion(ctx, (int)x, (int)y);
  315. if (ctx->input.mouse.grabbed) {
  316. glfwSetCursorPos(glfw.win, (double)ctx->input.mouse.prev.x, (double)ctx->input.mouse.prev.y);
  317. ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
  318. ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
  319. }
  320. nk_input_button(ctx, NK_BUTTON_LEFT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
  321. nk_input_button(ctx, NK_BUTTON_MIDDLE, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
  322. nk_input_button(ctx, NK_BUTTON_RIGHT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
  323. nk_input_button(ctx, NK_BUTTON_DOUBLE, (int)glfw.double_click_pos.x, (int)glfw.double_click_pos.y, glfw.is_double_click_down);
  324. nk_input_scroll(ctx, glfw.scroll);
  325. nk_input_end(&glfw.ctx);
  326. glfw.text_len = 0;
  327. glfw.scroll = nk_vec2(0,0);
  328. }
  329. NK_API
  330. void nk_glfw3_shutdown(void)
  331. {
  332. struct nk_glfw_device *dev = &glfw.ogl;
  333. nk_font_atlas_clear(&glfw.atlas);
  334. nk_free(&glfw.ctx);
  335. glDeleteTextures(1, &dev->font_tex);
  336. nk_buffer_free(&dev->cmds);
  337. NK_MEMSET(&glfw, 0, sizeof(glfw));
  338. }
  339. #endif