[PSP教程02]中文显示freetype



掌叔
2008-06-05 16:57:20

摘自:TOPOC studio
作者:newcreat
采用nge显示中文非常方便,可以采用美观的freetype,可以勾画轮廓阴影,效果可以媲美商业的游戏.以下是本节的效果截图:
[attach]12[/attach]
纯粹剧情需要...
[attach]13[/attach]
代码说明:用的是第一课的代码,增加的部分见说明

// bigproject.cpp : Defines the entry point for the console application.
//
#include "libnge.h"
#include "text_auto_line.h"

SDL_Joystick* joystick;
SDL_Event event;
IVideoDriver* sys ;

CTexture* bg; //游戏背景图片
CTexture* msgbox; //对话框
CTexture* character;//人物资源
int done = 0; //程序是否结束
float scale = 1.0; //放大缩小比率1.0就是原大小,2.0就是2倍 0.5 就是缩小一倍
float angel = 0.0; //旋转角度
CTextAutoLine textout("simkai.ttf");//简单的文字处理类,注意要有ttf字库可以在windows的fonts下获得

//第一课的代码,没有动他的
void ButtonPressedProc(SDL_JoyButtonEvent * jbutton)
{
switch(jbutton -> button)
{
case 8:
angel=90;
break;
case 6:
angel =180;
break;
case 7:
angel = 270;
break;
case 9:
angel = 360;
break;
case 0:
scale-=0.05;
if(scale<=0)
scale = 0.05;
break;
case 1:
scale+=0.05;
break;
case 2:
angel-=1.0;
break;
case 3:
angel+=1.0;
break;
case 11:
break;

}

}

void KeyPressedProc(SDL_keysym * keysym)
{

switch(keysym -> sym)
{
case SDLK_UP:
angel=90;
break;
case SDLK_DOWN:
angel =180;
break;
case SDLK_LEFT:
angel =270;
break;
case SDLK_RIGHT:
angel =360;
break;
case SDLK_w:
scale-=0.05;
if(scale<=0)
scale = 0.05;
break;
case SDLK_d:
scale+=0.05;
break;
case SDLK_s:
angel-=1.0;
break;
case SDLK_a:
angel+=1.0;
break;
case SDLK_m:
break;
}
}

void InputProc()
{
while( SDL_PollEvent( &event ) )
{
switch(event.type)
{
case SDL_QUIT:
done = 1;
break;
case SDL_KEYDOWN:
KeyPressedProc(&event.key.keysym);
break;
case SDL_JOYBUTTONDOWN:
ButtonPressedProc(&event.jbutton);
break;
}
}
}


int DrawScene()
{
sys->BeginScene();
sys->RenderQuad(bg,0,0,0,0,0,0,scale,scale,angel,0xffffffff);

 //显示人物,默认摄象机的的范围是(0,0)->(480,272)
//所以可以调整坐标位置使他位于合适的位置

sys->RenderQuad(character,0,0,0,0,120,-20,0.8,0.8,0,0xffffffff);

sys->RenderQuad(msgbox,0,0,0,0,0,160,1,1,0,0xafffffff);//画一个70%透明的对话框
for(int i=0;i sys->RenderQuad(textout.m_texs[i],0,0, 0,0,40+1.5,180+i*25+1.5,1,1,0,0x9f000000);//字阴影
sys->RenderQuad(textout.m_texs[i],0,0, 0,0,40,180+i*25,1,1,0,0xffffffff);//字

}
sys->EndScene();
return( TRUE );
}

extern "C"
int main(int argc, char* argv[])
{
NGE_InitSDL(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK);

if(SDL_NumJoysticks()) {
joystick = SDL_JoystickOpen(0);
}
if ( TTF_Init() < 0 ) {
return 0;
}

sys = NGE_GetVideoDriver();
bg = LoadTexture("gamebg.png");
msgbox = LoadTexture("msgbox.png");
character = LoadTexture("char.png");
textout.LoadString(" 教程第二课,简单的对话显示。动态显示人物的对话,是每个游戏的基本元素。最后我要说的是:请爱护萝莉。");//加载要说的话,也可以从文件里读取.
textout.CreateTextLine(19,21,TTF_STYLE_NORMAL);

sys->FpsInit();
while ( !done )
{
InputProc();
DrawScene();
sys->ShowFps();
}
FreeTexture(bg);
FreeTexture(msgbox);
FreeTexture(character);
TTF_Quit();
NGE_QuitSDL();

return 0;
}

本节代码下载:lesson2,不包含字体文件,请将字体文件加入[url]http://topoc.googlecode.com/files/lesson2.rar[/url]

注意main函数上边掉了下面语句 

#pragma comment (lib,"SDL_ttf.lib")

不加会出现text_auto_line.obj : error LNK2001: unresolved external symbol _TTF_CloseFont...

如果vc下编译时还有linkerror请看[教程00],解决办法写在那个里面了.


冰蓝雪山
2009-03-17 18:23:48

中文任意显示,哦耶,太好啦!


字的颜色,是什么语句呢?

[[i] 本帖最后由 冰蓝雪山 于 2009-3-17 18:24 编辑 [/i]]


newcreat
2009-03-19 21:14:08

这个是上一代的。。让它沉了吧。。。