dayu的Blog
dayuhome · 2008-01-04
<< Dev c++ & Sdl 同步学2
|
首页
|
去雷锋下书吧 >>
2008-01-04
Dev c++ & Sdl 同步学3 显示汉字
- [
c&c++&sdl
]
版权声明
:转载时请以超链接形式标明文章原始出处和作者信息及
本声明
http://dayuhome.blogbus.com/logs/13305666.html
显示汉字
虽然说显示英文的
ttf
字体比较容易。可以用官方的库(在某大大的
sdl
系列教程中有叙述,在下就不多说了)。但是,说真的,显示中文比较困难。
最后我想到了使用了最古老的
HZK
的字模信息,使用绘点函数才解决问题。
下面
DrawPixel
是一个标准的在表面上绘制点的函数,类似
flash
的
setpixel
();当然效率比
flash
要高
N
多了
void DrawPixel(SDL_Surface *screen, int x,
int y,
Uint8 R, Uint8 G, Uint8
B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel)
{
case 1: // Assuming 8-bpp
{
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*bufp = color;
}
break;
case 2: // Probably 15-bpp or 16-bpp
{
Uint16 *bufp;
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
*bufp = color;
}
break;
case 3: // Slow 24-bpp mode, usually not used
{
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
break;
}
}
因为部分显卡在绘制表面的时候需要锁定,所以就有了下面的
2
个函数了。
void Slock(SDL_Surface *screen)
{
if
( SDL_MUSTLOCK(screen) )
{
if ( SDL_LockSurface(screen) < 0 )
{
return;
}
}
}
void Sulock(SDL_Surface *screen)
{
if
( SDL_MUSTLOCK(screen) )
{
SDL_UnlockSurface(screen);
}
}
至于绘制汉字被我封装到一个函数里面了,只需要传递一个字符串就可以了
int drawtxt(const Uint8 str[]){
//unsigned char *str=st;
int sx=0;
int sy=0;
Uint8 mat[16][2];
// return 1;
FILE *HZK;
if((HZK=fopen("HZK16","rb"))==NULL){
printf("file???");
return 1;
}
int count=0;
while(str[count]!=0){
if(str[count]>0xa0){
Uint8 i=str[count]-0xa0;
Uint8 j=str[count+1]-0xa0;//
获得区码与位码
fseek(HZK,(94*(i-1)+(j-1))*32l,SEEK_SET);
fread(mat,32,1,HZK);
drawhz(mat,sx,sy);
count+=2;
sx+=16;
continue;
}else{
//
这里是绘制英文的代码;作为练习
Count++;
}
//
关闭汉字库的文件
fclose(HZK);
}
//
这个是绘制单个汉字的函数。
void drawhz(Uint8 mat[][2],int sx,int sy){
Slock(screen);
for(int j=0;j<16;j++)
for(int i=0;i<2;i++)
for(int k=0;k<8;k++)
if(mat[j][i]&(0x80>>k))//
测试为
1
的位则显示
DrawPixel(screen,sx+i*8+k,sy+j+1,255,255,255);
Sulock(screen);
}
具体测试,需要有汉字库才行。
随机文章:
Dev c++ & Sdl 同步学2
2008-01-03
Dev c++ & Sdl同步学1 参考部分翻译
2008-01-02
Dev c++ & Sdl同步学 前言
2008-01-02
c还是c++
2007-12-29
用eclipse+cdt+mingw写c&c++
2007-12-29
收藏到:
Del.icio.us
Tag:
sdl
引用地址:
zuwuneng
发表于
21:45:12
|
编辑
|
继续话题
|
转发
|
推荐
发表评论
原始快照:web.archive.org