#include <stdio.h>
#include <directfb.h>


static IDirectFB *dfb = NULL;
static IDirectFBSurface *primary = NULL;
static IDirectFBSurface *overlay = NULL;
static IDirectFBDisplayLayer *bes = NULL;

int main(int argc, char *argv[]) {

   DFBResult err;
   DFBSurfaceDescription dsc;
   DFBDisplayLayerConfig bes_config;
   DFBSurfaceCapabilities bes_caps;
   int width, height;
      
   err = DirectFBInit( &argc, &argv);
   
   if(err) {
      DirectFBError( "DirectFBInit() failed", err );
      return -1;
   }
   
   err = DirectFBCreate( &dfb );
   
   if(err) {
      DirectFBError( "DirectFBCreate() failed", err );
      return -1;
   }
   
   dfb->SetCooperativeLevel( dfb, DFSCL_FULLSCREEN );
   
   dsc.flags = DSDESC_CAPS | DSDESC_PIXELFORMAT;
   dsc.caps = DSCAPS_PRIMARY | DSCAPS_VIDEOONLY;
   dsc.pixelformat = DSPF_RGB32;
   
   dfb->CreateSurface( dfb, &dsc, &primary );
   primary->GetSize( primary, &width, &height );
   primary->FillRectangle( primary, 0, 0, width, height );
   primary->SetColor( primary, 0x50, 0x00, 0x00, 0xff );  //Alplha is set to 0xff but it wont be used coz pixel format is RGB32
   primary->FillRectangle( primary, 40, 40, 100, 100 );
   sleep(2);
   
   /* Get hold of bes display layer */
   dfb->GetDisplayLayer( dfb, 1, &bes );
   bes->SetCooperativeLevel( bes, DLSCL_EXCLUSIVE );
   
   /* Set its width and height */
   bes_config.flags = DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_PIXELFORMAT;
   bes_config.width = 1024;
   bes_config.height = 768;
   bes_config.pixelformat = DSPF_RGB16;
   bes->SetConfiguration( bes, &bes_config ); 
   
   
   bes->GetSurface( bes, &overlay );
   overlay->SetColor( overlay, 0x0, 0xf, 0x0, 0x00 );
   overlay->GetSize( overlay, &width, &height );
   printf("BES layer's surface has width=%d height=%d\n", width, height );

   overlay->GetCapabilities( overlay, &bes_caps );
   
   printf("BES caps %x\n", bes_caps );
   
   overlay->DrawRectangle( overlay, 50, 50, 100, 100 );
   printf("Drawn on BES layer\n");
   
   sleep(2);
   
   
   primary->Release( primary );
   dfb->Release( dfb );
   
}

   
