@@ -30,6 +30,7 @@ NK_API void nk_sdl_render(struct nk_context* ctx, enum nk_anti_a
3030NK_API void nk_sdl_shutdown (struct nk_context * ctx );
3131NK_API nk_handle nk_sdl_userdata (struct nk_context * ctx );
3232NK_API void nk_sdl_set_userdata (struct nk_context * ctx , nk_handle userdata );
33+ NK_API void nk_sdl_style_set_font_debug (struct nk_context * ctx );
3334
3435#endif /* NK_SDL3_RENDERER_H_ */
3536
@@ -63,6 +64,7 @@ struct nk_sdl_vertex {
6364struct nk_sdl {
6465 SDL_Window * win ;
6566 SDL_Renderer * renderer ;
67+ struct nk_user_font * font_debug ;
6668 struct nk_sdl_device ogl ;
6769 struct nk_context ctx ;
6870 struct nk_font_atlas atlas ;
@@ -410,9 +412,121 @@ void nk_sdl_shutdown(struct nk_context* ctx)
410412 }
411413
412414 ctx -> userdata = nk_handle_ptr (0 );
415+ SDL_free (sdl -> font_debug );
413416 SDL_free (sdl );
414417 nk_free (ctx );
415418}
416419
420+ /* FIXME: width/height of the debug font atlas (integer)
421+ * Probably needs better name... */
422+ #define WH (10)
423+
424+ NK_INTERN float
425+ nk_sdl_query_debug_font_width (nk_handle handle , float height ,
426+ const char * text , int len )
427+ {
428+ NK_UNUSED (handle );
429+ NK_UNUSED (text );
430+ /* FIXME: does this thing need to handle newlines and tabs ??? */
431+ return height * len ;
432+ }
433+
434+ NK_INTERN void
435+ nk_sdl_query_debug_font_glypth (nk_handle handle , float font_height ,
436+ struct nk_user_font_glyph * glyph ,
437+ nk_rune codepoint , nk_rune next_codepoint )
438+ {
439+ char ascii ;
440+ int idx , x , y ;
441+ NK_UNUSED (next_codepoint );
442+
443+ ascii = (codepoint > (nk_rune )'~' || codepoint < (nk_rune )' ' )
444+ ? '?' : (char )codepoint ;
445+ NK_ASSERT (ascii <= '~' && ascii >= ' ' );
446+
447+ idx = (int )(ascii - ' ' );
448+ NK_ASSERT (idx >= 0 && idx <= (WH * WH ));
449+
450+ x = idx / WH ;
451+ y = idx % WH ;
452+
453+ glyph -> height = font_height ;
454+ glyph -> width = font_height ;
455+ glyph -> xadvance = font_height ;
456+ glyph -> uv [0 ].x = (float )(x + 0 ) / WH ;
457+ glyph -> uv [0 ].y = (float )(y + 0 ) / WH ;
458+ glyph -> uv [1 ].x = (float )(x + 1 ) / WH ;
459+ glyph -> uv [1 ].y = (float )(y + 1 ) / WH ;
460+ glyph -> offset .x = 0.0f ;
461+ glyph -> offset .y = 0.0f ;
462+ }
463+
464+ NK_API void
465+ nk_sdl_style_set_font_debug (struct nk_context * ctx )
466+ {
467+ struct nk_user_font * font ;
468+ struct nk_sdl * sdl ;
469+ SDL_Surface * surface ;
470+ SDL_Renderer * renderer ;
471+ char buf [2 ];
472+ int x , y ;
473+ bool success ;
474+ NK_ASSERT (ctx );
475+
476+ /* FIXME: For now, use another software Renderer just to make sure
477+ * that we won't change any state in the main Renderer,
478+ * but it would be nice to reuse the main one */
479+ surface = SDL_CreateSurface (WH * SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE ,
480+ WH * SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE ,
481+ SDL_PIXELFORMAT_RGBA32 );
482+ NK_ASSERT (surface );
483+ renderer = SDL_CreateSoftwareRenderer (surface );
484+ NK_ASSERT (renderer );
485+ success = SDL_SetRenderDrawColor (renderer , 0xFF , 0xFF , 0xFF , 0xFF );
486+ NK_ASSERT (success );
487+
488+ /* SPACE is the first printable ASCII character */
489+ buf [0 ] = ' ' ;
490+ buf [1 ] = '\0' ;
491+ for (x = 0 ; x < WH ; x ++ )
492+ {
493+ for (y = 0 ; y < WH ; y ++ )
494+ {
495+ success = SDL_RenderDebugText (renderer ,
496+ (float )(x * SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE ),
497+ (float )(y * SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE ),
498+ buf );
499+ NK_ASSERT (success );
500+
501+ buf [0 ]++ ;
502+ /* TILDE is the last printable ASCII character */
503+ if (buf [0 ] > '~' )
504+ break ;
505+ }
506+ }
507+ success = SDL_RenderPresent (renderer );
508+ NK_ASSERT (success );
509+
510+ font = SDL_malloc (sizeof (* font ));
511+ NK_ASSERT (font );
512+ font -> userdata .ptr = NULL ;
513+ font -> height = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE ;
514+ font -> width = & nk_sdl_query_debug_font_width ;
515+ font -> query = & nk_sdl_query_debug_font_glypth ;
516+
517+ /* HACK: nk_sdl_device_upload_atlas turns pixels into SDL_Texture
518+ * and sets said Texture into sdl->ogl.font_tex
519+ * then nk_sdl_render expects same Texture at font->texture */
520+ sdl = (struct nk_sdl * )ctx -> userdata .ptr ;
521+ nk_sdl_device_upload_atlas (& sdl -> ctx , surface -> pixels , surface -> w , surface -> h );
522+ font -> texture .ptr = sdl -> ogl .font_tex ;
523+
524+ sdl -> font_debug = font ;
525+ nk_style_set_font (ctx , font );
526+
527+ SDL_DestroyRenderer (renderer );
528+ SDL_DestroySurface (surface );
529+ }
530+
417531#endif /* NK_SDL3_RENDERER_IMPLEMENTATION_ONCE */
418532#endif /* NK_SDL3_RENDERER_IMPLEMENTATION */
0 commit comments