newfont.c

来自「redboy for gba 是BPNS为GBA编写的一个小软件。软件的邹形是B」· C语言 代码 · 共 686 行 · 第 1/2 页

C
686
字号

void nsh_line(u8 xp,u8 line,u16 color,char *text)
{
   u8 xp=2,yp=1+line*(f_yp+f_yd);
   int tp=0;
   u8 lend=0;
   while (!lend)
   {
      if (text[tp]>127)//汉字
      {
         if (xp+f_xp>229) lend=1;
         else
         {
            new_font(xp,yp,color,text+tp);
            xp+=(f_xp+f_xd);
            tp++;
         }
      }
      else if ((text[tp]>32 && text[tp]<127) || text[tp]==' ')//可显示英文
      {
         if (xp>221) lend=1;
         else
         {
            if (text[tp]!=' ')
            {
               new_ascii(text[tp],xp,yp+((f_yp-16)>>1),color);
            }
            xp=xp+8;
         }
      }
      else if (text[tp]=='\n' || text[tp]=='\0') lend=1;
      if (xp>=223+6) lend=1;
      tp++;
   }
}
u32 etxt_predo(char *text,u32 *sav)//文本预处理
{
   u32 lines=1;
   u32 tp=0;//文本指针
   u8 xp=2;
   u8 lend=0;
   sav[0]=0;
   while (text[tp]!='\0')
   {
      if (text[tp]>127)//汉字
      {
         if (xp+f_xp>229)
         {
            sav[lines]=tp;
            lines++;
            xp=2+f_xp;
         }
         else
         {
            xp+=(f_xp+f_xd);
            if (xp>229)
            {
               sav[lines]=tp+2;
               lines++;
               xp=2;
            }
         }
         tp+=2;
      }
      else if ((text[tp]>32 && text[tp]<127) || text[tp]==' ')//可显示英文
      {
         if (xp>221)
         {
            sav[lines]=tp;
            lines++;
            xp=10;
         }
         else
         {
            xp+=8;
            if (xp>228)
            {
               sav[lines]=tp+1;
               lines++;
               xp=2;
            }
         }
         tp++;
      }
      else if (text[tp]=='\n')//自动换行
      {
         tp++;
         sav[lines]=tp;
         lines++;
         //if (lines>5115) return lines;
         xp=2;
      }
      else tp++;
   }
   //lines++;
   return lines;
}
void etxt_predo_end() {}
void show_ntxt_line(int line,char *str,u16 color,u16 bg)
{
   Q_box(2,1+(f_yp+f_yd)*line,229,(f_yp+f_yd)*(line+1),bg);
   nsh_line(2,line,color,str);
}

void ntext(char *text,u16 color,u16 bg,u32 *sav)
{
   u32 lines;
   u32 nowline=0;
   u32 lip=0;
   u8 done=1;
   u32 pages=0;
   u16 colors[2];
   int k;
   u8 i;
   u8 start=1;
   u8 csets=0;
   u8 shadow=1,autop=0;
   u8 plines=(158+f_yd)/(f_yd+f_yp);
   u32 code[((u32)etxt_predo_end-(u32)etxt_predo)/4+1];
   u32 (*fun)()=(u32)code+1;
   DmaCopy(3,etxt_predo,code,(u32)etxt_predo_end-(u32)etxt_predo,32);
   if (f_yp<16 || f_yp>64 || f_xp<12 || f_xp>64)
   {
      arlt("系统提示:","字体大小超出范围,请重新设置。");
      fnt_sets.on=0;
      font_set(1);
      return;
   }
   if (f_xd>6) f_xd=0;
   if (f_yd>6) f_yd=0;
   rb_yp=0;
   colors[0]=color;
   colors[1]=bg;
   set_text_bg(bg);
   lines=fun(text,sav);
   //lines=etxt_predo(text,sav);
   if (color==0) shadow=0;
   if (lines==0) {write(0,17,RGB(30,0,30),"文本打开失败!");return;}
   sys_deamon=text_deamon;
   pages=lines/plines;
   if (lines%plines) pages++;
   if (is_basic_run)
   {
      done=1;
      is_basic_run=0;
      nowline=_line;
   }
   else _line=0;
   while(1)
   {
      if (start) {start=0;CS_ReadKey();} else readkey();
      if(CS_IsKeyHold(KEY_R) && CS_IsKeyDown(KEY_LEFT))
      {
         csets=(csets+5)%6;
         color=text_colors[csets*2];
         bg=text_colors[csets*2+1];
         colors[0]=color;
         colors[1]=bg;
         txt_set(colors);
         done=1;
      }
      else if(CS_IsKeyHold(KEY_R) && CS_IsKeyDown(KEY_RIGHT))
      {
         csets=(csets+1)%6;
         color=text_colors[csets*2];
         bg=text_colors[csets*2+1];
         colors[0]=color;
         colors[1]=bg;
         txt_set(colors);
         done=1;
      }
      else if(CS_IsKeyHold(KEY_R) && CS_IsKeyDown(KEY_A))//保存书签
      {
         list_bmark(find_bmark(_fid),1);
         if (is_basic_run) return;
         title(_file);
         done=1;
      }
      else if(CS_IsKeyHold(KEY_R) && CS_IsKeyDown(KEY_UP))//调整自动滚屏速度
      {
         rb_xp=(rb_xp+11)%12;
      }
      else if(CS_IsKeyHold(KEY_R) && CS_IsKeyDown(KEY_DOWN))
      {
         rb_xp=(rb_xp+11)%12;
      }
      else if(CS_IsKeyHold(KEY_R) && CS_IsKeyHold(KEY_L))//帮助说明
      {
         if(!is_in_text_help)
         {
            title("文本浏览器帮助说明:");
            text_help();
            Q_box(1,140,229,158,bg);
            done=1;
         }
      }
      else if(CS_IsKeyHold(KEY_L) && CS_IsKeyDown(KEY_A))//设置字体
      {
         done=menu(font_menus,3);
         switch (done)
         {
            case 1://更改模式
            chg_fnt_mod();
            break;
            case 2://选择字体
            ebk_select_font();
            title(_file);
            break;
            case 3://字距设置
            fnt_dis();
            break;
         }
         done=1;
         Q_box(1,140,229,158,bg);
      }
      else if(CS_IsKeyHold(KEY_L) && CS_IsKeyDown(KEY_B))//文本阴影
      {
         shadow=!shadow;
         done=1;
      }
      else if((CS_IsKeyHold(KEY_L) && CS_IsKeyHold(KEY_DOWN)) ||CS_IsKeyDown(KEY_DOWN) || CS_IsKeyDown(KEY_A))
      {
         if (nowline<lines-1)
         {
            nowline=(nowline+plines<lines-1)?(nowline+plines):lines-1;
            done=1;
         }
      }
      else if(CS_IsKeyHold(KEY_R) && CS_IsKeyDown(KEY_B))//自动滚屏
      {
         autop=!autop;
      }
      else if((CS_IsKeyHold(KEY_L) && CS_IsKeyHold(KEY_LEFT)) || CS_IsKeyDown(KEY_LEFT))
      {
         if (nowline>0)
         {
            done=2;
            nowline--;
         }
      }
      else if((CS_IsKeyHold(KEY_L) && CS_IsKeyHold(KEY_RIGHT)) || CS_IsKeyDown(KEY_RIGHT)|| (rb_yp==1 && autop))
      {
         if (nowline<lines-1)
         {
            if (rb_yp==1) rb_yp=2;
            done=3;
            nowline++;
         }
      }
      else if ((CS_IsKeyHold(KEY_L) && CS_IsKeyHold(KEY_UP)) || CS_IsKeyDown(KEY_UP))
      {
         if (nowline>0)
         {
            nowline=(nowline>plines)?(nowline-plines):0;
            done=1;
         }
      }
      else if(CS_IsKeyDown(KEY_B))
      {
         rb_yp=0;
         sys_deamon=NULL;
         return;
      }
      else if(CS_IsKeyDown(KEY_START))
      {
         text_set(colors);
         color=colors[0];
         bg=colors[1];
         done=1;
      }
      else if(CS_IsKeyDown(KEY_SELECT))
      {
         nowline=quik_page(lines,nowline);
         done=1;
      }
      if (done) _line=nowline;
      if (done==1)
      {
         Q_box(1,17,229,20,bg);
         for (k=0;k<plines;k++)
         {
            if (nowline+k+1>lines) break;
            show_ntxt_line(k,text+sav[nowline+k],color, bg);
         }
         while(k<plines)
         {
            Q_box(2,1+(f_yp+f_yd)*k,229,(f_yp+f_yd)*(1+k),bg);
            k++;
         }
         done=0;
         new_scroll1(nowline,lines);
         //show_scroll(pages,nowline*100/lines);
      }
      else if(done==2)
      {
         mov_scr(1,1,229,(plines-1)*(f_yd+f_yp),f_yd+f_yp,2);
         //Q_box(1,1,239,f_yd+f_yp,bg);
         show_ntxt_line(0,text+sav[nowline],color,bg);
         new_scroll1(nowline,lines);
         //show_scroll(pages,nowline*100/lines);
         done=0;
      }
      else if(done==3)
      {
         mov_scr(1,1+f_yd+f_yp,229,159,f_yd+f_yp,1);
         if (nowline+plines<lines) show_ntxt_line(plines-1,text+sav[nowline+plines-1],color,bg);
         else Q_box(2,1+(f_yp+f_yd)*(plines-1),229,(f_yp+f_yd)*plines,bg);
         new_scroll1(nowline,lines);
         //show_scroll(pages,nowline*100/lines);
         done=0;
      }
   }
}
extern const char text_helps[];
void etxt()
{
}
void nsh_text(char *tit,char *txt,u32 size)
{
   u32 *sav=(u32 *)new_malloc(4*(size/28+10));
   u16 color[2];
   if (sav==NULL)
   {
      if (!arlt("系统提示:","文本过大或者内存不足,打开文本可能会导致死机.\n[A:继续]   [B:退出]")) return;
      else sav=(u32 *)new_malloc(1024);
   }
   txt_get(color);
   font_set(2);
   if (fnt_sets.xd>5) fnt_sets.xd=0;
   if (fnt_sets.yd>5) fnt_sets.xd=0;
   if (color[0]==color[1])
   {
      color[1]=RGB(0,9,16);
      color[0]=RGB(30,30,30);
      txt_set(color);
   }
   strcpy(_file,tit);
   win_plate(tit,"文本浏览器V1.0",RGB(26,26,25));
   if (fnt_sets.on && fnt_sets.font) ntext(txt,color[0],color[1],sav);
   else text(txt,color[0],color[1],sav);
   new_free(sav);
}

⌨️ 快捷键说明

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