Home > Back-end >  For a Linux using SDL_ttf display text demo
For a Linux using SDL_ttf display text demo

Time:09-16

As title, it is to find for a long time, haven't find a good example of control myself writing also strange wonderful out of the

This is the code I wrote (function is to bring up a window, shown above) pay attention to rest know brother help to look at, can write a better demo:
 
Void rest_remander (void) {

//initialize the SDL
SDL_Surface * screen;
Uint32 color;

If (SDL_Init (SDL_INIT_VIDEO) & lt; 0 {
//fprintf (stderr, "unable to initialize the SDL: % s \ n", SDL_GetError);
//exit (1);
WriteLog (" unable to initialize the SDL ");
return;

}
Screen=SDL_SetVideoMode (640480, SDL_SWSURFACE);
If (screen==NULL) {
//fprintf (stderr, "unable to set 640 * 480 bits of color video mode: % s \ n", SDL_GetError);
//exit (1);
WriteLog (" unable to set the video mode ");
return;
}
Atexit (SDL_Quit);

//initialize the vera.ttf library
If (TTF_Init ()==1) {
WriteLog (" initialize the vera.ttf library failed ");
return -1;
}
//create a the number font file TTF_Font
TTF_Font * the font;
The font=TTF_OpenFont ("/usr/local/share/fonts/simfang. The vera.ttf ", 16);
if(! The font) {
Printf (" TTF_OpenFont: Open/usr/local/share/fonts/simfang. The vera.ttf % s \ n ", TTF_GetError ());
WriteLog (" failed to create a font ");
return -1;
}
//set the font color
SDL_Color textColor={255, 255, 255};
SDL_Surface * message;
Message=TTF_RenderText_Solid (font, "pay attention to rest," textColor);
If (the message) {
Apply_surface (0, 0, message, screen);
}
//refresh screen
If (SDL_Flip (screen)==1) {
WriteLog (" refresh screen failure ");
The return - 2;
}
SDL_Delay (2000);

SDL_FreeSurface (message);

//release font
TTF_CloseFont (font);
TTF_Quit ();
return ;
}
Void apply_surface (int x, int y, SDL_Surface * source and destination SDL_Surface *) {
//Temporary rectangle to hold the offsets
SDL_Rect offset.
//Get the offsets
Offset. X=x;
Offset. Y=y;
//Blit the surface
SDL_BlitSurface (source, NULL, destination, & amp; Offset);
}

CodePudding user response:

The inside of the writeLog () function is to write logs
  • Related