SDL displays Chinese (only one function)

Source: Internet
Author: User

In fact, it was very easy for SDL to display Chinese characters. I didn't know it at the beginning, and I had been worried for a long time, but I found it too simple.
To display Chinese is a function. Use ttf_renderutf8_solid (font, "Hello !! ", Textcolor.


You can try


Below is a test code I wrote:


# Include <stdio. h>
# Include <stdlib. h>
# Include <mysql. h>
# Include "SDL/SDL. H"
# Include "SDL/sdl_image.h"
# Include "SDL/sdl_ttf.h"


Const int screen_width = 1000;
Const int screen_height = 750;
Const int screen_bpp = 32;


Sdl_surface * Background = NULL;
Sdl_surface * screen = NULL;
Sdl_surface * message = NULL;


Sdl_event event;


Ttf_font * font = NULL;
Sdl_color textcolor = {255, 0, 0}; // set the color
/* Load the image */
Sdl_surface * load_image (char filename [20])
{
Sdl_surface * loadedimage = NULL;
Sdl_surface * optimizedimage = NULL;
Loadedimage = sdl_loadbmp (filename );
If (loadedimage! = NULL)
{
Optimizedimage = sdl_displayformat (loadedimage );
Sdl_freesurface (loadedimage );
}
Return optimizedimage;
}


Void apply_surface (int x, int y, int W, int H, sdl_surface * picture, sdl_surface * screen)
{
Sdl_rect offset;


Offset. x = X;
Offset. Y = y;
Offset. W = W;
Offset. H = h;
Sdl_blitsurface (picture, null, screen, & offset );
}
/* Load the image */


Int Init () // initialize the Function
{
If (ttf_init () =-1)
{
Exit (1 );
}
Font = ttf_openfont ("simfang. TTF", 50 );
If (font = NULL)
{
Exit (1 );
}
If (sdl_init (sdl_init_everything) =-1)
{
Exit (1 );
}
Screen = sdl_setvideomode (screen_width, screen_height, screen_bpp, sdl_swsurface );
If (screen = NULL)
{
Exit (1 );
}
Sdl_wm_setcaption ("test", null );

Return 1;
}


Void freesdl () // release function
{
Sdl_freesurface (background );
Ttf_closefont (font );
Ttf_quit ();
Sdl_quit ();
}


Int main ()
{
If (Init ()! = 1)
{
Exit (1 );
}
Background = load_image ("background.bmp ");
Apply_surface (0, 0, 0, background, screen );
Message = ttf_renderutf8_solid (font, "Hello !! ", Textcolor); // Add it to Chinese
Apply_surface (300,300, message, screen );

If (sdl_flip (screen) =-1)
{
Exit (1 );
}
Int quit = 0;
Int SnO;
While (! Quit)
{
While (sdl_pollevent (& event ))
{
If (event. type = sdl_quit)
{
Quit = 1;
}
Sdl_flip (screen );
}
}
Freesdl ();
Return 0;
}

The effect is as follows:


SDL displays Chinese (only one function)

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.