#979

SDL3: When Font is NULL, use `SDL_RenderDebugText()`

OpenRobLoachopened 3mo ago

Allow using SDL3 renderer without a loaded font? Use SDL_RenderDebugText

Activity

16
sleeptightAnsiC3mo ago

I'm not entirely sure what "when Font is NULL" exactly means here, like did you mean passing NULL as nk_user_font to nk_init? I guess it is possible. Implementing this depends on your drawing loop and how it handles the fonts.

demo/sdl3_renderer already partially supports something similar with nk_sdl_style_set_debug_font(). You can enable it by flipping #if 0 on this line (but beware some of the styling code below that line might be broken since https://github.com/Immediate-Mode-UI/Nuklear/pull/924)

There are basically to ways:

Option 1: If you're using the vertex API (aka NK_INCLUDE_VERTEX_BUFFER_OUTPUT, nk_convert, nk_draw_foreach, etc...) take a look here: https://github.com/Immediate-Mode-UI/Nuklear/blob/0dbc52f86404f9e1f26ce0df3015ed23ff54a726/demo/sdl3_renderer/nuklear_sdl3_renderer.h#L596-L716 ^ this code renders the glyphs to the texture and treats it like font atlas with hardcoded values. I don't believe there is a different way to achieve it in this specific case.

Option 2: If you're using high level drawing commands (aka nk_command, nk_foreach, nk_command_text, etc...) then you can even call SDL_RenderDebugText directly. You just need to reuse nk_sdl_query_debug_font_width from above, and use something like this:

case NK_COMMAND_TEXT: {
    const struct nk_command_text *c = (const struct nk_command_text*)cmd;
    SDL_SetRenderDrawColor(your_renderer,
                           c->foreground.r,
                           c->foreground.g,
                           c->foreground.b,
                           c->foreground.a);
    SDL_RenderDebugText(your_renderer,
                        (float)c->x,
                        (float)c->y,
                        c->string);
} break;

^ IIRC, this ignores some nk_command_text fields and may not handle foreground.a correctly (you may need to turn on blending, don't really remember all the details now)

sleeptightAnsiC3mo ago

I'm not entirely sure what "when Font is NULL" exactly means here, like passing NULL as nk_user_font to nk_init? I guess it is possible. Implementing this depends on your drawing loop and how it handles the fonts. demo/sdl3_renderer already partially supports something similar with nk_sdl_style_set_debug_font(). You can enable it by flipping #if 0 on this line

There are basically to ways:

Option 1: If you're using the vertex API (aka NK_INCLUDE_VERTEX_BUFFER_OUTPUT, nk_convert, nk_draw_foreach, etc...) take a look here: https://github.com/Immediate-Mode-UI/Nuklear/blob/0dbc52f86404f9e1f26ce0df3015ed23ff54a726/demo/sdl3_renderer/nuklear_sdl3_renderer.h#L596-L716 ^ this code renders the glyphs to the texture and treats it like font atlas with hardcoded values. I don't believe there is a any better way to achieve it in this specific case.

Option 2: If you're using high level drawing commands (aka nk_command, nk_foreach, nk_command_text, etc...) then you can even call SDL_RenderDebugText directly. You just need to reuse nk_sdl_query_debug_font_width from above, and use something like this:

case NK_COMMAND_TEXT: {
    const struct nk_command_text *c = (const struct nk_command_text*)cmd;
    SDL_SetRenderDrawColor(your_renderer,
                           c->foreground.r,
                           c->foreground.g,
                           c->foreground.b,
                           c->foreground.a);
    SDL_RenderDebugText(your_renderer,
                        (float)c->x,
                        (float)c->y,
                        c->string);
} break;

^ IIRC, this ignores some nk_command_text fields and may not handle foreground.a correctly (you may need to turn on blending, don't really remember all the details now)

sleeptightAnsiC3mo ago

I'm not entirely sure what "when Font is NULL" exactly means here, like passing NULL as nk_user_font to nk_init? I guess it is possible. Implementing this depends on your drawing loop and how it handles the fonts. demo/sdl3_renderer already partially supports something similar with nk_sdl_style_set_debug_font(). You can enable it by flipping #if 0 on this line

There are basically to ways:

Option 1: If you're using the vertex API (aka NK_INCLUDE_VERTEX_BUFFER_OUTPUT, nk_convert, nk_draw_foreach, etc...) take a look here: https://github.com/Immediate-Mode-UI/Nuklear/blob/0dbc52f86404f9e1f26ce0df3015ed23ff54a726/demo/sdl3_renderer/nuklear_sdl3_renderer.h#L596-L716 ^ this code renders the glyphs to the texture and treats it like font atlas with hardcoded values. I don't believe there is a any better way to achieve it in this specific case.

Option 2: If you're using high level drawing commands (aka nk_command, nk_foreach, nk_command_text, etc...) then you can even call SDL_RenderDebugText directly. You just need to reuse nk_sdl_query_debug_font_width from above, and use something like this:

case NK_COMMAND_TEXT: {
    const struct nk_command_text *c = (const struct nk_command_text*)cmd;
    SDL_SetRenderDrawColor(your_renderer,
                           c->foreground.r,
                           c->foreground.g,
                           c->foreground.b,
                           c->foreground.a);
    SDL_RenderDebugText(your_renderer,
                        (float)c->x,
                        (float)c->y,
                        c->string);
} break;

^ IIRC, this ignores some nk_command_text fields and may not handle foreground.a correctly (you may need to turn on blending, don't really remember all the details now)

sleeptightAnsiC3mo ago

I'm not entirely sure what "when Font is NULL" exactly means here, like passing NULL as nk_user_font to nk_init? I guess it is possible. Implementing this depends on your drawing loop and how it handles the fonts. demo/sdl3_renderer already partially supports something similar with nk_sdl_style_set_debug_font(). You can enable it by flipping #if 0 on this line

There are basically two ways:

Option 1: If you're using the vertex API (aka NK_INCLUDE_VERTEX_BUFFER_OUTPUT, nk_convert, nk_draw_foreach, etc...) take a look here: https://github.com/Immediate-Mode-UI/Nuklear/blob/0dbc52f86404f9e1f26ce0df3015ed23ff54a726/demo/sdl3_renderer/nuklear_sdl3_renderer.h#L596-L716 ^ this code renders the glyphs to the texture and treats it like font atlas with hardcoded values. I don't believe there is a any better way to achieve it in this specific case.

Option 2: If you're using high level drawing commands (aka nk_command, nk_foreach, nk_command_text, etc...) then you can even call SDL_RenderDebugText directly. You just need to reuse nk_sdl_query_debug_font_width from above, and use something like this:

case NK_COMMAND_TEXT: {
    const struct nk_command_text *c = (const struct nk_command_text*)cmd;
    SDL_SetRenderDrawColor(your_renderer,
                           c->foreground.r,
                           c->foreground.g,
                           c->foreground.b,
                           c->foreground.a);
    SDL_RenderDebugText(your_renderer,
                        (float)c->x,
                        (float)c->y,
                        c->string);
} break;

^ IIRC, this ignores some nk_command_text fields and may not handle foreground.a correctly (you may need to turn on blending, don't really remember all the details now)

sleeptightAnsiC3mo ago

You can enable it by flipping #if 0 on this line

this is how it looks like with demo/sdl3_renderer:

CLICK ME

and this is how it looks like in my personal project:

CLICK ME

also, notice that I spiced my version with small shadow (maybe I've even changed something else, idk)

sleeptightAnsiC3mo ago

You can enable it by flipping #if 0 on this line

this is how it looks like with demo/sdl3_renderer:

CLICK ME

and this is how it looks like in my personal project:

CLICK ME

(my version uses shadow behind glyphs for better readability)

sleeptightAnsiC3mo ago

You can enable it by flipping #if 0 on this line

this is how it looks like with demo/sdl3_renderer:

CLICK ME

and this is how it looks like in my personal project:

CLICK ME

(my version uses shadow behind glyphs for better readability)

sleeptightAnsiC3mo ago

You can enable it by flipping #if 0 on this line

this is how it looks like with demo/sdl3_renderer:

CLICK ME

and this is how it looks like in my personal project:

CLICK ME

(my version uses shadow behind glyphs for better readability)

sleeptightAnsiC3mo ago

I remember there were few bugs in the library that were making Option 2 (and all custom font handling in general) pain to deal with. Maybe I'll enter some Issues later.

sleeptightAnsiC3mo ago

I remember there were few bugs in the library that were making Option 2 (and all custom font handling in general) pain to deal with. Maybe I'll enter some Issues later.

sleeptightAnsiC3mo ago

I remember there were few bugs in the library that were making Option 2 (and all custom font handling in general) pain to deal with. Maybe I'll enter some Issues later.

sleeptightAnsiC3mo ago

I remember there were few bugs in the library that were making Option 2 (and all custom font handling in general) pain to deal with. Maybe I'll enter some Issues later.

RobLoach2mo ago

Oh, that nk_sdl_style_set_debug_font() is nice. Didn't see that one before 👍

RobLoach2mo ago

Oh, that nk_sdl_style_set_debug_font() is nice. Didn't see that one before 👍

sleeptightAnsiC2mo ago

@RobLoach

Out of curiosity, does nk_sdl_style_set_debug_font() work well for your use case? I wrote that function back when I wasn't aware of all Nuklear caveats, and I'm not so proud of how it does few things.

I have a better version in my personal project but I'm hesitant to upstream it, because (a) I've never seen anyone being interested in that function before, and (b) there are some bugs/problems in Nuklear which become obvious when using it.

RobLoach2mo ago

It would be handy for cases where you don't have a font, and don't wanna bake the Nuklear one because your kitchen is on fire.

In my case, it was for a small demo application, and I didn't wanna eat the memory on a font when SDL already provided one.

Feel free to push up any clean up, I'd be happy to take a look.