DirectFB学习笔记二

时间:2023-03-09 23:23:11
DirectFB学习笔记二

本篇目的,画一个方框,在方框上画一串字符。

实现步骤:首先创建IDirectFB接口,通过它再创建要显示的表面surface,同时创建字体font,绘制字符必须要设置绘制的字体,否则绘制不成功。然后清理窗口,将整个surface填充为黑色,最后绘制方框和字符。

#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;//创建surface时需要的描述符
DFBFontDescription font_dsc;//创建字体font时需要的描述符
IDirectFBFont *font = NULL;//字体

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));创建surface
DFBCHECK(primary->GetSize(primary,&screen_width,&screen_height));//获取屏幕的高宽

DFBCHECK(primary->SetColor(primary,0x0,0x0,0x0,0xff));//设置画笔的颜色,每次绘制前都需要设置,否则将采用默认的或者上次设置的颜色
DFBCHECK(primary->FillRectangle(primary,0,0,screen_width,screen_height));//填充整个surface平面

DFBCHECK(primary->SetColor(primary,0x80,0x80,0xff,0xff));//设置画笔颜色
DFBCHECK(primary->DrawRectangle(primary,0,0,screen_width/3,
screen_height/3));//画一个坐上角在(0,0),宽为屏幕宽的1/3,高为屏幕高的1/3
font_dsc.flags = DFDESC_HEIGHT;
font_dsc.height = 50;
DFBCHECK(dfb->CreateFont(dfb,"./decker.ttf",&font_dsc,&font));//创建字体
DFBCHECK(primary->SetFont(primary,font));//设置字体,和画笔一样,每次绘制字符前都要设置

DFBCHECK(primary->DrawString(primary,"hello world",-1,screen_width/6,
screen_height/6,DSTF_CENTER));//绘制中心为(screen_width/6,screen_height/6)的字符串
DFBCHECK(primary->Flip(primary,NULL,DSFLIP_NONE));将缓冲区的图画(即上面填充的以及绘制的方框和字符串)显示出来
sleep(5);//挂起5秒,否则会秒退,看不到效果

font->Release(font);
primary->Release(primary);
dfb->Release(dfb);
return 0;
}