掌叔
2009-03-26 08:49:47
摘自:[url]www.iacger.com[/url]
作者:newcreat
AVG和RPG都有动态显示文字,动态显示文字就是慢慢的显示字的方式,让你一下看不全,叼着你胃口。。。 还不知道啥叫动态文字的看下图。。。。
[attach]1039[/attach]
下面本节就来说如何来动态显示文字:
文字层:包含全部文字的图层
遮罩层:相当于刷子,刷去后面的文字
显示层:最终的显示
方法:本文选取柔和的方式既用mask的方式
首先拷贝全部文字层到显示层,再用遮罩层按时间抹去
[code=c]#include "libnge2.h"
/**
* nge_test:测试nge2的动态显示字体
* 所需资源文件:拷贝到相应目录下
* fonts/GBK14,fonts/ASC14,fonts/simfang.ttf,images/demo1_bg.png,images/demo1_box.jpg
* images/demo1_icon.jpg
*/
int game_quit = 0;
image_p pimage_text,pimage_bg,pimage_box,pimage_mask,pimage_display = NULL;
image_p pimage_icon[2];
//半透明的box
int maskbox = MAKE_RGBA_8888(255,255,255,128);
//字体遮罩移动宽度
int xoffset= 0;
int lasttick = 0;
void btn_down(int keycode)
{
switch(keycode)
{
case PSP_BUTTON_UP:
break;
case PSP_BUTTON_DOWN:
break;
case PSP_BUTTON_LEFT:
break;
case PSP_BUTTON_RIGHT:
break;
case PSP_BUTTON_TRIANGLE:
break;
case PSP_BUTTON_CIRCLE:
break;
case PSP_BUTTON_CROSS:
break;
case PSP_BUTTON_SQUARE:
break;
case PSP_BUTTON_SELECT:
game_quit = 1;
break;
case PSP_BUTTON_START:
game_quit = 1;
break;
}
}
void btn_up(int keycode)
{
switch(keycode)
{
case PSP_BUTTON_UP:
break;
case PSP_BUTTON_DOWN:
break;
case PSP_BUTTON_LEFT:
break;
case PSP_BUTTON_RIGHT:
break;
case PSP_BUTTON_TRIANGLE:
break;
case PSP_BUTTON_CIRCLE:
break;
case PSP_BUTTON_CROSS:
break;
case PSP_BUTTON_SQUARE:
break;
case PSP_BUTTON_SELECT:
game_quit = 1;
break;
case PSP_BUTTON_START:
game_quit = 1;
break;
}
}
//乱写的句子...
char str[6][128]={"从前有个人,他没有事干,就找事干,最后他死了!",
"死了之后没人知道。结果尸体腐烂了,好可怕,T_T",
"就在这个时候来了另外一个人,这个人是来打丧尸的。",
"但是腐烂了不代表是丧尸啊,兄弟!哪里会有丧尸。。",
"丧尸是游戏里面才有的骗小孩的玩意儿,不要相信。",
"哎!传说中的故事就这样结束了。。。谢谢观赏。"
};
extern "C"
int main(int argc, char* argv[])
{
PFont pf[2] ;
int i = 0;
NGE_Init(INIT_VIDEO);
InitInput(btn_down,btn_up,1);
//创建一个字体image,字就打印在这个上面注意DISPLAY_PIXEL_FORMAT必须与创建字体的DISPLAY_PIXEL_FORMAT一致
pimage_text = image_create(512,16,DISPLAY_PIXEL_FORMAT_4444);
//遮罩
pimage_mask = image_create(512,16,DISPLAY_PIXEL_FORMAT_4444);
//显示
pimage_display = image_create(512,16,DISPLAY_PIXEL_FORMAT_4444);
//创建字体
pf[0] = create_font_freetype("fonts/simfang.ttf",13,DISPLAY_PIXEL_FORMAT_4444);
if(pf[0]==NULL){
printf("cannot open file!
");
}
font_setattr(pf[0],-1,0);
font_setcolor(pf[0],MAKE_RGBA_4444(255,0,0,255));
font_drawtext(pf[0],str[i],strlen(str[i]),pimage_text,0,0,FONT_SHOW_NORMAL);
pimage_bg = image_load("images/demo1_bg.jpg",DISPLAY_PIXEL_FORMAT_8888,1);
if(pimage_bg==NULL)
printf("cannot open font file
");
pimage_box = image_load("images/demo1_box.jpg",DISPLAY_PIXEL_FORMAT_8888,1);
if(pimage_box==NULL)
printf("cannot open font file
");
pimage_icon[0] = image_load_colorkey("images/demo1_icon1.png",DISPLAY_PIXEL_FORMAT_8888,MAKE_RGB(0,0,0),1);
if(pimage_icon[0]==NULL)
printf("cannot open font file
");
nge_timer* timer=timer_create();
timer->start(timer);
int display_buf_size = pimage_text->texw*pimage_text->texh*pimage_text->bpb;
while ( !game_quit )
{
ShowFps();
InputProc();
BeginScene(1);
ImageToScreen(pimage_bg,0,0);
RenderQuad(pimage_box,0,0,0,0,70,185,1,1,0,maskbox);
DrawImage(pimage_icon[0],0,0,0,0,-20,143,128,128);
if(pimage_display)
ImageToScreen(pimage_display,100,225);
EndScene();
if(timer->get_ticks(timer)- lasttick>50){
lasttick = timer->get_ticks(timer);
memcpy(pimage_display->data,pimage_text->data,display_buf_size);
image_to_image_ex(pimage_mask,pimage_display,0,0,512-xoffset,16,xoffset,0);
xoffset += 8;
if(xoffset >= pimage_mask->w){
//到下一句话,一共有6句,大于6了变为从头讲
xoffset = 0;
i++;
if(i >= 6){
i = 0;
}
memset(pimage_text->data,0,pimage_text->texw*pimage_text->texh*pimage_text->bpb);
font_drawtext(pf[0],str[i],strlen(str[i]),pimage_text,0,0,FONT_SHOW_NORMAL);
}
}
LimitFps(60);
}
font_destory(pf[0]);
image_free(pimage_bg);
image_free(pimage_mask);
image_free(pimage_text);
image_free(pimage_box);
image_free(pimage_icon[0]);
image_free(pimage_display);
timer_free(timer);
NGE_Quit();
return 0;
} [/code]