Document directory
- Step 1: declare a pointer to the allegro_ustr type
- Step 2: Create a string for a UTF-16
- Step 3: render a string
- Step 4: destroy the resources occupied by the string at the end
This series of articles is written by Sky. For more information, see the source. Http://blog.csdn.net/qq573011406/article/details/8263966
Author: Yuan quanwei mail: qq573011406@126.com welcome mail exchange programming experience
Index of this series of tutorials:
Tutorial series of 2D game engine Allegro (1) configure the allegro Development Environment
2d game engine Allegro series tutorial (2) Hello world!
2d game engine Allegro series tutorial (3) load and display pictures!
2d game engine Allegro series tutorial (4) text rendering and display of Chinese Characters
I. Introduction
Allegro can render text in two ways: bitmap font files or TTF font files. Here we will mainly talk about TTF.
TTF:
Allegro uses the FreeType library to load font files, extract the fonts, and render them. This is also a common method. This method supports common font files such as TrueType, OpenType, type1, CID, CFF, Windows Fon/fnt, and X11 PCF.
In Windows, font files are stored in the "C: \ WINDOWS \ fonts \" directory.
Ii. text rendering process
Step 1: Include the header file
Step 2: Link Library
Step 3: declare an allegro_font pointer
Step 4: Initialize the TTF plug-in
Step 5: load the font to allegro_font
Step 6: render the text to the screen
Step 7: Release occupied Resources
3. Step 1: Include the header file
The following header files must be included:
#include <allegro5/allegro_font.h>#include <allegro5/allegro_ttf.h>
Step 2: Link Library
You need to link the following libraries:
#pragma comment(lib,"allegro_font-5.0.7-mt-debug.lib") #pragma comment(lib,"allegro_ttf-5.0.7-mt-debug.lib")
Step 3: declare an ALLEGRO_FONT pointer
ALLEGRO_FONT *font=0;
Step 4: Initialize the font plug-in and ttf plug-in
The following two functions can be used to initialize the FONT plug-in and TTF plug-in respectively. The TTF plug-in is used to load various types of FONT files.
al_init_font_addon();al_init_ttf_addon();
Step 5: load the font to ALLEGRO_FONT
ALLEGRO_FONT * al_load_font (char const * filename // font file address, int size // font file size, int flags) // ALLEGRO_TTF_NO_KERNING no padding adjustment // ALLEGRO_TTF_MONOCHROME do not use anti-aliasing
ALLEGRO_FONT * al_load_font (char const * filename // font file address, int size // font file size, int flags) // ALLEGRO_TTF_NO_KERNING no padding adjustment // ALLEGRO_TTF_MONOCHROME do not use anti-aliasing
Step 6: render the text to the screen
The following functions render text:
Render a string
Void al_draw_text (const ALLEGRO_FONT * font, // font pointer ALLEGRO_COLOR // you can use the al_map_rgb (255, 0, 0) function to obtain the color, float x, float y // position where the text is rendered. The ALLEGRO_ALIGN_LEFT ALLEGRO_ALIGN_CENTRE ALLEGRO_ALIGN_LEFT ALLEGRO_ALIGN_RIGH value of int flags can be left ALLEGRO_ALIGN_RIGH by default, char const * text) // the string to be rendered
Renders a formatted string.
void al_draw_textf(const ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, int flags, const char *format, ...)
The function below is similar to al_draw_text, except that the text is limited to a region.
Diff difference refers to the spacing of words in the text. If diff is equal to the width of the text, the string is tiled in this area.
void al_draw_justified_text(const ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x1, float x2, float y, float diff, int flags, const char *text)
Format ....
void al_draw_justified_textf(const ALLEGRO_FONT *f, ALLEGRO_COLOR color, float x1, float x2, float y, float diff, int flags, const char *format, ...)
For example, we can use the al_draw_text function.
Al_draw_text (font, al_map_rgb (, 0), 0, "hello ");
Step 7: Release occupied Resources
You can use the following function to release the resources occupied by the font.
void al_destroy_font(ALLEGRO_FONT *f)
Eg:
al_destroy_font(font);
Let's take a look at the running results.
It's strange, the string we were going to render is
Al_draw_text (font, al_map_rgb (, 0), 0, "hello ");
However, only the English part is displayed. "Hello" is not displayed.
This is because
al_draw_text()
This function supports string encoding for UTF-8, while we write in the program source file (. in cpp), VS is automatically converted to the same encoding method as the system environment. Therefore, only Chinese characters can be displayed, but Chinese characters cannot be displayed.
If you want to display Chinese characters, you can use other methods.
Iv. How to display Chinese Characters in Allegro 5
There are two ways to display Chinese characters:
One is to store the string in a text file and save the text file as a UTF-8 type. Load the string with the program and then display it ..
However, this method is too troublesome. Here we will introduce another method.
That is, a string of the wchar_t type that uses the UTF-16.
In allegro, there is a library dedicated to UTF strings.
Let's take a look at the steps for using this library.
1. Steps
Step 1: declare a pointer to the allegro_ustr type
Step 2: Create a string for a UTF-16
Step 3: render a string
Step 4: destroy the resources occupied by the string at the end
II. Implementation Step 1: declare a pointer to the allegro_ustr type
ALLEGRO_USTR *ustr;
Step 2: Create a string for a UTF-16
USTR = al_ustr_new_from_utf16 (uint16_t *) l "Hello! ");
Step 3: render a string
al_draw_ustr(font,al_map_rgb(255,255,255),100,100,0,ustr);
Step 4: destroy the resources occupied by the string at the end
al_ustr_free(ustr);
Iii. Result 5: complete source code
// Header file # define ALLEGRO_NO_MAGIC_MAIN # include <Windows. h> # include <allegro5/allegro. h> # include <allegro5/allegro_font.h> # include <allegro5/allegro_ttf.h> # pragma comment (lib, "allegro-5.0.7-mt-debug.lib") // link Allegro library # pragma comment (lib, "allegro_font-5.0.7-mt-debug.lib") // link Allegro library # pragma comment (lib, "allegro_ttf-5.0.7-mt-debug.lib ") // link the database of Allegro ////////////////////////////////// /////////////////// //////////////////////////////////////// /Function int game_init (); // initialize the game int game_run (); // enter the game loop int game_frame (); // The logic processing function int game_render (); // The rendering function int game_distory (); // release the resource int game_msg (); // Message Processing // constant const int WIN_WIDTH = 800; // window width const int WIN_HEIGHT = 600; // window height // global variable ALLEGRO_DISPLAY * display; // display the device ALLEGRO_EVENT_QUEUE * queue; // event queue ALLEGRO_EVENT my_event; // event ALLEGRO_FONT * font = 0; ALLEGRO_USTR * ustr; ///////// //////////////////////////////////////// //////////////////////////////////////// /// Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {int error = 0; // write the game initialization code in our game_init () in the function // put it here and execute // assign the returned value of game_init () to error // If the returned value is 0, it means everything is normal and Initialization is successful! // If the value is not 0, an error occurs during initialization. // End the program immediately and return the error code error = game_init (); if (error! = 0) return error; // After initialization, the main loop of the game starts to enter. // The main loop is executed cyclically, messages are processed, and logic is processed, and the rendering function error = game_run (); if (error! = 0) return error; // destroy the occupied resource after the game ends. error = game_distory (); if (error! = 0) return error; return 0;} int game_init () {if (! Al_init () return 1; /*************************************** ********************************** use al_init () al_init is actually a macro. It actually calls al_install_system (ALLEGRO_VERSION_INT, atexit). The return value of this function is boolean. If the initialization is successful, true is returned, otherwise, false * // ********************************* is returned *//********************************** * **********************************/al_init_font_addon (); al_init_ttf_addon (); al_install_mouse (); al_install_keyboard ();/****************** **************************************** ************* // Initialize the mouse, and keyboard devices, so that Allegro can receive mouse and keyboard messages *//**************************** **************************************** * *** // al_set_new_display_flags (ALLEGRO_FULLSCREEN ); // if you cancel the annotation of this line, the full screen mode is display = al_create_display (WIN_WIDTH, WIN_HEIGHT); if (! Display) return 2; /*************************************** ******************************** // * ALLEGRO_DISPALY * al_create_display (int w, int h) when the display device parameter w is the width of the window to be created, and h is the height of the window, a disply pointer is returned, otherwise, NULL/*********************************** is returned /************************************ ********************************** // set windows title al_set_window_title (display, "Allrgro series tutorial (4) show text"); // initialize the event queue. The received messages are stored in this queue in queue = al_create_event_queue (); // specify Message queue (queue, queue (); queue (queue, al_get_display_event_source (display); font = al_load_ttf_font ("simfang. ttf", 24,1); if (! Font) return 3; ustr = al_ustr_new_from_utf16 (uint16_t *) L "Hello! "); If (! Ustr) return 4; return 0;} int game_msg () {al_wait_for_event (queue, & my_event ); /*************************************** ******************************* // void al_wait_for_event (ALLEGRO_EVENT_QUEUE * queue, ALLEGRO_EVENT * ret_event) when a new event exists in the queue, copy the content of the new event to ret_event, and remove it from the event queue *//****************************** **************************************** **/if (my_event.type = ALLEGRO_EVENT_DISPLAY_CLOSE) return 98; // if (my_event.type = ALLEGRO_EVENT_KEY_CHAR) {if (my_event.keyboard.keycode = ALLEGRO_KEY_ESCAPE) when the window is closed, return 99;} return 0 ;} int game_frame () {return 0;} int game_render () {// fill the screen with the specified color al_clear_to_color (al_map_rgb (, 0); al_draw_text (font, al_map_rgb (, 0, 0), 255,255,255, 0, "hello"); al_draw_ustr (font, al_map_rgb (100,100), 0, ustr); // flip al_flip_display (); return 0 ;} int game_run (){// The following variable is used to record the time, in seconds // used to control the frame rate (FPS), that is, the number of frames refreshed per second. // here we set the FPS to 30, 1 second time unit/FPS // that is, 0.033, that is, the interval between each refresh cannot be less than 0.033 seconds double t_now = 0.0; // double t_pre = 0.0 at the current time; int error = 0; while (true) {if (! Al_is_event_queue_empty (queue) {// check whether a new event error = game_msg () exists in the correct event; // if (error! = 0) return error;} else {// if no new event exists in the event queue, // when calculating the event interval, it is greater than 0.033t _ now = al_get_time (); if (t_now-t_pre> = 0.033) {// if the interval is appropriate then // update the new frame of the game logic error = game_frame (); if (error! = 0) return error; // refresh the screen. error = game_render (); if (error! = 0) return error; t_pre = t_now;} else {// when the CPU can process game logic beyond our needs, // call Sleep (0) // This Windows API will allow the current thread to release its control, // This is very important to the Windows platform, so that the game will not occupy all of the // CPU, each time the game is switched, the available time slice is used up al_rest (0) ;}}return 0 ;}int game_distory () {al_destroy_font (font ); al_ustr_free (ustr); return 0 ;}