nuklear_sdl_gl2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Nuklear - 1.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_SDL_GL2_H_
  14. #define NK_SDL_GL2_H_
  15. #include <SDL2/SDL.h>
  16. NK_API struct nk_context* nk_sdl_init(SDL_Window *win);
  17. NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas);
  18. NK_API void nk_sdl_font_stash_end(void);
  19. NK_API int nk_sdl_handle_event(SDL_Event *evt);
  20. NK_API void nk_sdl_render(enum nk_anti_aliasing);
  21. NK_API void nk_sdl_shutdown(void);
  22. #endif
  23. /*
  24. * ==============================================================
  25. *
  26. * IMPLEMENTATION
  27. *
  28. * ===============================================================
  29. */
  30. #ifdef NK_SDL_GL2_IMPLEMENTATION
  31. struct nk_sdl_device {
  32. struct nk_buffer cmds;
  33. struct nk_draw_null_texture null;
  34. GLuint font_tex;
  35. };
  36. struct nk_sdl_vertex {
  37. float position[2];
  38. float uv[2];
  39. nk_byte col[4];
  40. };
  41. static struct nk_sdl {
  42. SDL_Window *win;
  43. struct nk_sdl_device ogl;
  44. struct nk_context ctx;
  45. struct nk_font_atlas atlas;
  46. } sdl;
  47. NK_INTERN void
  48. nk_sdl_device_upload_atlas(const void *image, int width, int height)
  49. {
  50. struct nk_sdl_device *dev = &sdl.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_sdl_render(enum nk_anti_aliasing AA)
  60. {
  61. /* setup global state */
  62. struct nk_sdl_device *dev = &sdl.ogl;
  63. int width, height;
  64. int display_width, display_height;
  65. struct nk_vec2 scale;
  66. SDL_GetWindowSize(sdl.win, &width, &height);
  67. SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height);
  68. scale.x = (float)display_width/(float)width;
  69. scale.y = (float)display_height/(float)height;
  70. glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_TRANSFORM_BIT);
  71. glDisable(GL_CULL_FACE);
  72. glDisable(GL_DEPTH_TEST);
  73. glEnable(GL_SCISSOR_TEST);
  74. glEnable(GL_BLEND);
  75. glEnable(GL_TEXTURE_2D);
  76. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  77. /* setup viewport/project */
  78. glViewport(0,0,(GLsizei)display_width,(GLsizei)display_height);
  79. glMatrixMode(GL_PROJECTION);
  80. glPushMatrix();
  81. glLoadIdentity();
  82. glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f);
  83. glMatrixMode(GL_MODELVIEW);
  84. glPushMatrix();
  85. glLoadIdentity();
  86. glEnableClientState(GL_VERTEX_ARRAY);
  87. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  88. glEnableClientState(GL_COLOR_ARRAY);
  89. {
  90. GLsizei vs = sizeof(struct nk_sdl_vertex);
  91. size_t vp = offsetof(struct nk_sdl_vertex, position);
  92. size_t vt = offsetof(struct nk_sdl_vertex, uv);
  93. size_t vc = offsetof(struct nk_sdl_vertex, col);
  94. /* convert from command queue into draw list and draw to screen */
  95. const struct nk_draw_command *cmd;
  96. const nk_draw_index *offset = NULL;
  97. struct nk_buffer vbuf, ebuf;
  98. /* fill converting configuration */
  99. struct nk_convert_config config;
  100. static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  101. {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sdl_vertex, position)},
  102. {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_sdl_vertex, uv)},
  103. {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_sdl_vertex, col)},
  104. {NK_VERTEX_LAYOUT_END}
  105. };
  106. NK_MEMSET(&config, 0, sizeof(config));
  107. config.vertex_layout = vertex_layout;
  108. config.vertex_size = sizeof(struct nk_sdl_vertex);
  109. config.vertex_alignment = NK_ALIGNOF(struct nk_sdl_vertex);
  110. config.null = dev->null;
  111. config.circle_segment_count = 22;
  112. config.curve_segment_count = 22;
  113. config.arc_segment_count = 22;
  114. config.global_alpha = 1.0f;
  115. config.shape_AA = AA;
  116. config.line_AA = AA;
  117. /* convert shapes into vertexes */
  118. nk_buffer_init_default(&vbuf);
  119. nk_buffer_init_default(&ebuf);
  120. nk_convert(&sdl.ctx, &dev->cmds, &vbuf, &ebuf, &config);
  121. /* setup vertex buffer pointer */
  122. {const void *vertices = nk_buffer_memory_const(&vbuf);
  123. glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp));
  124. glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt));
  125. glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));}
  126. /* iterate over and execute each draw command */
  127. offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf);
  128. nk_draw_foreach(cmd, &sdl.ctx, &dev->cmds)
  129. {
  130. if (!cmd->elem_count) continue;
  131. glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
  132. glScissor(
  133. (GLint)(cmd->clip_rect.x * scale.x),
  134. (GLint)((height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * scale.y),
  135. (GLint)(cmd->clip_rect.w * scale.x),
  136. (GLint)(cmd->clip_rect.h * scale.y));
  137. glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
  138. offset += cmd->elem_count;
  139. }
  140. nk_clear(&sdl.ctx);
  141. nk_buffer_clear(&dev->cmds);
  142. nk_buffer_free(&vbuf);
  143. nk_buffer_free(&ebuf);
  144. }
  145. /* default OpenGL state */
  146. glDisableClientState(GL_VERTEX_ARRAY);
  147. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  148. glDisableClientState(GL_COLOR_ARRAY);
  149. glDisable(GL_CULL_FACE);
  150. glDisable(GL_DEPTH_TEST);
  151. glDisable(GL_SCISSOR_TEST);
  152. glDisable(GL_BLEND);
  153. glDisable(GL_TEXTURE_2D);
  154. glBindTexture(GL_TEXTURE_2D, 0);
  155. glMatrixMode(GL_MODELVIEW);
  156. glPopMatrix();
  157. glMatrixMode(GL_PROJECTION);
  158. glPopMatrix();
  159. glPopAttrib();
  160. }
  161. static void
  162. nk_sdl_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
  163. {
  164. const char *text = SDL_GetClipboardText();
  165. if (text) nk_textedit_paste(edit, text, nk_strlen(text));
  166. (void)usr;
  167. }
  168. static void
  169. nk_sdl_clipboard_copy(nk_handle usr, const char *text, int len)
  170. {
  171. char *str = 0;
  172. (void)usr;
  173. if (!len) return;
  174. str = (char*)malloc((size_t)len+1);
  175. if (!str) return;
  176. memcpy(str, text, (size_t)len);
  177. str[len] = '\0';
  178. SDL_SetClipboardText(str);
  179. free(str);
  180. }
  181. NK_API struct nk_context*
  182. nk_sdl_init(SDL_Window *win)
  183. {
  184. sdl.win = win;
  185. nk_init_default(&sdl.ctx, 0);
  186. sdl.ctx.clip.copy = nk_sdl_clipboard_copy;
  187. sdl.ctx.clip.paste = nk_sdl_clipboard_paste;
  188. sdl.ctx.clip.userdata = nk_handle_ptr(0);
  189. nk_buffer_init_default(&sdl.ogl.cmds);
  190. return &sdl.ctx;
  191. }
  192. NK_API void
  193. nk_sdl_font_stash_begin(struct nk_font_atlas **atlas)
  194. {
  195. nk_font_atlas_init_default(&sdl.atlas);
  196. nk_font_atlas_begin(&sdl.atlas);
  197. *atlas = &sdl.atlas;
  198. }
  199. NK_API void
  200. nk_sdl_font_stash_end(void)
  201. {
  202. const void *image; int w, h;
  203. image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
  204. nk_sdl_device_upload_atlas(image, w, h);
  205. nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.null);
  206. if (sdl.atlas.default_font)
  207. nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle);
  208. }
  209. NK_API int
  210. nk_sdl_handle_event(SDL_Event *evt)
  211. {
  212. struct nk_context *ctx = &sdl.ctx;
  213. /* optional grabbing behavior */
  214. if (ctx->input.mouse.grab) {
  215. SDL_SetRelativeMouseMode(SDL_TRUE);
  216. ctx->input.mouse.grab = 0;
  217. } else if (ctx->input.mouse.ungrab) {
  218. int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
  219. SDL_SetRelativeMouseMode(SDL_FALSE);
  220. SDL_WarpMouseInWindow(sdl.win, x, y);
  221. ctx->input.mouse.ungrab = 0;
  222. }
  223. if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
  224. /* key events */
  225. int down = evt->type == SDL_KEYDOWN;
  226. const Uint8* state = SDL_GetKeyboardState(0);
  227. SDL_Keycode sym = evt->key.keysym.sym;
  228. if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT)
  229. nk_input_key(ctx, NK_KEY_SHIFT, down);
  230. else if (sym == SDLK_DELETE)
  231. nk_input_key(ctx, NK_KEY_DEL, down);
  232. else if (sym == SDLK_RETURN)
  233. nk_input_key(ctx, NK_KEY_ENTER, down);
  234. else if (sym == SDLK_TAB)
  235. nk_input_key(ctx, NK_KEY_TAB, down);
  236. else if (sym == SDLK_BACKSPACE)
  237. nk_input_key(ctx, NK_KEY_BACKSPACE, down);
  238. else if (sym == SDLK_HOME) {
  239. nk_input_key(ctx, NK_KEY_TEXT_START, down);
  240. nk_input_key(ctx, NK_KEY_SCROLL_START, down);
  241. } else if (sym == SDLK_END) {
  242. nk_input_key(ctx, NK_KEY_TEXT_END, down);
  243. nk_input_key(ctx, NK_KEY_SCROLL_END, down);
  244. } else if (sym == SDLK_PAGEDOWN) {
  245. nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
  246. } else if (sym == SDLK_PAGEUP) {
  247. nk_input_key(ctx, NK_KEY_SCROLL_UP, down);
  248. } else if (sym == SDLK_z)
  249. nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && state[SDL_SCANCODE_LCTRL]);
  250. else if (sym == SDLK_r)
  251. nk_input_key(ctx, NK_KEY_TEXT_REDO, down && state[SDL_SCANCODE_LCTRL]);
  252. else if (sym == SDLK_c)
  253. nk_input_key(ctx, NK_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]);
  254. else if (sym == SDLK_v)
  255. nk_input_key(ctx, NK_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]);
  256. else if (sym == SDLK_x)
  257. nk_input_key(ctx, NK_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]);
  258. else if (sym == SDLK_b)
  259. nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && state[SDL_SCANCODE_LCTRL]);
  260. else if (sym == SDLK_e)
  261. nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && state[SDL_SCANCODE_LCTRL]);
  262. else if (sym == SDLK_UP)
  263. nk_input_key(ctx, NK_KEY_UP, down);
  264. else if (sym == SDLK_DOWN)
  265. nk_input_key(ctx, NK_KEY_DOWN, down);
  266. else if (sym == SDLK_LEFT) {
  267. if (state[SDL_SCANCODE_LCTRL])
  268. nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
  269. else nk_input_key(ctx, NK_KEY_LEFT, down);
  270. } else if (sym == SDLK_RIGHT) {
  271. if (state[SDL_SCANCODE_LCTRL])
  272. nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  273. else nk_input_key(ctx, NK_KEY_RIGHT, down);
  274. } else return 0;
  275. return 1;
  276. } else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) {
  277. /* mouse button */
  278. int down = evt->type == SDL_MOUSEBUTTONDOWN;
  279. const int x = evt->button.x, y = evt->button.y;
  280. if (evt->button.button == SDL_BUTTON_LEFT) {
  281. if (evt->button.clicks > 1)
  282. nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, down);
  283. nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
  284. } else if (evt->button.button == SDL_BUTTON_MIDDLE)
  285. nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
  286. else if (evt->button.button == SDL_BUTTON_RIGHT)
  287. nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
  288. return 1;
  289. } else if (evt->type == SDL_MOUSEMOTION) {
  290. /* mouse motion */
  291. if (ctx->input.mouse.grabbed) {
  292. int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
  293. nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel);
  294. } else nk_input_motion(ctx, evt->motion.x, evt->motion.y);
  295. return 1;
  296. } else if (evt->type == SDL_TEXTINPUT) {
  297. /* text input */
  298. nk_glyph glyph;
  299. memcpy(glyph, evt->text.text, NK_UTF_SIZE);
  300. nk_input_glyph(ctx, glyph);
  301. return 1;
  302. } else if (evt->type == SDL_MOUSEWHEEL) {
  303. /* mouse wheel */
  304. nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y));
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. NK_API
  310. void nk_sdl_shutdown(void)
  311. {
  312. struct nk_sdl_device *dev = &sdl.ogl;
  313. nk_font_atlas_clear(&sdl.atlas);
  314. nk_free(&sdl.ctx);
  315. glDeleteTextures(1, &dev->font_tex);
  316. nk_buffer_free(&dev->cmds);
  317. memset(&sdl, 0, sizeof(sdl));
  318. }
  319. #endif