示例#1
0
        //public  NuklearAPI(NuklearDevice Device) {
        public static void Init(NuklearDevice Device)
        {
            if (Initialized)
            {
                throw new InvalidOperationException("NuklearAPI.Init is called twice");
            }

            Initialized = true;

            Dev           = Device;
            FrameBuffered = Device as IFrameBuffered;

            // TODO: Free these later
            Ctx         = (nk_context *)ManagedAlloc(sizeof(nk_context));
            Allocator   = (nk_allocator *)ManagedAlloc(sizeof(nk_allocator));
            FontAtlas   = (nk_font_atlas *)ManagedAlloc(sizeof(nk_font_atlas));
            NullTexture = (nk_draw_null_texture *)ManagedAlloc(sizeof(nk_draw_null_texture));
            ConvertCfg  = (nk_convert_config *)ManagedAlloc(sizeof(nk_convert_config));
            Commands    = (nk_buffer *)ManagedAlloc(sizeof(nk_buffer));
            Vertices    = (nk_buffer *)ManagedAlloc(sizeof(nk_buffer));
            Indices     = (nk_buffer *)ManagedAlloc(sizeof(nk_buffer));

            VertexLayout    = (nk_draw_vertex_layout_element *)ManagedAlloc(sizeof(nk_draw_vertex_layout_element) * 4);
            VertexLayout[0] = new nk_draw_vertex_layout_element(nk_draw_vertex_layout_attribute.NK_VERTEX_POSITION, nk_draw_vertex_layout_format.NK_FORMAT_FLOAT,
                                                                Marshal.OffsetOf(typeof(NkVertex), nameof(NkVertex.Position)));
            VertexLayout[1] = new nk_draw_vertex_layout_element(nk_draw_vertex_layout_attribute.NK_VERTEX_TEXCOORD, nk_draw_vertex_layout_format.NK_FORMAT_FLOAT,
                                                                Marshal.OffsetOf(typeof(NkVertex), nameof(NkVertex.UV)));
            VertexLayout[2] = new nk_draw_vertex_layout_element(nk_draw_vertex_layout_attribute.NK_VERTEX_COLOR, nk_draw_vertex_layout_format.NK_FORMAT_R8G8B8A8,
                                                                Marshal.OffsetOf(typeof(NkVertex), nameof(NkVertex.Color)));
            VertexLayout[3] = nk_draw_vertex_layout_element.NK_VERTEX_LAYOUT_END;

            Alloc = (Handle, Old, Size) => ManagedAlloc(Size);
            Free  = (Handle, Old) => ManagedFree(Old);

            //GCHandle.Alloc(Alloc);
            //GCHandle.Alloc(Free);

            Allocator->alloc_nkpluginalloct = Marshal.GetFunctionPointerForDelegate(Alloc);
            Allocator->free_nkpluginfreet   = Marshal.GetFunctionPointerForDelegate(Free);

            Nuklear.nk_init(Ctx, Allocator, null);

            Dev.Init();
            FontStash(Dev.FontStash);

            ConvertCfg->shape_AA             = nk_anti_aliasing.NK_ANTI_ALIASING_ON;
            ConvertCfg->line_AA              = nk_anti_aliasing.NK_ANTI_ALIASING_ON;
            ConvertCfg->vertex_layout        = VertexLayout;
            ConvertCfg->vertex_size          = new IntPtr(sizeof(NkVertex));
            ConvertCfg->vertex_alignment     = new IntPtr(1);
            ConvertCfg->circle_segment_count = 22;
            ConvertCfg->curve_segment_count  = 22;
            ConvertCfg->arc_segment_count    = 22;
            ConvertCfg->global_alpha         = 1.0f;
            ConvertCfg->null_tex             = *NullTexture;

            Nuklear.nk_buffer_init(Commands, Allocator, new IntPtr(4 * 1024));
            Nuklear.nk_buffer_init(Vertices, Allocator, new IntPtr(4 * 1024));
            Nuklear.nk_buffer_init(Indices, Allocator, new IntPtr(4 * 1024));
        }
示例#2
0
        public static void Render()
        {
            nk_buffer Cmds, Verts, Idx;

            Nuklear.nk_buffer_init(&Cmds, Allocator, new IntPtr(4 * 1024));
            Nuklear.nk_buffer_init(&Verts, Allocator, new IntPtr(4 * 1024));
            Nuklear.nk_buffer_init(&Idx, Allocator, new IntPtr(4 * 1024));
            nk_convert_result R = (nk_convert_result)Nuklear.nk_convert(Ctx, &Cmds, &Verts, &Idx, ConvertCfg);

            if (R != nk_convert_result.NK_CONVERT_SUCCESS)
            {
                throw new Exception(R.ToString());
            }

            NkVertex[] NkVerts  = new NkVertex[(int)Verts.needed / sizeof(NkVertex)];
            NkVertex * VertsPtr = (NkVertex *)Verts.memory.ptr;

            for (int i = 0; i < NkVerts.Length; i++)
            {
                NkVerts[i] = VertsPtr[i];
            }

            ushort[] NkIndices  = new ushort[(int)Idx.needed / sizeof(ushort)];
            ushort * IndicesPtr = (ushort *)Idx.memory.ptr;

            for (int i = 0; i < NkIndices.Length; i++)
            {
                NkIndices[i] = IndicesPtr[i];
            }

            uint Offset = 0;

            Nuklear.nk_draw_foreach(Ctx, &Cmds, (Cmd) => {
                if (Cmd->elem_count == 0)
                {
                    return;
                }

                Dev.Render(Cmd->userdata, Cmd->texture.id, Cmd->clip_rect, Offset, Cmd->elem_count, NkVerts, NkIndices);
                Offset += Cmd->elem_count;
            });

            Nuklear.nk_buffer_free(&Cmds);
            Nuklear.nk_buffer_free(&Verts);
            Nuklear.nk_buffer_free(&Idx);
            Nuklear.nk_clear(Ctx);
        }
示例#3
0
        public static nk_context *Init()
        {
            var loader = new Squared.Util.EmbeddedDLLLoader(Assembly.GetExecutingAssembly());

            loader.Load("nuklear.dll");

            _OnNuklearAssert = OnNuklearAssert;
            Nuklear.nk_set_assert_handler(_OnNuklearAssert);

            // TODO: Free these later
            var ctx = (nk_context *)ManagedAlloc(sizeof(nk_context));

            Allocator   = MakeAllocator();
            NullTexture = (nk_draw_null_texture *)ManagedAlloc(sizeof(nk_draw_null_texture));
            Commands    = (nk_buffer *)ManagedAlloc(sizeof(nk_buffer));

            Nuklear.nk_init(ctx, Allocator, null);

            Nuklear.nk_buffer_init(Commands, Allocator, new IntPtr(4 * 1024));

            return(ctx);
        }