DirectFB 之 字体显示(2)
框架
示例代码
/**********************************************
* Author: younger.liucn@hotmail.com
* File name: dfbFont.c
* Description: dfbFont
* Version, Desc
* 1.1 Created
* 1.2 add config
**********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <directfb.h>
#include "dfbFont.h"
/* config information */
/*********************************************************
* log flags and func of debug and error infor.[Begin]
********************************************************/
//#define DFB_SYS_LAYER
//#define _DFB_DEBUG_
#define DFB_FONT_TYPE_1 "/home/liuyiy/DFB/Font/data/decker.ttf"
#define WINDOW_TEXT_LEN_X_PER (0.5)
#define WINDOW_TEXT_LEN_Y_PER (0.1)
#define WINDOW_TEXT_OFFSET_X_PER (0.25)
#define WINDOW_TEXT_OFFSET_Y_PER (0.75)
#ifdef _DFB_DEBUG_
#define DFB_DEBUG(format, ...) do { \
printf("[BootAnimation:%4dL]DEBUG: "format, __LINE__, ##__VA_ARGS__); \
} while (0)
#else
#define DFB_DEBUG(format, ...) do {} while (0)
#endif
#define DFB_NOTICE(format, ...) do { \
printf("[BootAnimation:%4dL]INFO: "format, __LINE__, ##__VA_ARGS__); \
} while (0)
#define DFB_ERROR(format, ...) do { \
printf("[BootAnimation:%4dL]ERROR: "format, __LINE__, ##__VA_ARGS__); \
} while (0)
/*********************************************************
* log flags and func of debug and error infor.[End]
********************************************************/
/*********************************************************
* Data structure and Global variants [Begin]
********************************************************/
/* 使用directFB画图所需的四个DFB资源 */
struct AnimationDsc {
IDirectFB *dfb;
IDirectFBDisplayLayer *layer;
IDirectFBWindow *window;
IDirectFBSurface *surface;
IDirectFBFont *font;
};
static struct AnimationDsc badsc;
static char *testString = "Hello world! This is DirectFB!";
/*********************************************************
* Data structure and Global variants [End]
********************************************************/
/*********************************************************
* Some functions help animation [Begin]
********************************************************/
/*********************************************************
* Some functions help animation [End]
********************************************************/
void freeResources()
{
/* Release the window's surface. */
if(badsc.surface)
badsc.surface->Release(badsc.surface);
/* Release the window. */
if (badsc.window)
badsc.window->Release(badsc.window);
/* Release the layer. */
if (badsc.layer)
badsc.layer->Release(badsc.layer);
badsc.dfb->Release(badsc.dfb);
return ;
}
static void initResources(int argc, char **argv)
{
DFBResult ret;
badsc.window = NULL;
badsc.surface = NULL;
badsc.dfb = NULL;
IDirectFB *dfb = NULL;
DFBWindowDescription desc;
DFBDisplayLayerConfig config;
/* 初始化DirectFB */
DirectFBInit(&argc, &argv);
DirectFBCreate(&dfb);
if (!dfb) {
DFB_ERROR("directfb interface is NULL\n");
return ;
}
badsc.dfb = dfb;
/* 初始化 display layer:其中DFB_LAYERID_USING设置为0.*/
ret = badsc.dfb->GetDisplayLayer(badsc.dfb, DFB_LAYERID_USING, &(badsc.layer));
if(ret != (DFBResult)DFB_OK) {
DFB_ERROR("Get layer(%d) failed!\n", DFB_LAYERID_USING);
goto fail;
} else {
DFB_DEBUG("Get layer(%d) independently.\n", DFB_LAYERID_USING);
}
/* 获取display layer的配置,. */
badsc.layer->GetConfiguration(badsc.layer, &config);
/* 设置window参数,并创建Window */
desc.flags = (DFBWindowDescriptionFlags)(DWDESC_POSX | DWDESC_POSY | DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_CAPS | DWDESC_OPTIONS);
DFB_NOTICE("Layer Screen width %d, height %d !\n", config.width, config.height);
desc.posx = WINDOW_TEXT_OFFSET_X_PER * config.width;
desc.posy = WINDOW_TEXT_OFFSET_Y_PER * config.height;
desc.width = WINDOW_TEXT_LEN_X_PER * config.width;
desc.height = WINDOW_TEXT_LEN_Y_PER * config.height;
desc.caps = (DFBWindowCapabilities)(DWCAPS_NODECORATION);
desc.options = (DFBWindowOptions) (DWOP_GHOST);
ret = badsc.layer->CreateWindow(badsc.layer, &desc, &(badsc.window));
if(ret != (DFBResult)DFB_OK) {
DFB_ERROR("Create window failed!\n");
goto fail;
}
/* 设置透明度 */
ret = badsc.window->SetOpacity(badsc.window, 0xFF);
if(ret != (DFBResult)DFB_OK) {
DFB_ERROR("SetOpacity failed!\n");
goto fail;
}
/* 获取window的surface. */
ret = badsc.window->GetSurface(badsc.window, &(badsc.surface));
if(ret != (DFBResult)DFB_OK) {
DFB_ERROR("SetOpacity failed!\n");
goto fail;
}
return ;
fail:
freeResources();
return ;
}
/* font init */
static initFont()
{
DFBFontDescription font_desc;
font_desc.flags = DFDESC_HEIGHT;
font_desc.height = 30;
badsc.dfb->CreateFont(badsc.dfb, DFB_FONT_TYPE_1, &font_desc, &badsc.font);
return ;
}
static void flipText(IDirectFBFont* font, IDirectFBSurface* surface, char* str) {
int w = 0, h = 0;
surface->GetSize(surface, &w, &h);
surface->Clear(surface, 0x0, 0x0, 0x0, 0xff);
surface->SetFont(surface, font);
/* 设置前景颜色 */
surface->SetColor(surface, 0x00, 0xFF, 0x00, 0xFF);
surface->DrawString(surface, str, strlen(str), w / 2, h / 2, DSTF_CENTER);
/* 变换、更新surface buffer */
surface->Flip(surface, NULL, DSFLIP_WAITFORSYNC);
return ;
}
static void doMovie()
{
int ret = 0, i = 0;
int width = 0, height = 0;
struct timespec before_draw, after_draw;
unsigned long elapsed_msec = 0, total_msec = 0;
IDirectFBSurface *primary = badsc.surface;
IDirectFBSurface *bg_sfc = NULL;
DFB_NOTICE("Animation start ...\n");
flipText(badsc.font, primary, testString);
usleep(2*1000*1000);
out:
primary->SetColor(primary, 0, 0, 0, 0);
primary->Clear(primary, 0, 0, 0, 0);
primary->Flip(primary, NULL, DSFLIP_WAITFORSYNC);
DFB_NOTICE("Animation exit with black screen...\n");
return ;
}
int main(int argc, char **argv)
{
DFB_NOTICE("Animation entry.\n");
initResources(argc, argv);
initFont();
doMovie();
freeResources();
DFB_NOTICE("Animation exit.\n");
return 0;
}
作者:Younger Liu,
本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 未本地化版本许可协议进行许可。
posted on 2016-05-24 21:57 YoungerChina 阅读(588) 评论(0) 收藏 举报
浙公网安备 33010602011771号