分类: LINUX
2006-05-26 10:33:15
#include
#include
#include "SDL.h"
#include "font.h"
unsigned char *eFONT;
unsigned char *cFONT;
unsigned char *wbTAB;
/* ----------------------------- */
void font_load(char *font,char *filename,unsigned int size)
{
FILE *fp;
if((fp=fopen(filename,"r"))==NULL){
printf("Can't open file: %s !\n",filename);
exit(1);
}
fread(font,sizeof(char),size,fp);
fclose(fp);
}
/* ----------------------------- */
void font_init(void)
{
eFONT=(char *)malloc(4096);
cFONT=(char *)malloc(267616);
wbTAB=(char *)malloc(65316);
if(eFONT==NULL || cFONT==NULL || wbTAB==NULL){
printf("Can't not malloc memory!\n");exit(1);
}
font_load(eFONT,"./font/asc16",4096);
font_load(cFONT,"./font/hzk16",267616);
font_load(wbTAB,"./cin/wubi.tab",65316);
}
/* ------------------------------ */
void font_free(void)
{
if(eFONT!=NULL)free(eFONT);
if(cFONT!=NULL)free(cFONT);
if(wbTAB!=NULL)free(wbTAB);
}
/* ------------------------------ */
void putpixel(SDL_Surface *surface,int x,int y,Uint32 color)
{
Uint16 *bufp;
if(SDL_MUSTLOCK(surface)){
if(SDL_LockSurface(surface)<0)return;
}
bufp=(Uint16 *)surface->pixels+y*surface->pitch/2+x;
*bufp=color;
SDL_UpdateRect(surface,x,y,1,1);
if(SDL_MUSTLOCK(surface)){
SDL_UnlockSurface(surface);
}
}
/* ------------------------------ */
void cputchar(SDL_Surface *surface,int x,int y,Uint32 color,char *code)
{
int i,j,k;
unsigned char mask=0x80;
unsigned char q,w;
char *ptr;
q=code[0]-0xa0;
w=code[1]-0xa0;
ptr=cFONT+((q-1)*94+(w-1))*32;
for(i=0;i<16;i++){
for(j=0;j<2;j++){
for(k=0;k<8;k++){
if(ptr[i*2+j]&(mask>>k))putpixel(surface,x+j*8+k,y+i,color);
}
}
}
}
/* ------------------------------ */
void cputchar2x(SDL_Surface *surface,int x,int y,Uint32 color,char *code)
{
int i,j,k;
int xx,yy;
unsigned char mask=0x80;
unsigned char q,w;
char *ptr;
q=code[0]-0xa0;
w=code[1]-0xa0;
ptr=cFONT+((q-1)*94+(w-1))*32;
for(i=0;i<16;i++){
for(j=0;j<2;j++){
for(k=0;k<8;k++){
if(!(ptr[i*2+j]&(mask>>k)))continue;
xx=(x+j*8+k)*2;yy=(y+i)*2;
putpixel(surface,xx,yy,color);putpixel(surface,xx+1,yy,color);
putpixel(surface,xx,yy+1,color);putpixel(surface,xx+1,yy+1,color);
}
}
}
}
/* ------------------------------ */
void eputchar(SDL_Surface *surface,int x,int y,Uint32 color,char code)
{
int i,j;
unsigned char mask=0x80;
char *ptr;
ptr=eFONT+code*16;
for(i=0;i<16;i++){
for(j=0;j<8;j++){
if(ptr[i]&(mask>>j))putpixel(surface,x+j,y+i,color);
}
}
}
/* ------------------------------ */
void eputchar2x(SDL_Surface *surface,int x,int y,Uint32 color,char code)
{
int i,j;
int xx,yy;
unsigned char mask=0x80;
char *ptr;
ptr=eFONT+code*16;
for(i=0;i<16;i++){
for(j=0;j<8;j++){
if(ptr[i]&(mask>>j)){
xx=(x+j)*2;yy=(y+i)*2;
putpixel(surface,xx,yy,color);putpixel(surface,xx+1,yy,color);
putpixel(surface,xx,yy+1,color);putpixel(surface,xx+1,yy+1,color);
}
}
}
}
/* ------------------------------ */
void ecprint(SDL_Surface *surface,int x,int y,Uint32 color,char *str)
{
char *ptr;
char code[2];
unsigned char l;
unsigned char mask=0x80;
ptr=str;
l=strlen(str);
while(l>0){
if(ptr[0]&mask){
code[0]=ptr[0];
code[1]=ptr[1];
cputchar(surface,x,y,color,code);
x+=16;
ptr+=2;
l-=2;
}
else {
code[0]=ptr[0];
eputchar(surface,x,y,color,code[0]);
x+=8;
ptr++;
l--;
}
}
}
/* ------------------------------ */
void ecprint2x(SDL_Surface *surface,int x,int y,Uint32 color,char *str)
{
char *ptr;
char code[2];
unsigned char l;
unsigned char mask=0x80;
ptr=str;
l=strlen(str);
while(l>0){
if(ptr[0]&mask){
code[0]=ptr[0];
code[1]=ptr[1];
cputchar2x(surface,x,y,color,code);
x+=16;
ptr+=2;
l-=2;
}
else {
code[0]=ptr[0];
eputchar2x(surface,x,y,color,code[0]);
x+=8;
ptr++;
l--;
}
}
}
/* ------------------------------ */