DirectFB Tutorials
Keybuffer and simple alphachannel example

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
 
#include <directfb.h>
 
static IDirectFB *dfb = NULL;
static IDirectFBSurface *primary = NULL;
static IDirectFBSurface *foot = NULL;
static IDirectFBInputDevice *keyboard = 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 IDirectFBEventBuffer *buffer = NULL;
A buffer for input events.
int main (int argc, char **argv)
{
 
  DFBSurfaceDescription   dsc;
IDirectFBImageProvider *provider;
(Locals)
  int quit = 0;
Set this to non-null to quit.
  DFBCHECK (DirectFBInit (&argc, &argv));
DFBCHECK (DirectFBCreate (&dfb));
DFBCHECK (dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN));
dsc.flags = DSDESC_CAPS;
dsc.caps = DSCAPS_PRIMARY;
DFBCHECK (dfb->CreateSurface( dfb, &dsc, &primary ));
DFBCHECK (primary->GetSize (primary, &screen_width, &screen_height));
(Initialize)
  DFBCHECK (primary->FillRectangle (primary, 
0, 0, screen_width, screen_height));
(Clear)
  DFBCHECK (dfb->GetInputDevice (dfb, DIDID_KEYBOARD, &keyboard));
(Get keyboard)
  DFBCHECK (keyboard->CreateEventBuffer (keyboard, &buffer));
Create an event buffer for the keyboard.
  DFBCHECK (dfb->CreateImageProvider (dfb, DATADIR"/foot.png", &provider));
DFBCHECK (provider->GetSurfaceDescription (provider, &dsc));
DFBCHECK (dfb->CreateSurface (dfb, &dsc, &foot));
DFBCHECK (provider->RenderTo (provider, foot, NULL));
provider->Release (provider);
(Load the foot)
  DFBCHECK (primary->SetBlittingFlags (primary, DSBLIT_BLEND_ALPHACHANNEL));
Set blitting flags to DSBLIT_BLEND_ALPHACHANNEL that enables alpha
blending using the alpha channel of the source.
  while (!quit)
{
Loop as long as the escape key has not been pressed.
      DFBInputEvent event;
Structure which stores a DirectFB input event from an input buffer.
      DFBCHECK (buffer->WaitForEvent (buffer));
This makes the current thread wait idle for the next event.
      while (buffer->GetEvent (buffer, DFB_EVENT(&event)) == DFB_OK)
{
Fetch all events from buffer one by one and process them.
          if (event.type == DIET_KEYRELEASE)
DFBCHECK (primary->FillRectangle (primary,
0, 0,
screen_width, screen_height));
If any key went up, we clear the screen.
          if (event.type == DIET_KEYPRESS)
{
if (event.key_id == DIKI_ESCAPE)
quit = 1;
else
DFBCHECK (primary->Blit (primary, foot, NULL,
rand() % (screen_width - dsc.width),
rand() % (screen_height - dsc.height)));
}
}
If a key has been pressed and it's the escape key, we quit.
Otherwise we put a foot print somewhere.
    }
We do no flipping here because we created a non flipping primary
surface.
  buffer->Release (buffer);
Release the input buffer.
  keyboard->Release (keyboard);
foot->Release (foot);
primary->Release (primary);
dfb->Release (dfb);
(Release)
  return 23;
}
 

  (C) Copyright by convergence GmbH