DirectFB Tutorials
Image loading and displaying

#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 ); \
} \
}
(Globals)
static IDirectFBSurface *logo = NULL;
The image is to be loaded into a surface that we can blit from.
int main (int argc, char **argv)
{
int i;
 
  DFBSurfaceDescription dsc;
(Locals)
  IDirectFBImageProvider *provider;
Loading an image is done with an Image Provider.
  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)
  DFBCHECK (dfb->CreateImageProvider (dfb, DATADIR"/dfblogo.png", &provider));
First we need to create an Image Provider by passing a filename.
DirectFB will find (or not) an Image Provider for the file type.
  DFBCHECK (provider->GetSurfaceDescription (provider, &dsc));
Get a surface description from the provider. It will contain the width,
height, bits per pixel and the flag for an alphachannel if the image has
one. If the image has no alphachannel the bits per pixel is set to the
bits per pixel of the primary layer to use simple blitting without pixel
format conversion.
  DFBCHECK (dfb->CreateSurface( dfb, &dsc, &logo ));
Create a surface based on the description of the provider.
  DFBCHECK (provider->RenderTo (provider, logo, NULL));
Let the provider render to our surface. Image providers are supposed to
support every destination pixel format and size. If the size differs the
image will be scaled (bilinear). The last parameter allows to specify
an optional destination rectangle. We use NULL here so that our image
covers the whole logo surface.
  provider->Release (provider);
Release the provider, we don't need it anymore.
  for (i = -dsc.width; i < screen_width; i++)
{
We want to let the logo slide in on the left and slide out on the right.
      DFBCHECK (primary->FillRectangle (primary, 0, 0, screen_width, screen_height));
Clear the screen.
      DFBCHECK (primary->Blit (primary, logo, NULL, i, (screen_height - dsc.height) / 2));
Blit the logo vertically centered with "i" as the X coordinate.
NULL means that we want to blit the whole surface.
      DFBCHECK (primary->Flip (primary, NULL, DSFLIP_WAITFORSYNC));
}
Flip the front and back buffer, but wait for the vertical retrace to
avoid tearing.
  logo->Release (logo);
Release the image.
  primary->Release (primary);
dfb->Release (dfb);
(Release)
  return 23;
}
 

  (C) Copyright by convergence GmbH