DirectFB简单示例

 

在上一篇文章中启动好了framebuffer,现在可以用这样一个简单的例子来检验下效果。

来自DirectFB官方网站的简单程序,在屏幕上画一条线。

 
编译后,执行 成功了。

 

Simple fullscreen application that draws a horizontal line.

代码如下:

#include <stdio.h>

#include <unistd.h>

#include <directfb.h>

 

static IDirectFB *dfb = NULL;

static IDirectFBSurface *primary = 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 );                          /

}                                                               /

}

 

int main(int argc, char **argv)

{

DFBSurfaceDescription dsc;

 

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));

DFBCHECK (primary->FillRectangle (primary, 0, 0, screen_width, screen_height));

 

DFBCHECK (primary->SetColor (primary, 0x80, 0x80, 0xff, 0xff));

DFBCHECK (primary->DrawLine (primary,

0, screen_height / 2,

screen_width - 1, screen_height / 2));

DFBCHECK (primary->Flip (primary, NULL, 0));

sleep(5);

 

primary->Release(primary);

dfb->Release(dfb);

return 23;

}

下面我们来看这个程序,前三行:

#include <stdio.h>

#include <unistd.h>

#include <directfb.h>

为头文件包含。第5到第8行:

static IDirectFB *dfb = NULL;

static IDirectFBSurface *primary = NULL;

static int screen_width = 0;

static int screen_height = 0;

为全局变量定义。第10到第19行为出错处理宏:

#define DFBCHECK(x...)                                                  /

{                                                                       /

DFBResult err = x;                                              /

/

if (err != DFB_OK)                                              /

{                                                               /

fprintf( stderr, "%s <%d>:/n/t", __FILE__, __LINE__ );  /

DirectFBErrorFatal( #x, err );                          /

}                                                               /

}

此宏定义在官方给的程序中很常见,确实也挺好用。第21到46行为主程序代码段。

 

其中,第23行:

DFBSurfaceDescription dsc;

为局部变量定义,dsc为平面属性描述变量,用以创建与其相匹配的平面。

 

第25到26行:

DFBCHECK (DirectFBInit (&argc, &argv));

DFBCHECK (DirectFBCreate (&dfb));

为DirectFB初始化函数,第25行用以初始化命令行参数,而第26行用以创建DirectFB的主接口。

 

第28到30行对平面属性进行了初始化,其中第28行:

DFBCHECK (dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN));

设置了DirectFB的主接口的同步等级,DFSCL_FULLSCREEN与DFSCL_EXCLUSIVE相同,均为非共享的同步等级,在总接口使用CreateSurface方法创建平面时,将创建全屏平面。

 

第29到30行:

dsc.flags = DSDESC_CAPS;

dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;

说明了即将创建的平面的属性,在本例中,我们要创建一个可flip的主平面。

 

第32到34行:

DFBCHECK (dfb->CreateSurface( dfb, &dsc, &primary ));

DFBCHECK (primary->GetSize (primary, &screen_width, &screen_height));

DFBCHECK (primary->FillRectangle (primary, 0, 0, screen_width, screen_height));

创建了一个平面,并对此平面作了相应的初始化操作。第33行获取了此平面(全屏主平面)的宽高值,第34行在平面上画了一个黑色(DirectFB系统默认颜色)的屏幕大小矩形,此函数是受stb225平台硬件加速支持的。

 

第36到41行:

DFBCHECK (primary->SetColor (primary, 0x80, 0x80, 0xff, 0xff));

DFBCHECK (primary->DrawLine (primary,

0, screen_height / 2,

screen_width - 1, screen_height / 2));

DFBCHECK (primary->Flip (primary, NULL, 0));

sleep(5);

的功能为在屏幕正中心画一条横线,并显示5秒钟。第36行设置了线的颜色,第37到39行进行画线操作,将36行所设置颜色的线画到primary平面上,第40行将画有横线的平面显示出来,第41行,程序休眼5秒,即此线在屏幕上显示5秒。

 

第43到45行:

primary->Release(primary);

dfb->Release(dfb);

return 23;

程序作了相应的清理操作,而后退出。

 

DirectFB程序中,如果定义了接口,则一定需要使用相应的Release方法将此接口进行释放,否则可能引起内存泄漏问题,甚至可能破坏程序的稳定性。

你可能感兴趣的:(File,null,application,平台)