⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 15-7.c

📁 《linux应用开发技术详解》的配套代码
💻 C
字号:
#inlcude <stdio.h>
#include <stdlib.h>
#include <SDL.h>

void Init_SDL();
void eputchar(SDL_Surface *surface,int x,int y,Uint32 color,char code);
void cputchar(SDL_Surface *surface,int x,int y,Uint32 color,char *code);
void ecprint(SDL_Surface *surface,int x,int y,Uint32 color,char *str);

int main()
{
    SDL_Surface *screen;
    Uint32 color;
    unsigned char *eFONT;         /* 定义字符指针,将用于保存字库首址 */
    unsigned char *cFONT;
    FILE *fp1,*fp2;

    eFONT=(char *)malloc(4096);    /* 分配内存 */
    cFONT=(char *)malloc(267616);
    if(eFONT==NULL || cFONT==NULL==NULL){
        printf("Can't not malloc memory!\n");exit(1);
    }
    fp1 = fopen("asc16","r");        /* 打开字库 */
    fp2 = fopen("hzk16","r");
    if ( fp1==NULL || fp2 == NULL){
        printf("Can't open file: %s !\n","asc16 or hzk16");
        exit(1);
    }
    fread(eFont,sizeof(char),4096,fp1); /* 读入内存 */
    fread(cFont, sizeof(char),267616,fp2);
    fclose(fp1);                   /* 关闭文件 */
    fclose(fp2);

    Init_SDL();                   /* 初始化SDL */

    color = SDL_MapRGB(screen->format,0,0,255);
    SDL_FillRect(screen,&screen->clip_rect,color);        /* 将整个屏幕显示成蓝色 */
    color = SDL_MapRGB(screen->format,255,255,255);
    ecprint(screen,200,20,color, "Demo: 点阵汉字显示");   /* 在屏幕上方显示白色的中英文字符串 */
    SDL_UpdateRect(screen,0,0,0,0);                    /* 更新整个屏幕 */
    SDL_Delay(5000);                                /* 延迟 */
}

void Init_SDL()
{
  if ( SDL_Init( SDL_INIT_VIDEO) < 0 ) {
    fprintf(stderr,"Can't init SDL: %s\n",SDL_GetError());
    exit(1);
  }
  screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
  if ( screen == NULL ) {
    fprintf(stderr, "Error: %s\n",SDL_GetError());
    exit(1);
  }
  atexit(SDL_Quit);
}

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);           /* 该位为1则描点 */
    }
  }
}

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;               /* 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);  /* 该位为1则描点 */
      }
    }
  }
}

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){      /* 如果是汉字则调用cputchar()来显示 */
      code[0]=ptr[0];
      code[1]=ptr[1];
      cputchar(surface,x,y,color,code);
      x+=16;
      ptr+=2;
      l-=2;
    }
    else {               /* 英文则调用eputchar()来显示 */
      code[0]=ptr[0];
      eputchar(surface,x,y,color,code[0]);
      x+=8;
      ptr++;
      l--;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -