How to get started with DirectFB.

From Gumstix User Wiki
Jump to: navigation, search

First, what is DirectFB? According to http://directfb.org:

DirectFB is a thin library that provides hardware graphics acceleration, input device handling and abstraction, integrated windowing system with support for translucent windows and multiple display layers, not only on top of the Linux Framebuffer Device. It is a complete hardware abstraction layer with software fallbacks for every graphics operation that is not supported by the underlying hardware. DirectFB adds graphical power to embedded systems and sets a new standard for graphics under Linux.

To write programs that use DirectFB you first have to have it installed. You can either use an image that contains it (gumstix-directfb-image) or you can install it with

ipkg install directfb

After that is installed you will be ready to make your first program. You should have already done the hello world tutorial. Make a folder in user.collection/packages called dfb. Then make your dfb_1.0.0.bb file and fill it with this.

DESCRIPTION = "a directfb sample"
DEPENDS = "directfb"
SECTION = "libs"
LICENSE = "GPL"

PR = "r0"

SRC_URI = " \
 file://dfb.c \
"

S = "${WORKDIR}"

CFLAGS += "-I/${STAGING_INCDIR}/directfb"

LDFLAGS += "-ldirectfb"

do_compile () {
   ${CC} ${CFLAGS} ${LDFLAGS} -o dfb dfb.c
}

do_install () {
   install -d ${D}${bindir}/
   install -m 0755 ${S}/dfb ${D}${bindir}/
}

FILES_${PN} = "${bindir}/dfb"

Next make a folder called files and make a file called dfb.c in it. dfb.c should contain

#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;

  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));
  DFBCHECK (primary->FillRectangle (primary, 0, 0, screen_width, screen_height));

  DFBCHECK (primary->SetColor (primary, 0x80, 0x80, 0xff, 0xff));
  DFBCHECK (primary->DrawLine (primary,
			                      0, screen_height / 2,
			       screen_width - 1, screen_height / 2));
  DFBCHECK (primary->Flip (primary, NULL, 0));

  sleep (5);

  primary->Release( primary );
  dfb->Release( dfb );

  return 23;
}

This example code came from http://www.directfb.org/docs/DirectFB_Tutorials/simple.html

You should now be ready to compile with

bitbake dfb

then you can install it following the same method in the hello world tutorial.