How do I correctly add a font in the GUI library Nuklear?

83 Views Asked by At

I am attempting to implement the example from the Nuklear Documentation.

// init gui state
enum {EASY, HARD};
static int op = EASY;
static float value = 0.6f;
static int i =  20;
struct nk_context ctx;
nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);
if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
    NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
    // fixed widget pixel width
    nk_layout_row_static(&ctx, 30, 80, 1);
    if (nk_button_label(&ctx, "button")) {
        // event handling
    }
    // fixed widget window ratio width
    nk_layout_row_dynamic(&ctx, 30, 2);
    if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
    if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;
    // custom widget pixel width
    nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
    {
        nk_layout_row_push(&ctx, 50);
        nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
        nk_layout_row_push(&ctx, 110);
        nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
    }
    nk_layout_row_end(&ctx);
}
nk_end(&ctx);

There's 2 things that need to be defined for this example, MAX_MEMORY and font.

I swapped nk_init_fixed(...) to be nk_init_default(...) to not worry about memory.

For the font I used:

    struct nk_font_atlas atlas;
    nk_font_atlas_init_default(&atlas);
    nk_font_atlas_begin(&atlas);

    const struct nk_font_config config = nk_font_config(12);
    struct nk_font *clean = nk_font_atlas_add_from_file(&atlas, "../Nuklear/extra_font/ProggyClean.ttf", 12, &config);

    atlas.default_font = clean;

then

nk_init_default(&ctx, &atlas.default_font->handle);

The entire file is:

#define NK_IMPLEMENTATION
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_STANDARD_IO
#include "Nuklear/nuklear.h"

int main() {


    struct nk_font_atlas atlas;
    nk_font_atlas_init_default(&atlas);
    nk_font_atlas_begin(&atlas);

    const struct nk_font_config config = nk_font_config(12);
    struct nk_font *clean = nk_font_atlas_add_from_file(&atlas, "../Nuklear/extra_font/ProggyClean.ttf", 12, &config);

    atlas.default_font = clean;

    // init gui state
    enum {EASY, HARD};
    static int op = EASY;
    static float value = 0.6f;
    struct nk_context ctx;

    nk_init_default(&ctx, &atlas.default_font->handle);

    if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
                 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
        // fixed widget pixel width
        nk_layout_row_static(&ctx, 30, 80, 1);
        if (nk_button_label(&ctx, "button")) {
            // event handling
        }
        // fixed widget window ratio width
        nk_layout_row_dynamic(&ctx, 30, 2);
        if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
        if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;
        // custom widget pixel width
        nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
        {
            nk_layout_row_push(&ctx, 50);
            nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
            nk_layout_row_push(&ctx, 110);
            nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
        }
        nk_layout_row_end(&ctx);
    }
    nk_end(&ctx);

    return EXIT_SUCCESS;
}

However I am getting this error when nk_begin(...) is called:

Assertion failed: (ctx->style.font && ctx->style.font->width && "if this triggers you forgot to add a font"), function nk_begin_titled

I also tried using nk_font_atlas_add_default instead of nk_font_atlas_add_from_file.

How do I correctly add a font in Nuklear?

0

There are 0 best solutions below