Getting Started with DirectX game Programming-Part II (Game Programming Toolbox)--Print text

Source: Internet
Author: User
Tags drawtext int size

This series of articles by Net_assassin finishing, reproduced please indicate the source.

http://blog.csdn.net/net_assassin/article/category/1100363


Author: Net_assassin e-mail: net_assassin@hotmail.com look forward to interacting with like-minded friends

In this chapter we will learn how to use Id3dxfont to create fonts and print text on the screen. This class allows us to print text using any TrueType fonts that are installed on Windows systems. However, it is recommended to use only standard fonts. creating Fonts in the past I like to use bitmap-based fonts, whose font sets are stored in a bitmap file in ASCII order. After the bitmap has been loaded, this font borrows every character in the string using the sprite's attributes. The result of this is that we get a font that is exactly the same as we expect in the real world, because we're controlling the in-situ map.
DirectX provides a font class that abstracts the entire process so that we can spend more time on the game code without paying too much attention to its internal logic, such as bitmap images loaded with fonts. The Id3dxfont interface is used to create fonts, and its pointer version is predefined:
Lpd3dxfont font;
Create a Font object

D3dxcreatefontindirect (D3ddev,&desc,&font);

DESC is a D3DXFONT_DESC structure, defined as follows:
D3dxfont_desc DESC = {
        size,                   //height
        0,                      //width
        0,                      //weight
        0,                      //miplevels
        False,                  //italic
        default_charset,        //charset
        out_tt_precis,          //output precision
        Clip_ Default_precis,    //quality
        default_pitch,          //pitch and Family
        ""                      //font name
    };

Reusable MakeFont FunctionsPut all of this code in a reusable function and add it to the game library. This function requires the font tomorrow morning and font point dimensions as parameters, which returns a pointer to the Lpd3dxfont object.
Lpd3dxfont MakeFont (string name, int size)
{
    Lpd3dxfont font = NULL;

    D3dxfont_desc DESC = {
        size,                   //height
        0,                      //width
        0,                      //weight
        0,                      //miplevels
        false,                  //italic
        default_charset,        //charset
        out_tt_precis,          //output Precision
        Clip_default_precis,    //quality
        default_pitch,          //pitch and Family
        ""                      //font Name
    };

    strcpy (Desc. FaceName, Name.c_str ());

    D3dxcreatefontindirect (D3ddev, &desc, &font);

    return font;
}

print text using Id3dxfont
void Fontprint (lpd3dxfont font, int x, int y, string text, D3dcolor color)
{
    //figure out the text boundary
    RE CT rect = {x, y, 0, 0};
    Font->drawtext (NULL, Text.c_str (), Text.length (), &rect, dt_calcrect, color); 

    Print the text
    font->drawtext (spriteobj, Text.c_str (), Text.length (), &rect, dt_left, color); 
}

Test Font Output



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.