Пример #1
0
        private static void UpdateBuffer(UIItem item)
        {
            if (item.Visible)
            {
                Queue <UIRenderData>[] data = { new Queue <UIRenderData>(), new Queue <UIRenderData>(), new Queue <UIRenderData>() };
                foreach (UIRenderData renderData in item.ConstructVertexData())
                {
                    data[renderData.Depth].Enqueue(renderData);
                }

                for (int d = 0; d < 3; d++)
                {
                    Queue <UIRenderData> queue = data[d];
                    int verticies = queue.Count * 8;
                    int position, index = FindCurrentIndex(item, d, out position);
                    int delta = verticies - ((index == -1) ? 0 : indexUsage[d][index].Item2);

                    if (delta != 0)
                    {
                        // create space
                        int oldend = position + ((index == -1) ? 0 : indexUsage[d][index].Item2), newend = position + verticies, length = Math.Max(oldend, newend);
                        Array.Copy(vertexBuffer.Cache, oldend, vertexBuffer.Cache, newend, vertexBuffer.Cache.Length - length);
                        Array.Copy(textureBuffer.Cache, oldend, textureBuffer.Cache, newend, textureBuffer.Cache.Length - length);
                        Array.Copy(colorBuffer.Cache, oldend * 2, colorBuffer.Cache, newend * 2, colorBuffer.Cache.Length - length * 2);
                    }

                    if (index != -1)
                    {
                        indexUsage[d][index] = new Tuple <UIItem, int>(item, verticies);
                    }
                    else
                    {
                        indexUsage[d].Add(new Tuple <UIItem, int>(item, verticies));
                    }

                    vertexCount += delta;
                    renderCount += delta * 6 / 8;
                    while (queue.Count > 0)
                    {
                        UIRenderData renderData = queue.Dequeue();
                        Array.Copy(renderData.Verticies, 0, vertexBuffer.Cache, position, 8);
                        Array.Copy(Texture[renderData.Texture], 0, textureBuffer.Cache, position, 8);
                        Array.Copy(renderData.Color.ToArray4(), 0, colorBuffer.Cache, position * 2, 16);
                        position += 8;
                    }
                    for (int di = d; di < 3; di++)
                    {
                        startPositions[di] += delta;
                    }
                }
            }
            else
            {
                int index;
                int position;
                for (int d = 0; d < 3; d++)
                {
                    index = FindCurrentIndex(item, d, out position);
                    if (index > -1)
                    {
                        int verticiesToClear = indexUsage[d][index].Item2;
                        indexUsage[d].RemoveAt(index);
                        vertexCount -= verticiesToClear;
                        renderCount -= verticiesToClear * 6 / 8;
                        for (int di = d; di < 3; di++)
                        {
                            startPositions[di] -= verticiesToClear;
                        }
                        int end = position + verticiesToClear;
                        Array.Copy(vertexBuffer.Cache, end, vertexBuffer.Cache, position, vertexBuffer.Cache.Length - end);
                        Array.Copy(textureBuffer.Cache, end, textureBuffer.Cache, position, textureBuffer.Cache.Length - end);
                        Array.Copy(colorBuffer.Cache, end * 2, colorBuffer.Cache, position * 2, colorBuffer.Cache.Length - end * 2);
                    }
                }
            }
        }