示例#1
0
        public NuklearRenderer(NuklearDevice device)
        {
            Device         = device;
            _frameBuffered = Device as IFrameBuffered;

            _nuklearContext = (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));
            _convertConfig  = (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(nk_vertex), nameof(nk_vertex.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(nk_vertex), nameof(nk_vertex.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(nk_vertex), nameof(nk_vertex.color)));
            _vertexLayout[3] = nk_draw_vertex_layout_element.NK_VERTEX_LAYOUT_END;

            _alloc = (_, _, size) => ManagedAlloc(size);
            _free  = (_, old) => ManagedFree(old);

            _allocator->alloc_nkpluginalloct = Marshal.GetFunctionPointerForDelegate(_alloc);
            _allocator->free_nkpluginfreet   = Marshal.GetFunctionPointerForDelegate(_free);

            Nuklear.nk_init(_nuklearContext, _allocator, null);

            Device.Init();
            FontStash(Device.FontStash);

            _convertConfig->shape_AA             = nk_anti_aliasing.NK_ANTI_ALIASING_ON;
            _convertConfig->line_AA              = nk_anti_aliasing.NK_ANTI_ALIASING_ON;
            _convertConfig->vertex_layout        = _vertexLayout;
            _convertConfig->vertex_size          = new IntPtr(sizeof(nk_vertex));
            _convertConfig->vertex_alignment     = new IntPtr(1);
            _convertConfig->circle_segment_count = 22;
            _convertConfig->curve_segment_count  = 22;
            _convertConfig->arc_segment_count    = 22;
            _convertConfig->global_alpha         = 1.0f;
            _convertConfig->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));

            NuklearUI.InitializeContext(_nuklearContext);
        }