0. 每个DFB程序都需要一个IDirectFB主接口,大部分程序还需要一个最上层的,用于显示图片或视频的IDirectFBSurface,前者一般命名为dfb,后者一般叫primary,当然也有不喜欢这样取命字的。
1.将要显示的图片及其相关信息通过createimageprovider保存在一个imageprovider中
2. 通过getsurfacedescription函数将保存在imageprovider中的图片信息提取出来,存于某surfacedescription中
3. 利用这个surfacedescription建立一个新的idirectfbsurface,将imageprovider中的图片renderto到这个idirectfbsurface中
4. 使用fillrectangle清空屏幕,准备画图
5. 将后面生成的那个idirectfbsurface Blit到primary平面的指定区域
6. 使用Flip显示结果
以下是一个典型的图片显示程序:
-----------------------------------------Makefile-----------------------------------------------------
all : main.c showImage.c showImage.h
gcc $^ `pkg-config --cflags --libs directfb`
-----------------------------------------main.c------------------------------------------------------
#include
#include "showImage.h"
int main(int argc, char *argv[])
{
init_application(&argc, &argv);
if(argc < 2)
fprintf(stderr, "%s: 请指定要显示的图片名.\n", argv[0]);
showImage(argv[1]);
sleep(5);
return 0;
}
------------------------------------------showImage.h---------------------------------------------
#ifndef SHOWIMAGE_H_
#define SHOWIMAGE_H_
#include
static IDirectFB *dfb = NULL;
static IDirectFBSurface *primary = NULL;
static int screen_width = 0;
static int screen_height = 0;
static IDirectFBSurface *logo = NULL;
static IDirectFBEventBuffer *events = NULL;
static IDirectFBImageProvider *provider = NULL;
static DFBSurfaceDescription sdsc;
static DFBResult ret = DFB_OK;
extern void showImage(char *imageName);
extern void init_application(int *argc, char **argv[]);
extern void exit_application(int status);
#endif /* SHOWIMAGE_H_ */
------------------------------------------showImage.c---------------------------------------------
#include "showImage.h"
#include
void showImage(char *iamgeName)
{
/*将经过初始化的primary的长宽信息赋予screen_width和screen_height
*primary占满全屏,则其长宽值也即屏幕像素值*/
primary->GetSize(primary, &screen_width, &screen_height);
//获取provider,也即图片的相关信息
provider->GetSurfaceDescription(provider, &sdsc);
//使用上面得到的sdsc来创建一个平面logo,这个平面的大小与图片大小一致
dfb->CreateSurface(dfb, &sdsc, &logo);
//将图片呈递给刚才建立的logo平面,如果大小不一,则进行缩放
provider->RenderTo(provider, logo, NULL);
//清空屏幕
primary->FillRectangle(primary, 0, 0, screen_width, screen_height);
//将图片blit到平面
primary->Blit(primary, logo, NULL, (screen_width - sdsc.width) / 2,
(screen_height - sdsc.height) / 2);
//Flit整个屏幕
primary->Flip(primary, NULL, DSFLIP_WAITFORSYNC);
}
void init_application(int *argc, char **argv[])
{
//使用命令行参数初始化DFB
ret = DirectFBInit(argc, argv);
if(ret)
{
DirectFBError("DirectFBInit() failed.\n", ret);
exit_application(1); //请注意不同的错误对应不同的输出
}
//创建总接口(IDirectFB *dfb)
ret = DirectFBCreate(&dfb);
if(ret)
{
DirectFBError("DirectFBCreate() faild.\n", ret);
exit_application(2);
}
//要求进行全屏显示
dfb->SetCooperativeLevel(dfb, DFSCL_FULLSCREEN);
//对平面属性(surface description)进行相应设置
sdsc.flags = DSDESC_CAPS;
sdsc.caps = DSCAPS_PRIMARY | DSCAPS_DOUBLE;
//创建最上层的primary平面
ret = dfb->CreateSurface(dfb, &sdsc, &primary);
if(ret)
{
DirectFBError("IDirectFB::CreateSurface() faild.\n", ret);
exit_application(3);
}
//创建一个键盘事件缓存
ret = dfb->CreateEventBuffer(dfb, &events);
if(ret)
{
DirectFBError("IDirectFB::CreateEventBuffer() faild.\n", ret);
exit_application(4);
}
//创建一个imageprovider
ret = dfb->CreateImageProvider(dfb, "./shot.png", &provider);
if(ret)
{
DirectFBError("IDirectFB::CreateImageProvider() faild.\n", ret);
// exit_application(5);
}
}
void exit_application(int status)
{
//先释放资源
if(events)
events->Release(events);
if(provider)
provider->Release(provider);
if(primary)
primary->Release(primary);
if(dfb)
dfb->Release(dfb);
exit(status);
}
阅读(2599) | 评论(0) | 转发(0) |