This article aims to draw a box and draw a string of characters on the box.
Implementation steps: first create the IDirectFB interface, use it to create the surface to be displayed, and create a font. You must set the font to draw characters; otherwise, the painting will fail. Then, clear the window, fill the entire surface with black, and draw boxes and characters.
# Include <stdio. h>
# Include <unistd. h>
# Include <directfb. h>
Static IDirectFB * dfb = NULL;
Static IDirectFBSurface * primary = NULL;
Static int screen_width = 0;
Static int screen_height = 0;
# Define DFBCHECK (x ...)\
{\
DFBResult err = x ;\
\
If (err! = DFB_ OK )\
{\
Fprintf (stderr, "% s <% d>: \ n \ t" ,__ FILE __,__ LINE __);\
DirectFBErrorFatal (# x, err );\
}\
}
Int main (int argc, char ** argv)
{
DFBSurfaceDescription dsc; // descriptor required for surface creation
DFBFontDescription font_dsc; // descriptor required for font creation
IDirectFBFont * font = NULL; // font
DFBCHECK (DirectFBInit (& argc, & argv ));
DFBCHECK (DirectFBCreate (& dfb); // create a general interface
DFBCHECK (dfb-> SetCooperativeLevel (dfb, DFSCL_FULLSCREEN ));
Dsc. flags = DSDESC_CAPS;
Dsc. caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
DFBCHECK (dfb-> CreateSurface (dfb, & dsc, & primary); create a surface
DFBCHECK (primary-> GetSize (primary, & screen_width, & screen_height); // Obtain the height and width of the screen
DFBCHECK (primary-> SetColor (primary, 0x0, 0xff); // you need to set the color of the paint brush before each painting, otherwise, the default or last set color will be used.
DFBCHECK (primary-> FillRectangle (primary, screen_width, screen_height); // fill the entire surface plane
DFBCHECK (primary-> SetColor (primary, 0x80, 0x80, 0xff, 0xff); // you can specify the paint color.
DFBCHECK (primary-> DrawRectangle (primary, 0,0, screen_width/3,
Screen_height/3); // draw a sitting cursor at (1/3). The width is 1/3 of the screen width, and the height is of the screen height.
Font_dsc.flags = DFDESC_HEIGHT;
Font_dsc.height = 50;
DFBCHECK (dfb-> CreateFont (dfb, "./decker. ttf", & font_dsc, & font); // Create a font
DFBCHECK (primary-> SetFont (primary, font); // set the font, which is the same as the paint brush, before each draw character
DFBCHECK (primary-> DrawString (primary, "hello world",-1, screen_width/6,
Screen_height/6, DSTF_CENTER); // draw a string with the center (screen_width/6, screen_height/6)
DFBCHECK (primary-> Flip (primary, NULL, DSFLIP_NONE); displays the diagram of the buffer (that is, the filled and drawn box and string).
Sleep (5); // wait for 5 seconds. Otherwise, the system will return in seconds and will not see the effect.
Font-> Release (font );
Primary-> Release (primary );
Dfb-> Release (dfb );
Return 0;
}
DirectFB study note 2