comparison nuklear_ui/nuklear_sdl_gles2.h @ 2308:b7768c58f0da

Initial stab at DPI scaling support
author Michael Pavone <pavone@retrodev.com>
date Sun, 26 Mar 2023 22:39:18 -0700
parents 1838b0b8f4ff
children 5e34369ed6be
comparison
equal deleted inserted replaced
2307:a8080240cb92 2308:b7768c58f0da
21 #include <SDL_opengles2.h> 21 #include <SDL_opengles2.h>
22 #else 22 #else
23 #include <GL/glew.h> 23 #include <GL/glew.h>
24 #endif 24 #endif
25 #endif 25 #endif
26 #include "../render.h"
26 27
27 28
28 NK_API struct nk_context* nk_sdl_init(SDL_Window *win); 29 NK_API struct nk_context* nk_sdl_init(SDL_Window *win);
29 NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas); 30 NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas);
30 NK_API void nk_sdl_font_stash_end(void); 31 NK_API void nk_sdl_font_stash_end(void);
116 "void main(){\n" 117 "void main(){\n"
117 " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV);\n" 118 " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV);\n"
118 "}\n"; 119 "}\n";
119 120
120 struct nk_sdl_device *dev = &sdl.ogl; 121 struct nk_sdl_device *dev = &sdl.ogl;
121 122
122 nk_buffer_init_default(&dev->cmds); 123 nk_buffer_init_default(&dev->cmds);
123 dev->prog = glCreateProgram(); 124 dev->prog = glCreateProgram();
124 dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER); 125 dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER);
125 dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER); 126 dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER);
126 glShaderSource(dev->vert_shdr, 1, &vertex_shader, 0); 127 glShaderSource(dev->vert_shdr, 1, &vertex_shader, 0);
146 { 147 {
147 dev->vs = sizeof(struct nk_sdl_vertex); 148 dev->vs = sizeof(struct nk_sdl_vertex);
148 dev->vp = offsetof(struct nk_sdl_vertex, position); 149 dev->vp = offsetof(struct nk_sdl_vertex, position);
149 dev->vt = offsetof(struct nk_sdl_vertex, uv); 150 dev->vt = offsetof(struct nk_sdl_vertex, uv);
150 dev->vc = offsetof(struct nk_sdl_vertex, col); 151 dev->vc = offsetof(struct nk_sdl_vertex, col);
151 152
152 /* Allocate buffers */ 153 /* Allocate buffers */
153 glGenBuffers(1, &dev->vbo); 154 glGenBuffers(1, &dev->vbo);
154 glGenBuffers(1, &dev->ebo); 155 glGenBuffers(1, &dev->ebo);
155 } 156 }
156 glBindTexture(GL_TEXTURE_2D, 0); 157 glBindTexture(GL_TEXTURE_2D, 0);
231 const nk_draw_index *offset = NULL; 232 const nk_draw_index *offset = NULL;
232 233
233 /* Bind buffers */ 234 /* Bind buffers */
234 glBindBuffer(GL_ARRAY_BUFFER, dev->vbo); 235 glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
235 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo); 236 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
236 237
237 { 238 {
238 /* buffer setup */ 239 /* buffer setup */
239 glEnableVertexAttribArray((GLuint)dev->attrib_pos); 240 glEnableVertexAttribArray((GLuint)dev->attrib_pos);
240 glEnableVertexAttribArray((GLuint)dev->attrib_uv); 241 glEnableVertexAttribArray((GLuint)dev->attrib_uv);
241 glEnableVertexAttribArray((GLuint)dev->attrib_col); 242 glEnableVertexAttribArray((GLuint)dev->attrib_col);
443 } 444 }
444 } 445 }
445 } else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) { 446 } else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) {
446 /* mouse button */ 447 /* mouse button */
447 int down = evt->type == SDL_MOUSEBUTTONDOWN; 448 int down = evt->type == SDL_MOUSEBUTTONDOWN;
448 const int x = evt->button.x, y = evt->button.y; 449 const int x = render_ui_to_pixels_x(evt->button.x), y = render_ui_to_pixels_y(evt->button.y);
449 if (evt->button.button == SDL_BUTTON_LEFT) { 450 if (evt->button.button == SDL_BUTTON_LEFT) {
450 if (evt->button.clicks > 1) 451 if (evt->button.clicks > 1)
451 nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, down); 452 nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, down);
452 nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); 453 nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
453 } else if (evt->button.button == SDL_BUTTON_MIDDLE) 454 } else if (evt->button.button == SDL_BUTTON_MIDDLE)
456 nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); 457 nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
457 return 1; 458 return 1;
458 } else if (evt->type == SDL_MOUSEMOTION) { 459 } else if (evt->type == SDL_MOUSEMOTION) {
459 /* mouse motion */ 460 /* mouse motion */
460 if (ctx->input.mouse.grabbed) { 461 if (ctx->input.mouse.grabbed) {
461 int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y; 462 int x = render_ui_to_pixels_x((int)ctx->input.mouse.prev.x), y = render_ui_to_pixels_y((int)ctx->input.mouse.prev.y);
462 nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel); 463 nk_input_motion(ctx, x + render_ui_to_pixels_x(evt->motion.xrel), y + render_ui_to_pixels_y(evt->motion.yrel));
463 } else nk_input_motion(ctx, evt->motion.x, evt->motion.y); 464 } else nk_input_motion(ctx, render_ui_to_pixels_x(evt->motion.x), render_ui_to_pixels_y(evt->motion.y));
464 return 1; 465 return 1;
465 } else if (evt->type == SDL_TEXTINPUT) { 466 } else if (evt->type == SDL_TEXTINPUT) {
466 /* text input */ 467 /* text input */
467 nk_glyph glyph; 468 nk_glyph glyph;
468 memcpy(glyph, evt->text.text, NK_UTF_SIZE); 469 memcpy(glyph, evt->text.text, NK_UTF_SIZE);