CHICKEN Scheme bindings to nuklear

nuklear_glfw_gl2.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Nuklear - v1.00 - public domain
  3. * no warrenty implied; use at your own risk.
  4. * authored from 2015-2016 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 , int max_vertex_buffer, int max_element_buffer);
  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. struct nk_glfw_device {
  41. struct nk_buffer cmds;
  42. struct nk_draw_null_texture null;
  43. GLuint font_tex;
  44. };
  45. static struct nk_glfw {
  46. GLFWwindow *win;
  47. int width, height;
  48. int display_width, display_height;
  49. struct nk_glfw_device ogl;
  50. struct nk_context ctx;
  51. struct nk_font_atlas atlas;
  52. struct nk_vec2 fb_scale;
  53. unsigned int text[NK_GLFW_TEXT_MAX];
  54. int text_len;
  55. float scroll;
  56. } glfw;
  57. NK_INTERN void
  58. nk_glfw3_device_upload_atlas(const void *image, int width, int height)
  59. {
  60. struct nk_glfw_device *dev = &glfw.ogl;
  61. glGenTextures(1, &dev->font_tex);
  62. glBindTexture(GL_TEXTURE_2D, dev->font_tex);
  63. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  64. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  65. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
  66. GL_RGBA, GL_UNSIGNED_BYTE, image);
  67. }
  68. NK_API void
  69. nk_glfw3_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
  70. {
  71. /* setup global state */
  72. struct nk_glfw_device *dev = &glfw.ogl;
  73. glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_TRANSFORM_BIT);
  74. glDisable(GL_CULL_FACE);
  75. glDisable(GL_DEPTH_TEST);
  76. glEnable(GL_SCISSOR_TEST);
  77. glEnable(GL_BLEND);
  78. glEnable(GL_TEXTURE_2D);
  79. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  80. /* setup viewport/project */
  81. glViewport(0,0,(GLsizei)glfw.display_width,(GLsizei)glfw.display_height);
  82. glMatrixMode(GL_PROJECTION);
  83. glPushMatrix();
  84. glLoadIdentity();
  85. glOrtho(0.0f, glfw.width, glfw.height, 0.0f, -1.0f, 1.0f);
  86. glMatrixMode(GL_MODELVIEW);
  87. glPushMatrix();
  88. glLoadIdentity();
  89. glEnableClientState(GL_VERTEX_ARRAY);
  90. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  91. glEnableClientState(GL_COLOR_ARRAY);
  92. {
  93. GLsizei vs = sizeof(struct nk_draw_vertex);
  94. size_t vp = offsetof(struct nk_draw_vertex, position);
  95. size_t vt = offsetof(struct nk_draw_vertex, uv);
  96. size_t vc = offsetof(struct nk_draw_vertex, col);
  97. /* convert from command queue into draw list and draw to screen */
  98. const struct nk_draw_command *cmd;
  99. const nk_draw_index *offset = NULL;
  100. struct nk_buffer vbuf, ebuf;
  101. /* fill converting configuration */
  102. struct nk_convert_config config;
  103. memset(&config, 0, sizeof(config));
  104. config.global_alpha = 1.0f;
  105. config.shape_AA = AA;
  106. config.line_AA = AA;
  107. config.circle_segment_count = 22;
  108. config.curve_segment_count = 22;
  109. config.arc_segment_count = 22;
  110. config.null = dev->null;
  111. /* convert shapes into vertexes */
  112. nk_buffer_init_default(&vbuf);
  113. nk_buffer_init_default(&ebuf);
  114. nk_convert(&glfw.ctx, &dev->cmds, &vbuf, &ebuf, &config);
  115. /* setup vertex buffer pointer */
  116. {const void *vertices = nk_buffer_memory_const(&vbuf);
  117. glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp));
  118. glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt));
  119. glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));}
  120. /* iterate over and execute each draw command */
  121. offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf);
  122. nk_draw_foreach(cmd, &glfw.ctx, &dev->cmds)
  123. {
  124. if (!cmd->elem_count) continue;
  125. glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
  126. glScissor(
  127. (GLint)(cmd->clip_rect.x * glfw.fb_scale.x),
  128. (GLint)((glfw.height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * glfw.fb_scale.y),
  129. (GLint)(cmd->clip_rect.w * glfw.fb_scale.x),
  130. (GLint)(cmd->clip_rect.h * glfw.fb_scale.y));
  131. glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
  132. offset += cmd->elem_count;
  133. }
  134. nk_clear(&glfw.ctx);
  135. nk_buffer_free(&vbuf);
  136. nk_buffer_free(&ebuf);
  137. }
  138. /* default OpenGL state */
  139. glDisableClientState(GL_VERTEX_ARRAY);
  140. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  141. glDisableClientState(GL_COLOR_ARRAY);
  142. glDisable(GL_CULL_FACE);
  143. glDisable(GL_DEPTH_TEST);
  144. glDisable(GL_SCISSOR_TEST);
  145. glDisable(GL_BLEND);
  146. glDisable(GL_TEXTURE_2D);
  147. glBindTexture(GL_TEXTURE_2D, 0);
  148. glMatrixMode(GL_MODELVIEW);
  149. glPopMatrix();
  150. glMatrixMode(GL_PROJECTION);
  151. glPopMatrix();
  152. glPopAttrib();
  153. }
  154. NK_API void
  155. nk_glfw3_char_callback(GLFWwindow *win, unsigned int codepoint)
  156. {
  157. (void)win;
  158. if (glfw.text_len < NK_GLFW_TEXT_MAX)
  159. glfw.text[glfw.text_len++] = codepoint;
  160. }
  161. NK_API void
  162. nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff)
  163. {
  164. (void)win; (void)xoff;
  165. glfw.scroll += (float)yoff;
  166. }
  167. NK_INTERN void
  168. nk_glfw3_clipbard_paste(nk_handle usr, struct nk_text_edit *edit)
  169. {
  170. const char *text = glfwGetClipboardString(glfw.win);
  171. if (text) nk_textedit_paste(edit, text, nk_strlen(text));
  172. (void)usr;
  173. }
  174. NK_INTERN void
  175. nk_glfw3_clipbard_copy(nk_handle usr, const char *text, int len)
  176. {
  177. char *str = 0;
  178. (void)usr;
  179. if (!len) return;
  180. str = (char*)malloc((size_t)len+1);
  181. if (!str) return;
  182. memcpy(str, text, (size_t)len);
  183. str[len] = '\0';
  184. glfwSetClipboardString(glfw.win, str);
  185. free(str);
  186. }
  187. NK_API struct nk_context*
  188. nk_glfw3_init(GLFWwindow *win, enum nk_glfw_init_state init_state)
  189. {
  190. glfw.win = win;
  191. if (init_state == NK_GLFW3_INSTALL_CALLBACKS) {
  192. glfwSetScrollCallback(win, nk_gflw3_scroll_callback);
  193. glfwSetCharCallback(win, nk_glfw3_char_callback);
  194. }
  195. nk_init_default(&glfw.ctx, 0);
  196. glfw.ctx.clip.copy = nk_glfw3_clipbard_copy;
  197. glfw.ctx.clip.paste = nk_glfw3_clipbard_paste;
  198. glfw.ctx.clip.userdata = nk_handle_ptr(0);
  199. nk_buffer_init_default(&glfw.ogl.cmds);
  200. return &glfw.ctx;
  201. }
  202. NK_API void
  203. nk_glfw3_font_stash_begin(struct nk_font_atlas **atlas)
  204. {
  205. nk_font_atlas_init_default(&glfw.atlas);
  206. nk_font_atlas_begin(&glfw.atlas);
  207. *atlas = &glfw.atlas;
  208. }
  209. NK_API void
  210. nk_glfw3_font_stash_end(void)
  211. {
  212. const void *image; int w, h;
  213. image = nk_font_atlas_bake(&glfw.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
  214. nk_glfw3_device_upload_atlas(image, w, h);
  215. nk_font_atlas_end(&glfw.atlas, nk_handle_id((int)glfw.ogl.font_tex), &glfw.ogl.null);
  216. if (glfw.atlas.default_font)
  217. nk_style_set_font(&glfw.ctx, &glfw.atlas.default_font->handle);
  218. }
  219. NK_API void
  220. nk_glfw3_new_frame(void)
  221. {
  222. int i;
  223. double x, y;
  224. struct nk_context *ctx = &glfw.ctx;
  225. struct GLFWwindow *win = glfw.win;
  226. glfwGetWindowSize(win, &glfw.width, &glfw.height);
  227. glfwGetFramebufferSize(win, &glfw.display_width, &glfw.display_height);
  228. glfw.fb_scale.x = (float)glfw.display_width/(float)glfw.width;
  229. glfw.fb_scale.y = (float)glfw.display_height/(float)glfw.height;
  230. nk_input_begin(ctx);
  231. for (i = 0; i < glfw.text_len; ++i)
  232. nk_input_unicode(ctx, glfw.text[i]);
  233. /* optional grabbing behavior */
  234. if (ctx->input.mouse.grab)
  235. glfwSetInputMode(glfw.win, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  236. else if (ctx->input.mouse.ungrab)
  237. glfwSetInputMode(glfw.win, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  238. nk_input_key(ctx, NK_KEY_DEL, glfwGetKey(win, GLFW_KEY_DELETE) == GLFW_PRESS);
  239. nk_input_key(ctx, NK_KEY_ENTER, glfwGetKey(win, GLFW_KEY_ENTER) == GLFW_PRESS);
  240. nk_input_key(ctx, NK_KEY_TAB, glfwGetKey(win, GLFW_KEY_TAB) == GLFW_PRESS);
  241. nk_input_key(ctx, NK_KEY_BACKSPACE, glfwGetKey(win, GLFW_KEY_BACKSPACE) == GLFW_PRESS);
  242. nk_input_key(ctx, NK_KEY_UP, glfwGetKey(win, GLFW_KEY_UP) == GLFW_PRESS);
  243. nk_input_key(ctx, NK_KEY_DOWN, glfwGetKey(win, GLFW_KEY_DOWN) == GLFW_PRESS);
  244. nk_input_key(ctx, NK_KEY_TEXT_START, glfwGetKey(win, GLFW_KEY_HOME) == GLFW_PRESS);
  245. nk_input_key(ctx, NK_KEY_TEXT_END, glfwGetKey(win, GLFW_KEY_END) == GLFW_PRESS);
  246. nk_input_key(ctx, NK_KEY_SHIFT, glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS||
  247. glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS);
  248. if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS ||
  249. glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS) {
  250. nk_input_key(ctx, NK_KEY_COPY, glfwGetKey(win, GLFW_KEY_C) == GLFW_PRESS);
  251. nk_input_key(ctx, NK_KEY_PASTE, glfwGetKey(win, GLFW_KEY_P) == GLFW_PRESS);
  252. nk_input_key(ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_X) == GLFW_PRESS);
  253. nk_input_key(ctx, NK_KEY_TEXT_UNDO, glfwGetKey(win, GLFW_KEY_Z) == GLFW_PRESS);
  254. nk_input_key(ctx, NK_KEY_TEXT_REDO, glfwGetKey(win, GLFW_KEY_R) == GLFW_PRESS);
  255. nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, glfwGetKey(win, GLFW_KEY_LEFT) == GLFW_PRESS);
  256. nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, glfwGetKey(win, GLFW_KEY_RIGHT) == GLFW_PRESS);
  257. nk_input_key(ctx, NK_KEY_TEXT_LINE_START, glfwGetKey(win, GLFW_KEY_B) == GLFW_PRESS);
  258. nk_input_key(ctx, NK_KEY_TEXT_LINE_END, glfwGetKey(win, GLFW_KEY_E) == GLFW_PRESS);
  259. } else {
  260. nk_input_key(ctx, NK_KEY_LEFT, glfwGetKey(win, GLFW_KEY_LEFT) == GLFW_PRESS);
  261. nk_input_key(ctx, NK_KEY_RIGHT, glfwGetKey(win, GLFW_KEY_RIGHT) == GLFW_PRESS);
  262. nk_input_key(ctx, NK_KEY_COPY, 0);
  263. nk_input_key(ctx, NK_KEY_PASTE, 0);
  264. nk_input_key(ctx, NK_KEY_CUT, 0);
  265. nk_input_key(ctx, NK_KEY_SHIFT, 0);
  266. }
  267. glfwGetCursorPos(win, &x, &y);
  268. nk_input_motion(ctx, (int)x, (int)y);
  269. if (ctx->input.mouse.grabbed)
  270. glfwSetCursorPos(glfw.win, ctx->input.mouse.prev.x, ctx->input.mouse.prev.y);
  271. nk_input_button(ctx, NK_BUTTON_LEFT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
  272. nk_input_button(ctx, NK_BUTTON_MIDDLE, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
  273. nk_input_button(ctx, NK_BUTTON_RIGHT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
  274. nk_input_scroll(ctx, glfw.scroll);
  275. nk_input_end(&glfw.ctx);
  276. glfw.text_len = 0;
  277. glfw.scroll = 0;
  278. }
  279. NK_API
  280. void nk_glfw3_shutdown(void)
  281. {
  282. struct nk_glfw_device *dev = &glfw.ogl;
  283. nk_font_atlas_clear(&glfw.atlas);
  284. nk_free(&glfw.ctx);
  285. glDeleteTextures(1, &dev->font_tex);
  286. nk_buffer_free(&dev->cmds);
  287. memset(&glfw, 0, sizeof(glfw));
  288. }
  289. #endif