vurtun / nuklear

A single-header ANSI C gui library
13.7k stars 1.11k forks source link

Issue #896: Allow re-baking without clearing the baker. #898

Closed molecularentropy closed 5 years ago

molecularentropy commented 5 years ago

The issue is that the loaded fonts store their offsets. The first bake we get the correct, zero offsets.

(gdb) p {dst_font->glyph_offset,dst_font->glyph_count,glyph_count}
$6 = {0, 0, 0}
(gdb) c
Continuing.
[Switching to thread 10 (Thread 0x7fffdae1a700 (LWP 10511))](running)

Thread 1 "renoise" hit Breakpoint 4, nk_font_bake (baker=0xa6d1ba0, image_memory=0xa6e0f10, width=512, height=2048, glyphs=0xa6d9100, glyphs_count=672, config_list=0x8949760, font_count=3) at thirdparty/nuklear/nuklear.h:11536
11536               for (i = 0; i < tmp->range_count; ++i) {
(gdb) p {dst_font->glyph_offset,dst_font->glyph_count,glyph_count}
$7 = {224, 0, 0}
(gdb) c
Continuing.
[Switching to thread 10 (Thread 0x7fffdae1a700 (LWP 10511))](running)

Thread 1 "renoise" hit Breakpoint 4, nk_font_bake (baker=0xa6d1ba0, image_memory=0xa6e0f10, width=512, height=2048, glyphs=0xa6d9100, glyphs_count=672, config_list=0x8949760, font_count=3) at thirdparty/nuklear/nuklear.h:11536
11536               for (i = 0; i < tmp->range_count; ++i) {
(gdb) p {dst_font->glyph_offset,dst_font->glyph_count,glyph_count}
$8 = {448, 0, 0}

But the second time we try to bake without adding any new fonts, the offsets have the glyph_count at non-zero.

11536               for (i = 0; i < tmp->range_count; ++i) {
(gdb) p {dst_font->glyph_offset,dst_font->glyph_count,glyph_count}
$9 = {0, 224, 0}
(gdb) r

Which causes a segfault here:

glyph = &glyphs[dst_font->glyph_offset + dst_font->glyph_count + (unsigned int)glyph_count];

You're probably wondering why anyone would want to re-bake without adding any new fonts. My application requires me to bring up and tear down the X11 context over the course of execution. I need to be able to push the texture to the video card for rendering every time the context is brought up, but I want to optimise by not re-adding fonts that were already loaded (and decompressed).

My testing was done on version 2.00.4. I plan on upgrading my project to use master HEAD soon. If you need me to test on HEAD, I can do that, but probably in a little while.