DirectFB Tutorials
Drawing text

#include <stdio.h>
#include <stdlib.h>
#include <string.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 ); \
} \
}
(Globals)
static IDirectFBFont *font = NULL;
The font we will use to draw the text.
static char *text = "DirectFB rulez!";
The string we will draw. Strings in DirectFB have to UTF-8 encoded.
For ASCII characters this does not make any difference.
int main (int argc, char **argv)
{
int i, width;
 
  DFBFontDescription font_dsc;
A structure describing font properties.
  DFBSurfaceDescription dsc;
(Locals)
  DFBCHECK (DirectFBInit (&argc, &argv));
DFBCHECK (DirectFBCreate (&dfb));
DFBCHECK (dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN));
dsc.flags = DSDESC_CAPS;
dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
DFBCHECK (dfb->CreateSurface( dfb, &dsc, &primary ));
DFBCHECK (primary->GetSize (primary, &screen_width, &screen_height));
(Initialize)
  font_dsc.flags = DFDESC_HEIGHT;
font_dsc.height = 48;
DFBCHECK (dfb->CreateFont (dfb, DATADIR"/decker.ttf", &font_dsc, &font));
First we need to create a font interface by passing a filename
and a font description to specify the desired font size. DirectFB will
find (or not) a suitable font loader.
  DFBCHECK (primary->SetFont (primary, font));
Set the font to the surface we want to draw to.
  DFBCHECK (font->GetStringWidth (font, text, -1, &width));
Determine the size of our string when drawn using the loaded font.
Since we are interested in the full string, we pass -1 as string length.
  for (i = screen_width; i > -width; i--)
{
We want to let the text slide in on the right and slide out on the left.
      DFBCHECK (primary->SetColor (primary, 0x0, 0x0, 0x0, 0xFF));
DFBCHECK (primary->FillRectangle (primary, 0, 0, screen_width, screen_height));
Clear the screen.
      DFBCHECK (primary->SetColor (primary, 0x80, 0x0, 0x20, 0xFF));
Set the color that will be used to draw the text.
      DFBCHECK (primary->DrawString (primary, text, -1, i, screen_height / 2, DSTF_LEFT));
Draw the text left aligned with "i" as the X coordinate.
      DFBCHECK (primary->Flip (primary, NULL, DSFLIP_WAITFORSYNC));
}
Flip the front and back buffer, but wait for the vertical retrace to avoid tearing.
  font->Release (font);
Release the font.
  primary->Release (primary);
dfb->Release (dfb);
(Release)
  return 23;
}
 

  (C) Copyright by convergence GmbH