/// <summary> /// Debug text draw function, using a small embedded font suitable for on screen debug prints. /// Since DrawTextDebug uses very small font data, it only work with ascii text (characters /// from ' ' to '~', 32 to 126) Any character outside this range will be displayed as '?'. /// For instance "こんにちは" will be displayed as "?????". /// </summary> /// <param name="str">The text to draw.</param> /// <param name="bottom_left_start_pos">The bottom left of the text rectangle, in world space/units.</param> /// <param name="char_height">The character height in world space/units.</param> /// <param name="draw">If false, don't draw anything, just return the Bounds2 used by the text.</param> /// <param name="shader">If no shader is specified, DefaultFontShader is used.</param> /// <returns>The rectangle area covered by rendered text (call with draw=false when you want to know the covered area before actually drawing it).</returns> public Bounds2 DrawTextDebug( string str, Vector2 bottom_left_start_pos, float char_height, bool draw = true, ISpriteShader shader = null ) { if (null == shader) shader = (ISpriteShader)m_default_font_shader; float scale = ( char_height / EmbeddedDebugFontData.CharSizei.Y ); Vector2 spacing_in_pixels = new Vector2( -1.0f, 1.0f ); Vector2 spacing = spacing_in_pixels * scale; //1 character sprite bounding box in world space, // could use Bounds2 instead really. //R and S stay unchaged, T gets incrementd as we draw chars TRS char_box; char_box.R = Math._10; char_box.S = EmbeddedDebugFontData.CharSizef * scale; char_box.T = bottom_left_start_pos; Vector2 max = bottom_left_start_pos; float left = char_box.T.X; if ( draw ) { DefaultFontShader.SetUVTransform(ref Math.UV_TransformFlipV); BeginSprites(m_embedded_font_texture_info, DefaultFontShader, str.Length); } for ( int i=0; i < str.Length; ++i ) { if ( str[i] == '\n') { char_box.T -= new Vector2( 0.0f, char_box.S.Y + spacing.Y ); char_box.T.X = left; continue; } if (str[i] == '\r') continue; if ( draw ) { int char_index = (int)str[i] - 32; if ( char_index < 0 || char_index >= EmbeddedDebugFontData.NumChars ) char_index = (int)'?' - 32; AddSprite( ref char_box, new Vector2i( char_index, 0 ) ); } char_box.T += new Vector2( char_box.S.X + spacing.X, 0.0f ); max.X = FMath.Max( char_box.T.X, max.X ); max.Y = FMath.Min( char_box.T.Y, max.Y ); } if ( draw ) EndSprites(); return Bounds2.SafeBounds( bottom_left_start_pos + new Vector2( 0.0f, char_box.S.Y ), max ); }
/// <summary>This text draw function uses a FontMap object.</summary> /// <param name="str">The text to draw.</param> /// <param name="bottom_left_start_pos">The bottom left of the text rectangle, in world space/units.</param> /// <param name="char_height">The character height in world space/units.</param> /// <param name="draw">If false, don't draw anything, just return the Bounds2 used by the text.</param> /// <param name="fontmap">the fontmap object (that holds the texture).</param> /// <param name="shader">The shader defaults to SpriteRenderer.DefaultFontShader.</param> public Bounds2 DrawTextWithFontMap( string str, Vector2 bottom_left_start_pos, float char_height, bool draw, FontMap fontmap, ISpriteShader shader ) { float scale = ( char_height / fontmap.CharPixelHeight ); Vector2 spacing_in_pixels = new Vector2( 1.0f, 1.0f ); Vector2 spacing = spacing_in_pixels * scale; Vector2 turtle = bottom_left_start_pos; Vector2 max = bottom_left_start_pos; float left = bottom_left_start_pos.X; if ( draw ) { shader.SetUVTransform( ref Math.UV_TransformFlipV ); BeginSprites( new TextureInfo( fontmap.Texture ), shader, str.Length ); } for ( int i=0; i < str.Length; ++i ) { if ( str[i] == '\n' ) { turtle -= new Vector2( 0.0f, char_height + spacing.Y ); turtle.X = left; continue; } FontMap.CharData cdata; if ( !fontmap.TryGetCharData( str[i], out cdata ) ) continue; Vector2 scaled_char_size = cdata.PixelSize * scale; if ( draw ) AddSprite( turtle, new Vector2(scaled_char_size.X,0.0f), cdata.UV ); turtle += new Vector2( scaled_char_size.X + spacing.X, 0.0f ); max.X = FMath.Max( turtle.X, max.X ); max.Y = FMath.Min( turtle.Y, max.Y ); } if ( draw ) EndSprites(); return Bounds2.SafeBounds( bottom_left_start_pos + new Vector2( 0.0f, char_height ), max ); }
/// <summary>Start batch rendering of sprites.</summary> /// <param name="texture_info">The texture object.</param> /// <param name="shader">The shader object.</param> /// <param name="num_sprites">The maximum number of sprite you intend to draw.</param> public void BeginSprites( TextureInfo texture_info, ISpriteShader shader, int num_sprites ) { ////Common.Profiler.Push("SpriteRenderer.BeginSprites"); Matrix4 mvp = GL.GetMVP(); shader.SetMVP( ref mvp ); GL.Context.SetShaderProgram( shader.GetShaderProgram() ); Common.Assert( !texture_info.Disposed, "This TextureInfo object has been disposed" ); GL.Context.SetTexture( 0, texture_info.Texture ); m_current_texture_info = texture_info; m_imm_quads.ImmBeginQuads( (uint)num_sprites ); ////Common.Profiler.Pop(); }