示例#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));
        }