Gamua / Starling-Framework

The Cross Platform Game Engine
http://www.starling-framework.org
Other
2.85k stars 819 forks source link

Bug with BitmapFont #885

Closed subdan closed 8 years ago

subdan commented 8 years ago

I want to modify the BitmapFont class to dynamically generate some characters. I want to display a fraction instead of special symbols. For example: I want to display this text: Some Ӑ text as: screenshot_2

Inside parseFontXml method I found a loop:

 for each (var charElement:XML in fontXml.chars.char)
            {
                var id:int = parseInt(charElement.@id);
                var xOffset:Number  = parseFloat(charElement.@xoffset)  / scale;
                var yOffset:Number  = parseFloat(charElement.@yoffset)  / scale;
                var xAdvance:Number = parseFloat(charElement.@xadvance) / scale;

                var region:Rectangle = new Rectangle();
                region.x = parseFloat(charElement.@x) / scale + frameX;
                region.y = parseFloat(charElement.@y) / scale + frameY;
                region.width  = parseFloat(charElement.@width)  / scale;
                region.height = parseFloat(charElement.@height) / scale;

                var texture:Texture = Texture.fromTexture(mTexture, region);
                var bitmapChar:BitmapChar = new BitmapChar(id, texture, xOffset, yOffset, xAdvance); 
                addChar(id, bitmapChar);
            }

I added a condition:

                if (id == 1232) // I want to replace the symbol Ӑ with dynamic texture
                {
                    texture = Texture.empty(100,100); // I create a new empty texture (for testing)
                }

I see the next picture: screenshot_1

It's a TextField: var tf:TextField = new TextField(400, 100, "Some Ӑ text"); I replace Ӑ symbol with empty Texture.

Why I see a font texture atlas instead of empty space? screenshot_3

In the future I will replace Texture.fromEmpty to RenderTexture. Inside RenderTexture I will combine 3 Image: numerator, denominator and separator.

This bug appears in all versions of Starling.

PrimaryFeather commented 8 years ago

The reason for this issue is that the text is drawn into a MeshBatch in order to provide the maximum performance. And all quads that are added to a MeshBatch need to reference the same texture. The empty texture you're adding is just ignored; the batch can't work with that. Instead, it references the font texture, and that's what you're seeing.

My recommendation for what you want to achieve: use BitmapFont.createSprite instead. You'll get a sprite where each glyph is one image. Use placeholders for the positions where you want to show the fractions; then replace those placeholders with the manually created fraction symbols.

However what's happening inside fillMeshBatch is intentional — so this is not a bug. I'll close this issue.

subdan commented 8 years ago

Thank you, Daniel.