DirectFB Tutorials
Simple fullscreen application that draws a horizontal line

#include <stdio.h>
#include <unistd.h>
 
#include <directfb.h>
 
static IDirectFB *dfb = NULL;
This is the super interface, it's the entry point to all functionality.
static IDirectFBSurface *primary = NULL;
The primary surface, i.e. the "screen".
In cooperative level DFSCL_FULLSCREEN it's the surface of the primary layer.
static int screen_width  = 0;
static int screen_height = 0;
Store the width and height of the primary surface here to support all resolutions.
#define DFBCHECK(x...)                                         \
{ \
DFBResult err = x; \
\
if (err != DFB_OK) \
{ \
fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \
DirectFBErrorFatal( #x, err ); \
} \
}
An error checking macro for a call to DirectFB.
It is suitable for very simple apllications or tutorials.
In more sophisticated applications this general error checking should not be used.
int main (int argc, char **argv)
{
 
  DFBSurfaceDescription dsc;
A surface description is needed to create a surface.
  DFBCHECK (DirectFBInit (&argc, &argv));
Initialize DirectFB passing argc and argv
to support the standard DirectFB command line options.
DirectFB command line options will be stripped out automatically.
  DFBCHECK (DirectFBCreate (&dfb));
Create the super interface.
  DFBCHECK (dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN));
We want to go fullscreen,
the primary surface will be exclusive access to the surface of the primary layer.
If you disable this call a window will be created implicitly,
no further changes needed, flipping the surface updates the window.
  dsc.flags = DSDESC_CAPS;
dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
1. Specify which fields of the struct are set.
2. Fill out fields,
in this example we want to have a flippable primary surface.
  DFBCHECK (dfb->CreateSurface( dfb, &dsc, &primary ));
Create the primary surface by passing our surface description.
  DFBCHECK (primary->GetSize (primary, &screen_width, &screen_height));
We have exclusive access to the primary layer's surface now,
get the width and height of the surface, i.e. the screen resolution.
  DFBCHECK (primary->FillRectangle (primary, 0, 0, screen_width, screen_height));
Clear the screen by filling a rectangle with the size of the surface.
Default color is black, default drawing flags are DSDRAW_NOFX.
  DFBCHECK (primary->SetColor (primary, 0x80, 0x80, 0xff, 0xff));
DFBCHECK (primary->DrawLine (primary,
0, screen_height / 2,
screen_width - 1, screen_height / 2));
Draw a horizontal line in the middle of the screen.
Current color is black, so we have to set another one before.
  DFBCHECK (primary->Flip (primary, NULL, 0));
Now flip the whole surface to make things visible.
  sleep (5);
Wait a bit to see the result before exiting the example application.
  primary->Release( primary );
dfb->Release( dfb );
Cleanup in a stack like style.
  return 23;
}
 

  (C) Copyright by convergence GmbH