DirectFB实例1--加载一幅图片


在DirectFB中加载一幅图片,并显示:

1. 通过IDirectFBImageProvider加载一幅图片.

2. 创建一个DirectFBSurface,并将图片数据放入其中.

3. 将该Surface的内容Blit到主Surface, 以更进行显示.


下面用代码解释上述三个步骤.


在执行上述步骤前,首先得进行上下文初始化,对于DirectFB程序来说,这些初始化步骤是一样的.


DFBResult ret;
   //初始化DirectFB
   ret = DirectFBInit(&argc, &argv);
   if (ret != DFB_OK) {
       cout<<"初始化DirectFB失败!"<<endl;
       return -1;
   }
   DirectFBCreate(&dfb);

   dfb->SetCooperativeLevel (dfb, DFSCL_NORMAL);
   //设置屏幕模式
   dfb->SetVideoMode(dfb, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);

然后创建一个主Surface :

dsc.flags = DSDESC_CAPS;
    dsc.caps = (DFBSurfaceCapabilities)(DSCAPS_PRIMARY | DSCAPS_FLIPPING);
    dfb->CreateSurface(dfb, &dsc, &screen);

接下来是加载图片:

//加载图片
    ret = dfb->CreateImageProvider (dfb, IMGDIR"/helloworld.png", &img_provider);
    if (ret != DFB_OK) {
         cout<<"加载图片失败!"<<endl;
        return -1;
    }

然后根据ImageProvider提供的描述信息,量身创建一个匹配的Surface.

img_provider->GetSurfaceDescription(img_provider, &dsc);
    dfb->CreateSurface(dfb, &dsc, &hello);
    img_provider->RenderTo(img_provider, hello, NULL);
    img_provider->Release(img_provider);

最后,将图片数据复制到主Surface上,

//将图片应用到屏幕上
    screen->Blit(screen, hello, NULL, 0, 0);

对于双Buffer的主Surface,需要通过以下调用 才用在屏幕上看到输出:

//更新屏幕
    screen->Flip (screen, NULL, DSFLIP_WAITFORSYNC);



设置Color Key, 一般用于去掉背景色

static void apply_surface(int x, int y, IDirectFBSurface *source, IDirectFBSurface *destination)
    {
        destination->SetBlittingFlags(destination, DSBLIT_SRC_COLORKEY );
        source->SetSrcColorKey(source, 0x0, 0xFF, 0xFF);
//        destination->DisableAcceleration(destination, DFXL_BLIT);
        destination->Blit(destination, source, NULL, x, y);
    }


你可能感兴趣的:(DirectFB实例1--加载一幅图片)