Convert text to idirect3dtexture9

Source: Internet
Author: User

If there is reprint please indicate the source: http://blog.csdn.net/tianhai110/

In 3D Game Development, text is usually converted into textures for 3D display. This article describes how to generate a d3d texture through GDI.

Since there are more than 5000 Chinese characters, it is obviously unrealistic to create corresponding images for each Chinese character, and the font cannot be changed. The easiest thing to think of is, of course, the Win32 GDI painting, which is the simplest and most commonly used method and can change the font, size, and style. However, the disadvantage is also obvious. If each frame is constantly switched to GDI, the performance will be greatly reduced. In fact, d3d id3dxfont also uses GDI to draw words. It was quite slow before direct3d 9.0. After dx9.0, Microsoft made a major change to it and achieved a qualitative leap in performance, however, the real-time rendering of a large amount of text still has a great impact on the performance (I used to draw 50 lines per frame, and the performance has changed from more than 300 frames to more than 50 frames ).

Idea: Use GDI to draw the text and save it as a texture (in fact, the text in the game usually stays on the screen for a while), and manage the texture. Build a vertex buffer to paste the texture to a quadrilateral block.

Save the GDI text as idirect3dtexture9Code:

 

Idirect3dtexture9 * texture

HDC =: createcompatibledc (null); // create the device content HDC on the current desktop
settextcolor (HDC, RGB (255,255,255 )); // set the background color and text color
setbkcolor (HDC, 0xff000000);
setmapmode (HDC, mm_text);

DWORD * pbitmapbits; // create a bitmap
bitmapinfo;
zeromemory (& bitmapinfo. bmiheader, sizeof (bitmapinfoheader);
bitmapinfo. bmiheader. bisize = sizeof (bitmapinfoheader);
bitmapinfo. bmiheader. biwidth = 900;
bitmapinfo. bmiheader. biheight = 900;
bitmapinfo. bmiheader. biplanes = 1;
bitmapinfo. bmiheader. bicompression = bi_rgb;
bitmapinfo. bmiheader. bibitcount = 32;

Hbitmap = createdibsection (HDC, & bitmapinfo, dib_rgb_colors, (void **) & pbitmapbits, null, 0 );
SelectObject (HDC, hbitmap); // associate the bitmap with HDC so that the text time is saved in hbitmap.

Logfont lf;
Zeromemory (& lf, sizeof (logfont ));
Lf. lfheight = 72;
Lf. lfwidth = 0;

Hfont = createfontindirect (& lf); // create a 72 font
 
SelectObject (HDC, hfont );
Textout (HDC, 0, 0, "write here", strlen ("write here "));

Bitmap BMP;
GetObject (hbitmap, sizeof (Bitmap), & BMP );
Byte * buffer;
Int blength; // The length of the entire hbitmap file in bytes
Blength = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + BMP. bmwidthbytes * BMP. bmheight;
Buffer = new byte [blength];

Bitmapinfoheader Bi;
Bi. bisize = sizeof (bitmapinfoheader );
Bi. biwidth = BMP. bmwidth;
Bi. biheight = BMP. bmheight;
Bi. biplanes = 1;
Bi. bibitcount = BMP. bmbitspixel;
Bi. bicompression = bi_rgb;
Bi. bisizeimage = 0;
Bi. bixpelspermeter = 0;
Bi. biypelspermeter = 0;
Bi. biclrimportant = 0;
Bi. biclrused = 0;

Bitmapfileheader bmfhdr;
Bmfhdr. bftype = 0x4d42; // "BM"
Bmfhdr. bfsize = blength;
Bmfhdr. bfreserved1 = 0;
Bmfhdr. bfreserved2 = 0;
Bmfhdr. bfoffbits = (DWORD) sizeof (bitmapfileheader) + (DWORD) sizeof (bitmapinfoheader );

Memcpy (buffer, & bmfhdr, sizeof (bitmapfileheader ));
Memcpy (& buffer [sizeof (bitmapfileheader)], & BI, sizeof (bitmapinfoheader ));
Memcpy (& buffer [sizeof (bitmapfileheader) + sizeof (bitmapinfoheader)],
BMP. bmbits, blength-sizeof (bitmapfileheader)-sizeof (bitmapinfoheader ));

// Convert the constructed BMP data to the idirect3dtexture9 * texture
If (failed (d3dxcreatetexturefromfileinmemory (g_pdev, buffer, blength, & texture )))
{
Return s_false;
}

Delete [] buffer;
Deleteobject (hfont );
Deleteobject (hbitmap );
Deletedc (HDC );

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.