Exemplo n.º 1
0
        public void Draw()
        {
            BeginDraw();

            //  ushort* offset = null;

            _cmds.reset();
            _vertices.reset();
            _indices.reset();
            Convert(_cmds, _vertices, _indices, _convertConfig);

            var vertex_count = (uint)((ulong)_vertices.Count / _convertConfig.vertex_size);

            /* iterate over and execute each draw command */
            uint offset = 0;

            SetBuffers(_vertices.Data, _indices.Data, _indices.Count, (int)vertex_count);
            for (var i = 0; i < _cmds.Count; ++i)
            {
                var cmd = _cmds[i];
                if (cmd.elem_count == 0)
                {
                    continue;
                }

                Draw((int)cmd.clip_rect.x, (int)cmd.clip_rect.y, (int)cmd.clip_rect.w, (int)cmd.clip_rect.h,
                     cmd.texture.id, (int)offset, (int)(cmd.elem_count / 3));
                offset += cmd.elem_count;
            }
            Nuklear.nk_clear(_ctx);

            EndDraw();
        }
Exemplo n.º 2
0
        private void Render(bool HadInput)
        {
            if (HadInput)
            {
                var Dirty = true;

                if (_frameBuffered != null)
                {
                    Dirty = false;

                    var MemoryBuffer = Nuklear.nk_buffer_memory(&_nuklearContext->memory);
                    if ((int)_nuklearContext->memory.allocated == 0)
                    {
                        Dirty = true;
                    }

                    if (!Dirty)
                    {
                        if (_lastMemory == null || _lastMemory.Length < (int)_nuklearContext->memory.allocated)
                        {
                            _lastMemory = new byte[(int)_nuklearContext->memory.allocated];
                            Dirty       = true;
                        }
                    }

                    if (!Dirty)
                    {
                        fixed(byte *LastMemoryPtr = _lastMemory)
                        if (MemoryManagement.MemCmp(new IntPtr(LastMemoryPtr), MemoryBuffer,
                                                    _nuklearContext->memory.allocated) != 0)
                        {
                            Dirty = true;
                            Marshal.Copy(MemoryBuffer, _lastMemory, 0, (int)_nuklearContext->memory.allocated);
                        }
                    }
                }

                if (Dirty)
                {
                    var convertResult = (nk_convert_result)Nuklear.nk_convert(
                        _nuklearContext,
                        _commands,
                        _vertices,
                        _indices,
                        _convertConfig
                        );

                    if (convertResult != nk_convert_result.Success)
                    {
                        throw new Exception(convertResult.ToString());
                    }

                    var nkVertices  = new nk_vertex[(int)_vertices->needed / sizeof(nk_vertex)];
                    var verticesPtr = (nk_vertex *)_vertices->memory.ptr;

                    if (verticesPtr == null)
                    {
                        throw new Exception("Vertex buffer pointer invalid.");
                    }

                    for (var i = 0; i < nkVertices.Length; i++)
                    {
                        nkVertices[i] = verticesPtr[i];
                    }

                    var nkIndices  = new ushort[(int)_indices->needed / sizeof(ushort)];
                    var indicesPtr = (ushort *)_indices->memory.ptr;

                    if (indicesPtr == null)
                    {
                        throw new Exception("Index buffer pointer invalid.");
                    }

                    for (var i = 0; i < nkIndices.Length; i++)
                    {
                        nkIndices[i] = indicesPtr[i];
                    }

                    Device.SetBuffer(nkVertices, nkIndices);
                    _frameBuffered?.BeginBuffering();

                    uint Offset = 0;
                    Device.BeginRender();

                    Nuklear.nk_draw_foreach(_nuklearContext, _commands, (command) =>
                    {
                        if (command->elem_count == 0)
                        {
                            return;
                        }

                        Device.Render(
                            command->userdata,
                            command->texture.id,
                            command->clip_rect,
                            Offset,
                            command->elem_count
                            );

                        Offset += command->elem_count;
                    });

                    Device.EndRender();
                    _frameBuffered?.EndBuffering();
                }

                var list = &_nuklearContext->draw_list;

                if (list->buffer != null)
                {
                    Nuklear.nk_buffer_clear(list->buffer);
                }

                if (list->vertices != null)
                {
                    Nuklear.nk_buffer_clear(list->vertices);
                }

                if (list->elements != null)
                {
                    Nuklear.nk_buffer_clear(list->elements);
                }

                Nuklear.nk_clear(_nuklearContext);
            }

            _frameBuffered?.RenderFinal();
        }