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