官术网_书友最值得收藏!

"Hello World" drawing text on an image

This recipe dynamically creates and displays a texture with text created using the imaging APIs.

Getting ready

This recipe builds on the code from the last recipe. Add the following using statement to the beginning of the program code:

using Sce.PlayStation.Core.Imaging

For the complete source code for this example, see Ch1_Example3.

How to do it...

  1. In the Initialize() function, instead of loading a texture from the file, we will create one using the following code:
    Image img = new Image(ImageMode.Rgba,new ImageSize(500,300),new ImageColor(0,0,0,0));
    img.DrawText("Hello World", 
       new ImageColor(255,255,255,255),
       new Font(FontAlias.System,96,FontStyle.Italic),
       new ImagePosition(0,150));
    _texture = new Texture2D(500,300,false,PixelFormat.Rgba);
    _texture.SetPixels(0,img.ToBuffer());
  2. Next, update the Render() method in the following code in which the bolded portions represent the changes:
    public static void Render (){
     _graphics.SetClearColor (0.0f, 0.0f, 0.0f, 0.0f);
     _graphics.Clear ();
     
     var worldViewProjection = _projectionMatrix * _viewMatrix * _localMatrix;
     _textureShaderProgram.SetUniformValue(0,ref worldViewProjection);
     
                    
     _graphics.SetShaderProgram(_textureShaderProgram);
     _graphics.SetVertexBuffer(0,_vertexBuffer);
     _graphics.SetTexture(0,_texture);
     
     _graphics.Enable(EnableMode.Blend);
     _graphics.SetBlendFunc(BlendFuncMode.Add, BlendFuncFactor.SrcAlpha, BlendFuncFactor.OneMinusSrcAlpha);
     _graphics.DrawArrays(DrawMode.TriangleFan,0,4);
     
     _graphics.SwapBuffers ();
    }

How it works...

Instead of loading a texture from an image file, we create an image dynamically. In the Image constructor, we pass the type of image we want created, the dimensions and the background color to fill it with.

Next, we draw on our newly created image using the DrawText() function, which takes as parameters the text to draw, the color to draw it in, the font to use (there is only one option, System) and the position to draw the text at. We then create a Texture2D object to hold our image. We pass its constructor the image dimensions, whether we want to generate a mipmap or not, as well as the pixel format to use. Finally, we assign the pixel data to our _texture object by calling SetPixel() function and passing in a byte array generated by calling ToBuffer() function on our image.

We had to make the change to the Render() method to support blending using the alpha channel, or background would not be properly visible through the transparent portions of the text. Run the code again without EnableMode.Blend enabled and your text will be illegible.

Now if we run our application, we will see the following screenshot:

How it works...

There's more...

You can also load a font by name instead of using the built in system font. If you need precise control over your text positioning or size, be sure to check out the FontMetrics and CharMetrics classes in the documentation.

主站蜘蛛池模板: 灌云县| 梨树县| 承德市| 大田县| 柞水县| 安宁市| 商洛市| 蓝山县| 贵德县| 长治县| 南召县| 贵定县| 渭南市| 临夏县| 车险| 东丽区| 曲松县| 临洮县| 许昌市| 西乌珠穆沁旗| 岳阳市| 冕宁县| 罗源县| 都兰县| 太康县| 商河县| 大兴区| 丰都县| 磴口县| 延津县| 呈贡县| 耿马| 无锡市| 建宁县| 巴彦县| 高邑县| 浠水县| 沛县| 龙江县| 东台市| 讷河市|