Example #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));
        }
Example #2
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);
        }