nuklear_sfml_gl2.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Nuklear - 1.32.0 - 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_SFML_GL2_H_
  14. #define NK_SFML_GL2_H_
  15. #include <SFML/Window.hpp>
  16. NK_API struct nk_context* nk_sfml_init(sf::Window* window);
  17. NK_API void nk_sfml_font_stash_begin(struct nk_font_atlas** atlas);
  18. NK_API void nk_sfml_font_stash_end(void);
  19. NK_API int nk_sfml_handle_event(sf::Event* event);
  20. NK_API void nk_sfml_render(enum nk_anti_aliasing);
  21. NK_API void nk_sfml_shutdown(void);
  22. #endif
  23. /*
  24. * ==============================================================
  25. *
  26. * IMPLEMENTATION
  27. *
  28. * ===============================================================
  29. */
  30. #ifdef NK_SFML_GL2_IMPLEMENTATION
  31. struct nk_sfml_device {
  32. struct nk_buffer cmds;
  33. struct nk_draw_null_texture null;
  34. GLuint font_tex;
  35. };
  36. struct nk_sfml_vertex {
  37. float position[2];
  38. float uv[2];
  39. nk_byte col[4];
  40. };
  41. static struct nk_sfml {
  42. sf::Window* window;
  43. struct nk_sfml_device ogl;
  44. struct nk_context ctx;
  45. struct nk_font_atlas atlas;
  46. } sfml;
  47. NK_INTERN void
  48. nk_sfml_device_upload_atlas(const void* image, int width, int height)
  49. {
  50. struct nk_sfml_device* dev = &sfml.ogl;
  51. glGenTextures(1, &dev->font_tex);
  52. glBindTexture(GL_TEXTURE_2D, dev->font_tex);
  53. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  54. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  55. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
  56. GL_RGBA, GL_UNSIGNED_BYTE, image);
  57. }
  58. NK_API void
  59. nk_sfml_render(enum nk_anti_aliasing AA)
  60. {
  61. /* setup global state */
  62. struct nk_sfml_device* dev = &sfml.ogl;
  63. int window_width = sfml.window->getSize().x;
  64. int window_height = sfml.window->getSize().y;
  65. glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
  66. glDisable(GL_CULL_FACE);
  67. glDisable(GL_DEPTH_TEST);
  68. glEnable(GL_SCISSOR_TEST);
  69. glEnable(GL_BLEND);
  70. glEnable(GL_TEXTURE_2D);
  71. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  72. glViewport(0, 0, (GLsizei)window_width, (GLsizei)window_height);
  73. glMatrixMode(GL_TEXTURE);
  74. glPushMatrix();
  75. glLoadIdentity();
  76. glMatrixMode(GL_PROJECTION);
  77. glPushMatrix();
  78. glLoadIdentity();
  79. glOrtho(0.0f, window_width, window_height, 0.0f, -1.0f, 1.0f);
  80. glMatrixMode(GL_MODELVIEW);
  81. glPushMatrix();
  82. glLoadIdentity();
  83. glEnableClientState(GL_VERTEX_ARRAY);
  84. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  85. glEnableClientState(GL_COLOR_ARRAY);
  86. {
  87. GLsizei vs = sizeof(struct nk_sfml_vertex);
  88. size_t vp = offsetof(struct nk_sfml_vertex, position);
  89. size_t vt = offsetof(struct nk_sfml_vertex, uv);
  90. size_t vc = offsetof(struct nk_sfml_vertex, col);
  91. /* convert from command queue into draw list and draw to screen */
  92. const struct nk_draw_command* cmd;
  93. const nk_draw_index* offset = NULL;
  94. struct nk_buffer vbuf, ebuf;
  95. /* fill converting configuration */
  96. struct nk_convert_config config;
  97. static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  98. {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sfml_vertex, position)},
  99. {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sfml_vertex, uv)},
  100. {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_sfml_vertex, col)},
  101. {NK_VERTEX_LAYOUT_END}
  102. };
  103. NK_MEMSET(&config, 0, sizeof(config));
  104. config.vertex_layout = vertex_layout;
  105. config.vertex_size = sizeof(struct nk_sfml_vertex);
  106. config.vertex_alignment = NK_ALIGNOF(struct nk_sfml_vertex);
  107. config.null = dev->null;
  108. config.circle_segment_count = 22;
  109. config.curve_segment_count = 22;
  110. config.arc_segment_count = 22;
  111. config.global_alpha = 1.0f;
  112. config.shape_AA = AA;
  113. config.line_AA = AA;
  114. /* convert shapes into vertices */
  115. nk_buffer_init_default(&vbuf);
  116. nk_buffer_init_default(&ebuf);
  117. nk_convert(&sfml.ctx, &dev->cmds, &vbuf, &ebuf, &config);
  118. /* setup vertex buffer pointer */
  119. const void* vertices = nk_buffer_memory_const(&vbuf);
  120. glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp));
  121. glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt));
  122. glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));
  123. /* iterate over and execute each draw command */
  124. offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf);
  125. nk_draw_foreach(cmd, &sfml.ctx, &dev->cmds)
  126. {
  127. if(!cmd->elem_count) continue;
  128. glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
  129. glScissor(
  130. (GLint)(cmd->clip_rect.x),
  131. (GLint)((window_height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h))),
  132. (GLint)(cmd->clip_rect.w),
  133. (GLint)(cmd->clip_rect.h));
  134. glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
  135. offset += cmd->elem_count;
  136. }
  137. nk_clear(&sfml.ctx);
  138. nk_buffer_free(&vbuf);
  139. nk_buffer_free(&ebuf);
  140. }
  141. /* default OpenGL state */
  142. glDisableClientState(GL_VERTEX_ARRAY);
  143. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  144. glDisableClientState(GL_COLOR_ARRAY);
  145. glDisable(GL_CULL_FACE);
  146. glDisable(GL_DEPTH_TEST);
  147. glDisable(GL_SCISSOR_TEST);
  148. glDisable(GL_BLEND);
  149. glDisable(GL_TEXTURE_2D);
  150. glBindTexture(GL_TEXTURE_2D, 0);
  151. glMatrixMode(GL_TEXTURE);
  152. glPopMatrix();
  153. glMatrixMode(GL_MODELVIEW);
  154. glPopMatrix();
  155. glMatrixMode(GL_PROJECTION);
  156. glPopMatrix();
  157. glPopAttrib();
  158. }
  159. static void
  160. nk_sfml_clipboard_paste(nk_handle usr, struct nk_text_edit* edit)
  161. {
  162. #if 0
  163. /* Not Implemented in SFML */
  164. sf::Clipboard clipboard(sfml.window);
  165. const char* text = clipboard.getText();
  166. if(text)
  167. nk_textedit_paste(edit, text, nk_strlen(text));
  168. (void)usr;
  169. #endif
  170. }
  171. static void
  172. nk_sfml_clipboard_copy(nk_handle usr, const char* text, int len)
  173. {
  174. #if 0
  175. char* str = 0;
  176. (void)usr;
  177. if(!len) return;
  178. str = (char*)malloc((size_t)len+1);
  179. if(!str) return;
  180. memcpy(str, text, (size_t)len);
  181. str[len] = '\0';
  182. /* Not Implemented in SFML */
  183. sf::Clipboard clipboard(sfml.window);
  184. clipboard.setText(str);
  185. free(str);
  186. #endif
  187. }
  188. NK_API struct nk_context*
  189. nk_sfml_init(sf::Window* window)
  190. {
  191. sfml.window = window;
  192. nk_init_default(&sfml.ctx, 0);
  193. sfml.ctx.clip.copy = nk_sfml_clipboard_copy;
  194. sfml.ctx.clip.paste = nk_sfml_clipboard_paste;
  195. sfml.ctx.clip.userdata = nk_handle_ptr(0);
  196. nk_buffer_init_default(&sfml.ogl.cmds);
  197. return &sfml.ctx;
  198. }
  199. NK_API void
  200. nk_sfml_font_stash_begin(struct nk_font_atlas** atlas)
  201. {
  202. nk_font_atlas_init_default(&sfml.atlas);
  203. nk_font_atlas_begin(&sfml.atlas);
  204. *atlas = &sfml.atlas;
  205. }
  206. NK_API void
  207. nk_sfml_font_stash_end()
  208. {
  209. int w, h;
  210. const void* img;
  211. img = nk_font_atlas_bake(&sfml.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
  212. nk_sfml_device_upload_atlas(img, w, h);
  213. nk_font_atlas_end(&sfml.atlas, nk_handle_id((int)sfml.ogl.font_tex), &sfml.ogl.null);
  214. if(sfml.atlas.default_font)
  215. nk_style_set_font(&sfml.ctx, &sfml.atlas.default_font->handle);
  216. }
  217. NK_API int
  218. nk_sfml_handle_event(sf::Event* evt)
  219. {
  220. struct nk_context* ctx = &sfml.ctx;
  221. /* optional grabbing behavior */
  222. if(ctx->input.mouse.grab)
  223. ctx->input.mouse.grab = 0;
  224. else if(ctx->input.mouse.ungrab) {
  225. int x = (int)ctx->input.mouse.prev.x;
  226. int y = (int)ctx->input.mouse.prev.y;
  227. sf::Mouse::setPosition(sf::Vector2i(x, y), *sfml.window);
  228. ctx->input.mouse.ungrab = 0;
  229. }
  230. if(evt->type == sf::Event::KeyReleased || evt->type == sf::Event::KeyPressed)
  231. {
  232. int down = evt->type == sf::Event::KeyPressed;
  233. sf::Keyboard::Key key = evt->key.code;
  234. if(key == sf::Keyboard::RShift || key == sf::Keyboard::LShift)
  235. nk_input_key(ctx, NK_KEY_SHIFT, down);
  236. else if(key == sf::Keyboard::Delete)
  237. nk_input_key(ctx, NK_KEY_DEL, down);
  238. else if(key == sf::Keyboard::Return)
  239. nk_input_key(ctx, NK_KEY_ENTER, down);
  240. else if(key == sf::Keyboard::Tab)
  241. nk_input_key(ctx, NK_KEY_TAB, down);
  242. else if(key == sf::Keyboard::BackSpace)
  243. nk_input_key(ctx, NK_KEY_BACKSPACE, down);
  244. else if(key == sf::Keyboard::Home) {
  245. nk_input_key(ctx, NK_KEY_TEXT_START, down);
  246. nk_input_key(ctx, NK_KEY_SCROLL_START, down);
  247. } else if(key == sf::Keyboard::End) {
  248. nk_input_key(ctx, NK_KEY_TEXT_END, down);
  249. nk_input_key(ctx, NK_KEY_SCROLL_END, down);
  250. } else if(key == sf::Keyboard::PageDown)
  251. nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
  252. else if(key == sf::Keyboard::PageUp)
  253. nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
  254. else if(key == sf::Keyboard::Z)
  255. nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
  256. else if(key == sf::Keyboard::R)
  257. nk_input_key(ctx, NK_KEY_TEXT_REDO, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
  258. else if(key == sf::Keyboard::C)
  259. nk_input_key(ctx, NK_KEY_COPY, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
  260. else if(key == sf::Keyboard::V)
  261. nk_input_key(ctx, NK_KEY_PASTE, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
  262. else if(key == sf::Keyboard::X)
  263. nk_input_key(ctx, NK_KEY_CUT, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
  264. else if(key == sf::Keyboard::B)
  265. nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
  266. else if(key == sf::Keyboard::E)
  267. nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl));
  268. else if(key == sf::Keyboard::Up)
  269. nk_input_key(ctx, NK_KEY_UP, down);
  270. else if(key == sf::Keyboard::Down)
  271. nk_input_key(ctx, NK_KEY_DOWN, down);
  272. else if(key == sf::Keyboard::Left) {
  273. if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
  274. nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
  275. else nk_input_key(ctx, NK_KEY_LEFT, down);
  276. } else if(key == sf::Keyboard::Right) {
  277. if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
  278. nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  279. else nk_input_key(ctx, NK_KEY_RIGHT, down);
  280. } else return 0;
  281. return 1;
  282. } else if(evt->type == sf::Event::MouseButtonPressed || evt->type == sf::Event::MouseButtonReleased) {
  283. int down = evt->type == sf::Event::MouseButtonPressed;
  284. const int x = evt->mouseButton.x, y = evt->mouseButton.y;
  285. if(evt->mouseButton.button == sf::Mouse::Left)
  286. nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
  287. if(evt->mouseButton.button == sf::Mouse::Middle)
  288. nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
  289. if(evt->mouseButton.button == sf::Mouse::Right)
  290. nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
  291. else return 0;
  292. return 1;
  293. } else if(evt->type == sf::Event::MouseMoved) {
  294. nk_input_motion(ctx, evt->mouseMove.x, evt->mouseMove.y);
  295. return 1;
  296. } else if(evt->type == sf::Event::TouchBegan || evt->type == sf::Event::TouchEnded) {
  297. int down = evt->type == sf::Event::TouchBegan;
  298. const int x = evt->touch.x, y = evt->touch.y;
  299. ctx->input.mouse.pos.x = x;
  300. ctx->input.mouse.pos.y = y;
  301. nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
  302. return 1;
  303. } else if(evt->type == sf::Event::TouchMoved) {
  304. if(ctx->input.mouse.grabbed) {
  305. int x = (int)ctx->input.mouse.prev.x;
  306. int y = (int)ctx->input.mouse.prev.y;
  307. nk_input_motion(ctx, x + evt->touch.x, y + evt->touch.y);
  308. } else nk_input_motion(ctx, evt->touch.x, evt->touch.y);
  309. return 1;
  310. } else if(evt->type == sf::Event::TextEntered) {
  311. nk_input_unicode(ctx, evt->text.unicode);
  312. return 1;
  313. } else if(evt->type == sf::Event::MouseWheelScrolled) {
  314. nk_input_scroll(ctx, nk_vec2(0,evt->mouseWheelScroll.delta));
  315. return 1;
  316. }
  317. return 0;
  318. }
  319. NK_API
  320. void nk_sfml_shutdown(void)
  321. {
  322. struct nk_sfml_device* dev = &sfml.ogl;
  323. nk_font_atlas_clear(&sfml.atlas);
  324. nk_free(&sfml.ctx);
  325. glDeleteTextures(1, &dev->font_tex);
  326. nk_buffer_free(&dev->cmds);
  327. memset(&sfml, 0, sizeof(sfml));
  328. }
  329. #endif