comparison nuklear_ui/nuklear_sdl_gles2.h @ 2693:46dba737b931

WIP Nuklear UI in VDP debug windows
author Michael Pavone <pavone@retrodev.com>
date Thu, 19 Jun 2025 19:59:05 -0700
parents 568c1c22f3e3
children
comparison
equal deleted inserted replaced
2692:effbb52ab3f0 2693:46dba737b931
27 #include "../render.h" 27 #include "../render.h"
28 #include "../controller_info.h" 28 #include "../controller_info.h"
29 29
30 30
31 NK_API struct nk_context* nk_sdl_init(SDL_Window *win); 31 NK_API struct nk_context* nk_sdl_init(SDL_Window *win);
32 NK_API void nk_sdl_font_stash_begin(struct nk_font_atlas **atlas); 32 NK_API void nk_sdl_font_stash_begin(struct nk_context *ctx, struct nk_font_atlas **atlas);
33 NK_API void nk_sdl_font_stash_end(void); 33 NK_API void nk_sdl_font_stash_end(struct nk_context *ctx);
34 NK_API int nk_sdl_handle_event(SDL_Event *evt); 34 NK_API int nk_sdl_handle_event(struct nk_context *ctx, SDL_Event *evt);
35 NK_API void nk_sdl_render(enum nk_anti_aliasing , int max_vertex_buffer, int max_element_buffer); 35 NK_API void nk_sdl_render(struct nk_context *ctx, enum nk_anti_aliasing , int max_vertex_buffer, int max_element_buffer);
36 NK_API void nk_sdl_shutdown(void); 36 NK_API void nk_sdl_shutdown(struct nk_context *ctx);
37 NK_API void nk_sdl_device_destroy(void); 37 NK_API void nk_sdl_device_destroy(struct nk_context *ctx);
38 NK_API void nk_sdl_device_create(void); 38 NK_API void nk_sdl_device_create(struct nk_context *ctx);
39 39
40 #endif 40 #endif
41 41
42 /* 42 /*
43 * ============================================================== 43 * ==============================================================
73 GLfloat uv[2]; 73 GLfloat uv[2];
74 nk_byte col[4]; 74 nk_byte col[4];
75 }; 75 };
76 #endif 76 #endif
77 77
78 static struct nk_sdl { 78 struct nk_sdl {
79 struct nk_context ctx;
79 SDL_Window *win; 80 SDL_Window *win;
80 #ifndef DISABLE_OPENGL 81 #ifndef DISABLE_OPENGL
81 struct nk_sdl_device ogl; 82 struct nk_sdl_device ogl;
82 #endif 83 #endif
83 struct nk_context ctx;
84 struct nk_font_atlas atlas; 84 struct nk_font_atlas atlas;
85 } sdl; 85 };
86 86
87 #ifdef USE_GLES 87 #ifdef USE_GLES
88 #define NK_SHADER_VERSION "#version 100\n" 88 #define NK_SHADER_VERSION "#version 100\n"
89 #define DECLARE_PRECISION "precision mediump float;\n" 89 #define DECLARE_PRECISION "precision mediump float;\n"
90 #else 90 #else
92 #define DECLARE_PRECISION 92 #define DECLARE_PRECISION
93 #endif 93 #endif
94 94
95 #ifndef DISABLE_OPENGL 95 #ifndef DISABLE_OPENGL
96 NK_API void 96 NK_API void
97 nk_sdl_device_create(void) 97 nk_sdl_device_create(struct nk_context *ctx)
98 { 98 {
99 GLint status; 99 GLint status;
100 struct nk_sdl *sdl = (struct nk_sdl *)ctx;
100 static const GLchar *vertex_shader = 101 static const GLchar *vertex_shader =
101 NK_SHADER_VERSION 102 NK_SHADER_VERSION
102 "uniform mat4 ProjMtx;\n" 103 "uniform mat4 ProjMtx;\n"
103 "attribute vec2 Position;\n" 104 "attribute vec2 Position;\n"
104 "attribute vec2 TexCoord;\n" 105 "attribute vec2 TexCoord;\n"
118 "varying vec4 Frag_Color;\n" 119 "varying vec4 Frag_Color;\n"
119 "void main(){\n" 120 "void main(){\n"
120 " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV);\n" 121 " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV);\n"
121 "}\n"; 122 "}\n";
122 123
123 struct nk_sdl_device *dev = &sdl.ogl; 124 struct nk_sdl_device *dev = &sdl->ogl;
124 125
125 nk_buffer_init_default(&dev->cmds); 126 nk_buffer_init_default(&dev->cmds);
126 dev->prog = glCreateProgram(); 127 dev->prog = glCreateProgram();
127 dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER); 128 dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER);
128 dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER); 129 dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER);
160 glBindBuffer(GL_ARRAY_BUFFER, 0); 161 glBindBuffer(GL_ARRAY_BUFFER, 0);
161 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 162 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
162 } 163 }
163 164
164 NK_INTERN void 165 NK_INTERN void
165 nk_sdl_device_upload_atlas(const void *image, int width, int height) 166 nk_sdl_device_upload_atlas(struct nk_context *ctx, const void *image, int width, int height)
166 { 167 {
167 struct nk_sdl_device *dev = &sdl.ogl; 168 struct nk_sdl *sdl = (struct nk_sdl *)ctx;
169 struct nk_sdl_device *dev = &sdl->ogl;
168 glGenTextures(1, &dev->font_tex); 170 glGenTextures(1, &dev->font_tex);
169 glBindTexture(GL_TEXTURE_2D, dev->font_tex); 171 glBindTexture(GL_TEXTURE_2D, dev->font_tex);
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 172 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 173 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
172 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, 174 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
176 printf("glTexImage2D failed with error %d\n", err); 178 printf("glTexImage2D failed with error %d\n", err);
177 } 179 }
178 } 180 }
179 181
180 NK_API void 182 NK_API void
181 nk_sdl_device_destroy(void) 183 nk_sdl_device_destroy(struct nk_context *ctx)
182 { 184 {
183 struct nk_sdl_device *dev = &sdl.ogl; 185 struct nk_sdl *sdl = (struct nk_sdl *)ctx;
186 struct nk_sdl_device *dev = &sdl->ogl;
184 glDetachShader(dev->prog, dev->vert_shdr); 187 glDetachShader(dev->prog, dev->vert_shdr);
185 glDetachShader(dev->prog, dev->frag_shdr); 188 glDetachShader(dev->prog, dev->frag_shdr);
186 glDeleteShader(dev->vert_shdr); 189 glDeleteShader(dev->vert_shdr);
187 glDeleteShader(dev->frag_shdr); 190 glDeleteShader(dev->frag_shdr);
188 glDeleteProgram(dev->prog); 191 glDeleteProgram(dev->prog);
191 glDeleteBuffers(1, &dev->ebo); 194 glDeleteBuffers(1, &dev->ebo);
192 nk_buffer_free(&dev->cmds); 195 nk_buffer_free(&dev->cmds);
193 } 196 }
194 197
195 NK_API void 198 NK_API void
196 nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer) 199 nk_sdl_render(struct nk_context *ctx, enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
197 { 200 {
198 struct nk_sdl_device *dev = &sdl.ogl; 201 struct nk_sdl *sdl = (struct nk_sdl *)ctx;
202 struct nk_sdl_device *dev = &sdl->ogl;
199 int display_width, display_height; 203 int display_width, display_height;
200 struct nk_vec2 scale; 204 struct nk_vec2 scale;
201 GLfloat ortho[4][4] = { 205 GLfloat ortho[4][4] = {
202 {2.0f, 0.0f, 0.0f, 0.0f}, 206 {2.0f, 0.0f, 0.0f, 0.0f},
203 {0.0f,-2.0f, 0.0f, 0.0f}, 207 {0.0f,-2.0f, 0.0f, 0.0f},
204 {0.0f, 0.0f,-1.0f, 0.0f}, 208 {0.0f, 0.0f,-1.0f, 0.0f},
205 {-1.0f,1.0f, 0.0f, 1.0f}, 209 {-1.0f,1.0f, 0.0f, 1.0f},
206 }; 210 };
207 SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height); 211 SDL_GL_GetDrawableSize(sdl->win, &display_width, &display_height);
208 ortho[0][0] /= (GLfloat)display_width; 212 ortho[0][0] /= (GLfloat)display_width;
209 ortho[1][1] /= (GLfloat)display_height; 213 ortho[1][1] /= (GLfloat)display_height;
210 214
211 scale.x = 1.0f; 215 scale.x = 1.0f;
212 scale.y = 1.0f; 216 scale.y = 1.0f;
275 config.line_AA = AA; 279 config.line_AA = AA;
276 280
277 /* setup buffers to load vertices and elements */ 281 /* setup buffers to load vertices and elements */
278 nk_buffer_init_fixed(&vbuf, vertices, (nk_size)max_vertex_buffer); 282 nk_buffer_init_fixed(&vbuf, vertices, (nk_size)max_vertex_buffer);
279 nk_buffer_init_fixed(&ebuf, elements, (nk_size)max_element_buffer); 283 nk_buffer_init_fixed(&ebuf, elements, (nk_size)max_element_buffer);
280 nk_convert(&sdl.ctx, &dev->cmds, &vbuf, &ebuf, &config); 284 nk_convert(ctx, &dev->cmds, &vbuf, &ebuf, &config);
281 } 285 }
282 glBufferSubData(GL_ARRAY_BUFFER, 0, (size_t)max_vertex_buffer, vertices); 286 glBufferSubData(GL_ARRAY_BUFFER, 0, (size_t)max_vertex_buffer, vertices);
283 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, (size_t)max_element_buffer, elements); 287 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, (size_t)max_element_buffer, elements);
284 free(vertices); 288 free(vertices);
285 free(elements); 289 free(elements);
286 290
287 /* iterate over and execute each draw command */ 291 /* iterate over and execute each draw command */
288 nk_draw_foreach(cmd, &sdl.ctx, &dev->cmds) { 292 nk_draw_foreach(cmd, ctx, &dev->cmds) {
289 if (!cmd->elem_count) continue; 293 if (!cmd->elem_count) continue;
290 glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id); 294 glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
291 glScissor((GLint)(cmd->clip_rect.x * scale.x), 295 glScissor((GLint)(cmd->clip_rect.x * scale.x),
292 (GLint)((display_height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * scale.y), 296 (GLint)((display_height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * scale.y),
293 (GLint)(cmd->clip_rect.w * scale.x), 297 (GLint)(cmd->clip_rect.w * scale.x),
294 (GLint)(cmd->clip_rect.h * scale.y)); 298 (GLint)(cmd->clip_rect.h * scale.y));
295 glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset); 299 glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
296 offset += cmd->elem_count; 300 offset += cmd->elem_count;
297 } 301 }
298 nk_clear(&sdl.ctx); 302 nk_clear(ctx);
299 glDisableVertexAttribArray((GLuint)dev->attrib_pos); 303 glDisableVertexAttribArray((GLuint)dev->attrib_pos);
300 glDisableVertexAttribArray((GLuint)dev->attrib_uv); 304 glDisableVertexAttribArray((GLuint)dev->attrib_uv);
301 glDisableVertexAttribArray((GLuint)dev->attrib_col); 305 glDisableVertexAttribArray((GLuint)dev->attrib_col);
302 } 306 }
303 307
333 } 337 }
334 338
335 NK_API struct nk_context* 339 NK_API struct nk_context*
336 nk_sdl_init(SDL_Window *win) 340 nk_sdl_init(SDL_Window *win)
337 { 341 {
338 sdl.win = win; 342 struct nk_sdl *sdl = calloc(1, sizeof(struct nk_sdl));
339 nk_init_default(&sdl.ctx, 0); 343 sdl->win = win;
340 sdl.ctx.clip.copy = nk_sdl_clipbard_copy; 344 nk_init_default(&sdl->ctx, 0);
341 sdl.ctx.clip.paste = nk_sdl_clipbard_paste; 345 sdl->ctx.clip.copy = nk_sdl_clipbard_copy;
342 sdl.ctx.clip.userdata = nk_handle_ptr(0); 346 sdl->ctx.clip.paste = nk_sdl_clipbard_paste;
343 return &sdl.ctx; 347 sdl->ctx.clip.userdata = nk_handle_ptr(0);
348 return &sdl->ctx;
344 } 349 }
345 350
346 #ifndef DISABLE_OPENGL 351 #ifndef DISABLE_OPENGL
347 NK_API void 352 NK_API void
348 nk_sdl_font_stash_begin(struct nk_font_atlas **atlas) 353 nk_sdl_font_stash_begin(struct nk_context *ctx, struct nk_font_atlas **atlas)
349 { 354 {
350 nk_font_atlas_init_default(&sdl.atlas); 355 struct nk_sdl *sdl = (struct nk_sdl *)ctx;
351 nk_font_atlas_begin(&sdl.atlas); 356 nk_font_atlas_init_default(&sdl->atlas);
352 *atlas = &sdl.atlas; 357 nk_font_atlas_begin(&sdl->atlas);
358 *atlas = &sdl->atlas;
353 } 359 }
354 360
355 NK_API void 361 NK_API void
356 nk_sdl_font_stash_end(void) 362 nk_sdl_font_stash_end(struct nk_context *ctx)
357 { 363 {
364 struct nk_sdl *sdl = (struct nk_sdl *)ctx;
358 const void *image; int w, h; 365 const void *image; int w, h;
359 image = nk_font_atlas_bake(&sdl.atlas, &w, &h, NK_FONT_ATLAS_RGBA32); 366 image = nk_font_atlas_bake(&sdl->atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
360 nk_sdl_device_upload_atlas(image, w, h); 367 nk_sdl_device_upload_atlas(ctx, image, w, h);
361 nk_font_atlas_end(&sdl.atlas, nk_handle_id((int)sdl.ogl.font_tex), &sdl.ogl.null); 368 nk_font_atlas_end(&sdl->atlas, nk_handle_id((int)sdl->ogl.font_tex), &sdl->ogl.null);
362 if (sdl.atlas.default_font) 369 if (sdl->atlas.default_font)
363 nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle); 370 nk_style_set_font(&sdl->ctx, &sdl->atlas.default_font->handle);
364 371
365 } 372 }
366 #endif 373 #endif
367 374
368 NK_API int 375 NK_API int
369 nk_sdl_handle_event(SDL_Event *evt) 376 nk_sdl_handle_event(struct nk_context *ctx, SDL_Event *evt)
370 { 377 {
371 struct nk_context *ctx = &sdl.ctx; 378 struct nk_sdl *sdl = (struct nk_sdl *)ctx;
372 if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) { 379 if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
373 /* key events */ 380 /* key events */
374 int down = evt->type == SDL_KEYDOWN; 381 int down = evt->type == SDL_KEYDOWN;
375 const Uint8* state = SDL_GetKeyboardState(0); 382 const Uint8* state = SDL_GetKeyboardState(0);
376 SDL_Keycode sym = evt->key.keysym.sym; 383 SDL_Keycode sym = evt->key.keysym.sym;
485 } 492 }
486 return 0; 493 return 0;
487 } 494 }
488 495
489 NK_API 496 NK_API
490 void nk_sdl_shutdown(void) 497 void nk_sdl_shutdown(struct nk_context *ctx)
491 { 498 {
492 nk_font_atlas_clear(&sdl.atlas); 499 struct nk_sdl *sdl = (struct nk_sdl *)ctx;
493 nk_free(&sdl.ctx); 500 nk_font_atlas_clear(&sdl->atlas);
501 nk_free(&sdl->ctx);
494 #ifndef DISABLE_OPENGL 502 #ifndef DISABLE_OPENGL
495 nk_sdl_device_destroy(); 503 nk_sdl_device_destroy(ctx);
496 #endif 504 #endif
497 memset(&sdl, 0, sizeof(sdl)); 505 free(sdl);
498 } 506 }
499 507
500 #endif 508 #endif