示例#1
0
        private TextSprite CreateSprite(object key, string renderedStringBuilder,
                                        int frameNumber, bool cached, GraphicsDevice device)
        {
            // First try to find an existing sprite to reuse
            for (int i = 0; i < sprites.Count; i++)
            {
                // If it was last used before the immediately previous frame, assume it is free to use:
                if (sprites[i].cached == cached && sprites[i].foundOnFrame < frameNumber - 1)
                {
                    TextSpriteInstance instance = sprites[i];
                    instance.key = key;
                    instance.renderedStringBuilder = renderedStringBuilder;
                    instance.foundOnFrame          = frameNumber;
                    sprites[i] = instance;
                    return(instance.value);
                }
            }

            // If no sprite to reuse was found...
            TextSprite sprite = new TextSprite(this, cached);

            sprites.Add(new TextSpriteInstance()
            {
                key   = key,
                value = sprite,
                renderedStringBuilder = renderedStringBuilder,
                foundOnFrame          = frameNumber,
                cached = cached
            });
            device.AddSprite(sprite);
            return(sprite);
        }
示例#2
0
        private TextSprite UpdateStringBuilderInstance(StringBuilder text, int frameNumber, int i)
        {
            TextSpriteInstance instance = sprites[i];

            // If the value of the StringBuilder has changed, re-render it:
            if (HasStringBuilderChanged(text, instance.renderedStringBuilder))
            {
                instance.renderedStringBuilder = text.ToString();                 // Allocates memory
                instance.value.SetText(instance.renderedStringBuilder);           // Causes rendering
            }

            // Always update the frame number, then insert it back into the list of sprites
            instance.foundOnFrame = frameNumber;
            sprites[i]            = instance;

            return(sprites[i].value);
        }
示例#3
0
        private TextSprite GetSprite(string text, bool cached, GraphicsDevice device)
        {
            int frameNumber = device.CurrentSpriteFrameNumber;

            // Strings are immutable, if an instance is matched: use it directly
            for (int i = 0; i < sprites.Count; i++)
            {
                if (ReferenceEquals(sprites[i].key, text) &&
                    sprites[i].cached == cached && sprites[i].foundOnFrame != frameNumber)
                {
                    TextSpriteInstance instance = sprites[i];
                    instance.foundOnFrame = frameNumber;
                    sprites[i]            = instance;
                    return(instance.value);
                }
            }

            // No match found, create one:
            TextSprite sprite = CreateSprite(text, null, frameNumber, cached, device);

            sprite.SetText(text);
            return(sprite);
        }