Hello,
I'm attempting to create bindings for the C library nuklear - https://github.com/vurtun/nuklear
Here's the github repo with my bindings - https://github.com/zacharycarter/nuklear-nim
Here's a description of the problem - https://github.com/vurtun/nuklear/issues/348
(copied & pasted here for convenience sake)
Hello,
I've created bindings to nuklear for the Nim programming language. They can be found here - https://github.com/zacharycarter/nuklear-nim
The bindings are not complete and there are still a few constructs in nuklear.h I have yet to figure out how to port to nim. Regardless, they're mostly complete and I'm trying to work up a small example based on the Opengl 3 GLFW3 demo.
I'm running into a problem though. When I call -
nk_begin
(https://github.com/zacharycarter/nuklear-nim/blob/master/testNuklear.nim#L85) - in my program, the following assertion fails :
Assertion failed: (!ctx->current && "if this triggers you missed a nk_end call"), function nk_begin_titled, file ./nuklear.h, line 17271.
Here's some output from lldb
* thread #1: tid = 0x14966, 0x0000000100001116 testNuklear`main_mgJNO5hlIp1wZRNie7UREA + 1094 at testNuklear.nim:85, queue = 'com.apple.main-thread', stop
reason = breakpoint 3.1
frame #0: 0x0000000100001116 testNuklear`main_mgJNO5hlIp1wZRNie7UREA + 1094 at testNuklear.nim:85
82 glfw.PollEvents()
83 #nk_glfw3_new_frame()
84 ## GUI
-> 85 if nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250), nk_flags NK_WINDOW_BORDER.cint or
86 NK_WINDOW_MOVABLE.cint or NK_WINDOW_SCALABLE.cint or NK_WINDOW_MINIMIZABLE.cint or
87 NK_WINDOW_TITLE.cint) == 1:
88 discard
(lldb) frame variable ctx0->current
(nkwindow_F8W9asuU9aVqgjmJLA5nrAQw *) ctx0->current = 0x0000000000000000
(lldb)
I'm not sure why when the C code is invoked - this is no longer the case...
I've added some printf statements to nuklear.h to ensure that the method isn't being called twice and failing on a subsequent invocation - it's failing on the first call.
I've been tearing my hair out over this - I can't figure out why the context isn't null. Is there anything that needs to be done initialization wise, besides calling nk_init and setting a default font? I'm reaching for help here as I'm guessing most folks here don't know nim, but any suggestions would be greatly appreciated!
I'm not sure why this assert fails, anyone have any ideas? I can help folks with getting the project checked out / built if they'd like to run it in an effort to help me.
Thanks!
I've updated my bindings with another simpler example -
https://github.com/zacharycarter/nuklear-nim/blob/master/testNuklear2.nim
I've removed all the GLFW code, and now the code is failing here -
NK_INTERN struct nk_window*
nk_find_window(struct nk_context *ctx, nk_hash hash, const char *name)
{
struct nk_window *iter;
iter = ctx->begin;
while (iter) {
NK_ASSERT(iter != iter->next);
if (iter->name == hash) { <--- HERE line 17159 of nuklear.h
int max_len = nk_strlen(iter->name_string);
if (!nk_stricmpn(iter->name_string, name, max_len))
return iter;
}
iter = iter->next;
}
return 0;
}
So it's failing on line 17159 of nuklear.h because iter which is initialized to ctx->begin is nil, however the while loop fails to skip since in reality ctx->begin is nil and a check in the nim test of
echo repr ctx.begin
confirms that.
Still no idea what's going on here. I was able to get passed my initial problem simply by removing the assert and changing the line below to not check if ctx->current is null.
Something is still very broken here since all these null checks in C are failing.
I've gotten passed that issue (at least on OSX) and am now facing a new issue. Please see this gist for a run down of what's going on -
https://gist.github.com/zacharycarter/de596b1968b63f2e0967d41b31b73804
void nk_convert(const struct nk_convert_config *config)
{
printf("%d\n", config->vertex_layout[0].attribute);
}
is printing
1536
The relevant nim code -
{.compile: "test.c".}
type nk_size = culong
type
nk_draw_vertex_layout_attribute* = enum
NK_VERTEX_POSITION, NK_VERTEX_COLOR, NK_VERTEX_TEXCOORD,
NK_VERTEX_ATTRIBUTE_COUNT
type
nk_draw_vertex_layout_format* = enum
NK_FORMAT_SCHAR, NK_FORMAT_SSHORT, NK_FORMAT_SINT, NK_FORMAT_UCHAR,
NK_FORMAT_USHORT, NK_FORMAT_UINT, NK_FORMAT_FLOAT, NK_FORMAT_DOUBLE,
NK_FORMAT_COLOR_BEGIN, NK_FORMAT_R16G15B16, NK_FORMAT_R32G32B32,
NK_FORMAT_R8G8B8A8, NK_FORMAT_R16G15B16A16, NK_FORMAT_R32G32B32A32,
NK_FORMAT_R32G32B32A32_FLOAT, NK_FORMAT_R32G32B32A32_DOUBLE, NK_FORMAT_RGB32,
NK_FORMAT_RGBA32, NK_FORMAT_COUNT
type
nk_draw_vertex_layout_element* = object
attribute*: nk_draw_vertex_layout_attribute
format*: nk_draw_vertex_layout_format
offset*: nk_size
type
nk_convert_config* = object
vertex_layout*: ptr nk_draw_vertex_layout_element
proc nk_convert*(a6: ptr nk_convert_config) {.cdecl, importc: "nk_convert".}
type nk_byte* = cuchar
template offsetof(typ, field): expr = (var dummy: typ; cast[uint](addr(dummy.field)) - cast[uint](addr(dummy)))
type
nk_glfw_vertex = object
position: array[2, float]
uv: array[2, float]
col: array[4, nk_byte]
var config = nk_convert_config()
var vertex_layout {.global.} = @[
nk_draw_vertex_layout_element(
attribute: NK_VERTEX_POSITION,
format: NK_FORMAT_FLOAT,
offset: offsetof(nk_glfw_vertex, position)
),
nk_draw_vertex_layout_element(
attribute: NK_VERTEX_TEXCOORD,
format: NK_FORMAT_FLOAT,
offset: offsetof(nk_glfw_vertex, uv)
),
nk_draw_vertex_layout_element(
attribute: NK_VERTEX_COLOR,
format: NK_FORMAT_FLOAT,
offset: offsetof(nk_glfw_vertex, col)
),
nk_draw_vertex_layout_element(
attribute: NK_VERTEX_ATTRIBUTE_COUNT,
format: NK_FORMAT_COUNT,
offset: 0
)
]
config.vertex_layout = addr vertex_layout[0]
nk_convert(addr config)
Thanks for the help Araq.
I think I've now got the bindings working for the most part. There may still be some missing functionality - like attaching user data to the context, but that stuff can be added relatively easily and I will do so.
I'm still playing around with the drawing code / backend stuff, but here's a screenshot from an example using OpenGL and GLFW3. Once I have a few demos ported over completely I will let everyone know.
Also I plan on adding a backend for - https://github.com/Halsys/nim-bgfx - and I will publish that once it's available.
Here's the github repo for nuklear-nim : https://github.com/zacharycarter/nuklear-nim
Expect it to be in pretty heavy flux over the next month or so as I get everything stable. Also thanks to araq, varriount, krux02 and everyone else in #nim for helping me out with these bindings over the past couple of months.