#include <stdio.h>
#include <unistd.h>
#include <directfb/directfb.h>
static IDirectFB *dfb = NULL;
static IDirectFBEventBuffer *events = NULL;
static IDirectFBSurface *primary = NULL;
static IDirectFBSurface *logo = NULL;//此IDirectFBSurface为用于显示的图片
static DFBSurfaceDescription dsc;
static IDirectFBImageProvider *provider; //要显示图片,必须要有一个IDirectFBImageProvider,
//此接口一般用于收集图片信息,
//并将图片提供给IDirectFBSurface显示
static int screen_width = 0;
static int screen_height = 0;
static int pic_x = 0; //图片的x坐标
static int pic_y = 0; //图片的y坐标
int myBitBlit(int x, int y)//绘制图形
{
primary->FillRectangle(primary, 0, 0, screen_width - 1,screen_height - 1);
primary->Blit(primary, logo, NULL, x, y);
//primary->Blit(primary, logo, NULL, i, (screen_height- dsc.height) / 2);
//上面Blit函数原型为:
//Blit(IDirectFBSurface *thiz, IDirectFBSurface *source, const DFBRectangle *source_rect, int x, int y);
//如果source_rect为NULL,则使用整个平面。DFBRctangle为一个矩形框,由两个点的坐标确定。
//x, y为图像左上角在屏幕上的坐标。
primary->Flip(primary, NULL, DSFLIP_WAITFORSYNC);
return 0;
}
int exit_app( void)//注销函数,销毁申请的变量
{
/* Release the event buffer. */
if (events){
events->Release( events );
}
if(logo){
logo->Release(logo);
}
if(primary){
primary->Release(primary);
}
if(dfb){
dfb->Release(dfb);
}
exit(0);
}
int actionMonitor(void)
{
DFBEvent evt;
events->WaitForEvent(events); //等待事件
while(events->GetEvent(events, &evt) == DFB_OK){
if(evt.clazz != DFEC_INPUT){//判断是否是输入事件
break;
}
if(evt.input.type == DIET_KEYPRESS){//响应键盘事件
switch (evt.input.key_symbol){
case DIKI_ENTER:
printf( "ENTER %s(%d); /n", __FILE__, __LINE__);
break;
case DIKS_ESCAPE:
case DIKS_POWER:
case DIKS_BACK:
case DIKS_SMALL_Q:
case DIKS_CAPITAL_Q:
return -1;
break;
case DIKS_CURSOR_LEFT:
case DIKI_LEFT:
pic_x--;
printf( "LEFT %s(%d); /n", __FILE__, __LINE__);
break;
case DIKS_CURSOR_RIGHT:
case DIKI_RIGHT:
pic_x++;
printf( "RIGHT %s(%d); /n", __FILE__, __LINE__);
break;
case DIKS_CURSOR_UP:
case DIKI_UP:
pic_y--;
printf( "UP %s(%d); /n", __FILE__, __LINE__);
break;
case DIKS_CURSOR_DOWN:
case DIKI_DOWN:
pic_y++;
printf( "DOWN %s(%d); /n", __FILE__, __LINE__);
break;
default:
break;
}
}//if(evt.input.type == DIET_KEYPRESS)//响应键盘事件
if(evt.input.type == DIET_AXISMOTION){//响应鼠标事件
if(evt.input.flags & DIEF_AXISREL){
switch(evt.input.axis){
case DIAI_X:
pic_x += evt.input.axisrel;
break;
case DIAI_Y:
pic_y += evt.input.axisrel;
break;
default :
break;
}//switch(evt.input.axis){
if(pic_x < 0){pic_x = 0;}
if(pic_y < 0){pic_y = 0;}
}
}//if DIET_AXISMOTION){//响应鼠标事件
}//while(!quit && events->GetEvent(events, &evt) == DFB_OK)
return 0;
}
//int actionMonitor(void )//事件监听只响应键盘
//{
// DFBInputEvent event;
//
// while (events->GetEvent( events, DFB_EVENT(&event) ) == DFB_OK){
// if (event.type == DIET_KEYPRESS){
// switch (event.key_symbol){
// case DIKI_ENTER:
// printf( "ENTER %s(%d); /n", __FILE__, __LINE__);
// break;
// case DIKS_ESCAPE:
// case DIKS_POWER:
// case DIKS_BACK:
// case DIKS_SMALL_Q:
// case DIKS_CAPITAL_Q:
// return -1;
// break;
//
// case DIKS_CURSOR_LEFT:
// case DIKI_LEFT:
// pic_x--;
// printf( "LEFT %s(%d); /n", __FILE__, __LINE__);
// break;
// case DIKS_CURSOR_RIGHT:
// case DIKI_RIGHT:
// pic_x++;
// printf( "RIGHT %s(%d); /n", __FILE__, __LINE__);
// break;
// case DIKS_CURSOR_UP:
// case DIKI_UP:
// pic_y--;
// printf( "UP %s(%d); /n", __FILE__, __LINE__);
// break;
// case DIKS_CURSOR_DOWN:
// case DIKI_DOWN:
// pic_y++;
// printf( "DOWN %s(%d); /n", __FILE__, __LINE__);
// break;
// default:
// break;
// }
// }//if (event.type == DIET_KEYPRESS){
// }
//
// return 0;
//}
int main(int argc, char **argv)
{
int ret = 0;
DirectFBInit(&argc, &argv);//程序初始化语句
DirectFBCreate(&dfb);
dfb->SetCooperativeLevel(dfb, DFSCL_FULLSCREEN);
dsc.flags = DSDESC_CAPS;
dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
dfb->CreateSurface(dfb, &dsc, &primary);
primary->GetSize(primary, &screen_width, &screen_height);
//利用指定的图片来创建一个image provider
dfb->CreateImageProvider(dfb, "./my.jpg", &provider);
//获取一个DFBSurfaceDescription,此描述与image provider中的图像相匹配
provider->GetSurfaceDescription(provider, &dsc);
//利用平面描述来创建一个平面,在这里,这个平面即与图像相匹配
dfb->CreateSurface(dfb, &dsc, &logo);
//将图像呈递给logo,并自动进行缩放,要注意:如果图像带有alpha通道,则目的层(logo)的像素格式为ARGB,
//否则图像文件的透明部分将会和黑色背景混合,这里DFBRectangle参数用的是NULL,则将自动进行缩放
provider->RenderTo(provider, logo, NULL);
//到此,provider已经没用,这里将其释放
provider->Release(provider);
ret = dfb->CreateInputEventBuffer( dfb, DICAPS_ALL, DFB_TRUE, &events );
//ret = dfb->CreateInputEventBuffer( dfb, DICAPS_KEYS, DFB_FALSE, &events );
if (ret)
{
DirectFBError( "IDirectFB::CreateEventBuffer() failed", ret );
return -1;
}
//for (i = -dsc.width; i < screen_width; i++)
myBitBlit(pic_x, pic_y);
while (1){
ret = actionMonitor();
if(ret == -1){
break;
}
myBitBlit(3*pic_x, 3*pic_y);
}
exit_app();
return 0;
}