Exemple #1
0
    public SpriteRenderQueue(UIRenderQueueConfiguration config, TextManager textManager, FontManager fontManager)
    {
        var maxVertices = config.MaxSprites * 4 + config.MaxNinePatchSprites * 4 * 9 + config.MaxTextBlocks * 4 * 100; // 100 characters per block (TODO: change this at some point)
        var maxIndices  = maxVertices * 6;

        _spriteBatch     = new SpriteBatch(config.MaxSprites);
        _nineSliceSprite = new NineSliceSpriteBatch(config.MaxNinePatchSprites);
        _textBatch       = new TextBatch(config.MaxTextBlocks, textManager, fontManager);

        _vertices = MemoryUtils.AllocateBlock <SpriteVertex>(maxVertices);
        _elements = new SpriteElement[100];        // TODO: Hardcoded for now, not sure what an optimal number would be. UIElements will be created for each texture change
        _sortable = new SortableRenderable[1_000]; // TODO: hardcoded for now. This is the amount of "items" added to the queue. 1 for each text, sprite or nine patch.
        /// <summary>
        /// Performs batch drawing of the queued 3D texts and lines.
        /// </summary>
        public static void Flush(bool clear)
        {
            if (queued3DTexts == null)
            {
                return;
            }

            if (textBatch == null)
            {
                textBatch = new TextBatch(State.Device);
            }

            if (queued3DTexts.Count > 0)
            {
                textBatch.ViewProjection = State.ViewMatrix * State.ProjectionMatrix;
                int        numIndices = 0;
                Text3DInfo info;
                for (int i = 0; i < queued3DTexts.Count; i++)
                {
                    info = queued3DTexts[i];

                    if (numIndices == 0)
                    {
                        textBatch.Begin();
                    }

                    textBatch.DrawText(info.text3d, info.transform, info.color);

                    numIndices += info.text3d.Indices.Length;

                    if (numIndices > MAX_NUM_INDICES)
                    {
                        textBatch.End();
                        numIndices = 0;
                    }
                }

                if (numIndices != 0)
                {
                    textBatch.End();
                }

                if (clear)
                {
                    queued3DTexts.Clear();
                }
            }
        }