DirectFB Tutorials
Moving a sprite with the cursor keys (without input buffer)

#include <stdio.h>
#include <unistd.h>
 
#include <directfb.h>
 
static IDirectFB *dfb = NULL;
static IDirectFBSurface *primary = NULL;
static IDirectFBSurface *tux = 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 IDirectFBInputDevice *keyboard = NULL;
Our interface to the keyboard.
int main (int argc, char **argv)
{
 
  DFBSurfaceDescription   dsc;
IDirectFBImageProvider *provider;
(Locals)
  DFBInputDeviceKeyState escape = DIKS_UP;
To quit the application when escape is pressed we store the key state here.
  int sprite_x, sprite_y, max_x, max_y;
Here we store the current position of the sprite on the screen
and the maximum x/y coordinates the sprite can have without being clipped.
  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->GetInputDevice (dfb, DIDID_KEYBOARD, &keyboard));
Retrieve an interface to the keyboard so we can query key states.
DIDID_KEYBOARD is the device id of the primary keyboard.
  DFBCHECK (dfb->CreateImageProvider (dfb, DATADIR"/tux.png", &provider));
DFBCHECK (provider->GetSurfaceDescription (provider, &dsc));
DFBCHECK (dfb->CreateSurface (dfb, &dsc, &tux));
DFBCHECK (provider->RenderTo (provider, tux, NULL));
provider->Release (provider);
(Load the sprite)
  max_x = screen_width - dsc.width;
max_y = screen_height - dsc.height;
Calculate maximum x/y coordinates depending on screen resolution and sprite size.
  sprite_x = (screen_width - dsc.width) / 2;
sprite_y = (screen_height - dsc.height) / 2;
Initialize the sprite position by centering it on the screen.
  while (escape == DIKS_UP)
{
Loop through until the escape key is pressed.
      DFBInputDeviceKeyState state;
Local variable for key state queries.
      DFBCHECK (primary->FillRectangle (primary, 0, 0, screen_width, screen_height));
Clear the screen.
      DFBCHECK (primary->Blit (primary, tux, NULL, sprite_x, sprite_y));
Blit the sprite at its current position.
      DFBCHECK (primary->Flip (primary, NULL, DSFLIP_WAITFORSYNC));
Flip the front and back buffer, but wait for the vertical retrace to avoid tearing.
      DFBCHECK (keyboard->GetKeyState (keyboard, DIKI_LEFT, &state));
if (state == DIKS_DOWN)
sprite_x--;
Query state of cursor keys and eventually move the sprite.
      DFBCHECK (keyboard->GetKeyState (keyboard, DIKI_RIGHT, &state));
if (state == DIKS_DOWN)
sprite_x++;
 
      DFBCHECK (keyboard->GetKeyState (keyboard, DIKI_UP, &state));
if (state == DIKS_DOWN)
sprite_y--;
 
      DFBCHECK (keyboard->GetKeyState (keyboard, DIKI_DOWN, &state));
if (state == DIKS_DOWN)
sprite_y++;
 
      if (sprite_x < 0)
sprite_x = 0;
else if (sprite_x > max_x)
sprite_x = max_x;
if (sprite_y < 0)
sprite_y = 0;
else if (sprite_y > max_y)
sprite_y = max_y;
Now make sure we didn't move the sprite out of the screen.
      DFBCHECK (keyboard->GetKeyState (keyboard, DIKI_ESCAPE, &escape));
}
Query state of escape key.
  keyboard->Release (keyboard);
Release the keyboard.
  tux->Release (tux);
primary->Release (primary);
dfb->Release (dfb);
(Release)
  return 23;
}
 

  (C) Copyright by convergence GmbH