Subversion Repositories wimsdev

Rev

Rev 3718 | Rev 7675 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.         /* line input / output / translation routines
  18.          * and error routines */
  19.  
  20. void error(char *p)
  21. {
  22.     fprintf(stderr,"%s %d\n",p,linecnt);
  23. }
  24.  
  25.         /* Get a line in a stored working file.
  26.          * Buffer length is always MAX_LINELEN. */
  27.  
  28. /* 1/2012
  29. J.M. Evers
  30. Added ';' as additional command seperator
  31. */
  32. int ggetline(char buf[])
  33. {
  34.     int c;
  35.     int i;
  36.     for(i=0,c=getchar();i<MAX_LINELEN;i++,c=getchar()) {
  37.         if(c=='\n' || c==EOF || c==';') break;
  38.         buf[i]=c;
  39.     }
  40.     buf[i]=0;
  41.     if(linecnt!=-100000) linecnt++;
  42.     return c;
  43. }
  44.  
  45. double getvar(char *p)
  46. {
  47.     int i;
  48.     for(i=0;i<varcnt && strcmp(p,vartab[i].name)!=0;i++);
  49.     if(i<varcnt) return vartab[i].value;
  50.     else return 0;
  51. }
  52.  
  53. void setvar(char *p, double v)
  54. {
  55.     int i;
  56.     if((strlen(p)>2 && strcmp(p,"animstep")!=0) || !isalpha(*p)) return;
  57.     for(i=0;i<varcnt && strcmp(p,vartab[i].name)!=0;i++);
  58.     if(i<varcnt) {vartab[i].value=v; return;}
  59.     else {
  60.         if(varcnt>=MAX_VARS || varnameptr>=varnamebuf+sizeof(varnamebuf)-1) return;
  61.         ovlstrcpy(varnameptr,p);
  62.         vartab[varcnt].name=varnameptr; vartab[varcnt].value=v;
  63.         varnameptr+=strlen(varnameptr)+1; (varcnt)++;
  64.     }
  65. }
  66.  
  67.         /* Points to the end of a name */
  68. char *find_name_end(char *p)
  69. {
  70.     int i;
  71.     for(i=0;isalnum(*p) && i<MAX_LINELEN; p++,i++);
  72.     return p;
  73. }
  74.  
  75.         /* Find the beginning of a name */
  76. char *find_name_start(char *p)
  77. {
  78.     int i;
  79.     for(i=0; !isalpha(*p) && i<MAX_LINELEN; p++,i++);
  80.     return p;
  81. }
  82.  
  83. void collapse_item(char *p, int n)
  84. {
  85.     int i;
  86.     char *pp;
  87.     if(n<1) return;
  88.     for(i=1,pp=strchr(p,','); i<n && pp!=NULL; i++,pp=strchr(pp+1,','));
  89.     if(pp==NULL) *p=0;
  90.     else ovlstrcpy(p,pp+1);
  91. }
  92.  
  93. int getcolor(int r, int g, int b)
  94. {
  95.     int col;
  96.     if(r>255) r=255; if(r<0) r=0;
  97.     if(g>255) g=255; if(g<0) g=0;
  98.     if(b>255) b=255; if(b<0) b=0;
  99.     col=gdImageColorExact(image, r, g, b);
  100.     if(col==-1) col=gdImageColorAllocate(image,r,g,b);
  101.     return col;
  102. }
  103.  
  104. int widthcolor(int w, int color)
  105. {
  106.     int bg,fg,sh,e;
  107.             /* already allocated */
  108.     if(wimg!=NULL && savew==w && wcolor==color) goto end;
  109.     if(wimg!=NULL) gdImageDestroy(wimg);
  110.     wimg=gdImageCreate(w,w);
  111.     if(wimg==NULL) {
  112.         error("width_creation_failure"); return color;
  113.     }
  114.     bg=gdImageColorAllocate(wimg,255,255,255);
  115.     gdImageColorTransparent(wimg,bg);
  116.     fg=gdImageColorAllocate(wimg,gdImageRed(image,color),
  117.                             gdImageGreen(image,color),
  118.                             gdImageBlue(image,color));
  119.     e=w-1;sh=e/3;
  120.     switch(w) {
  121.         case 2: {
  122.             gdImageFill(wimg,0,0,fg); break;
  123.         }
  124.         case 3: {
  125.             gdImageFill(wimg,0,0,fg);
  126.             gdImageSetPixel(wimg,2,0,bg);gdImageSetPixel(wimg,2,2,bg);
  127.             break;
  128.         }
  129.         case 4:
  130.         case 5:
  131.         case 6:
  132.         case 7:
  133.         case 8:
  134.         case 9:
  135.         case 10:
  136.         case 11:
  137.         case 12: {
  138.             int pl[]={0,sh, sh,0, e-sh,0, e,sh,
  139.                   e,e-sh, e-sh,e, sh,e, 0,e-sh};
  140.             gdImageFilledPolygon(wimg,(gdPointPtr) pl,8,fg);
  141.             break;
  142.         }
  143.         default: {
  144.             gdImageArc(wimg,w/2,w/2,w,w,0,360,fg);
  145.             gdImageFillToBorder(wimg,w/2,w/2,fg,fg);
  146.             break;
  147.         }
  148.     }
  149.     savew=w; wcolor=color;
  150.     end: gdImageSetBrush(image,wimg); return gdBrushed;
  151. }
  152.  
  153.         /* scale coordinates, x then y */
  154. void scale(double dbuf[], int ibuf[], int cnt)
  155. {
  156.     int i; double x,y;
  157.     for(i=0;i<cnt*2 && i<MAX_PARMS;i+=2) {
  158.         if(transform) {
  159.             x=dbuf[i]*matrix[0]+dbuf[i+1]*matrix[1];
  160.             y=dbuf[i]*matrix[2]+dbuf[i+1]*matrix[3];
  161.         }
  162.         else {x=dbuf[i]; y=dbuf[i+1];}
  163.         scale_buf[i]=x+transx; scale_buf[i+1]=y+transy;
  164.         ibuf[i]=rint((x-xstart+transx)*xscale);
  165.         ibuf[i+1]=rint((y-ystart+transy)*yscale);
  166.         if(ibuf[i]<-BOUND) ibuf[i]=-BOUND;if(ibuf[i]>BOUND) ibuf[i]=BOUND;
  167.         if(ibuf[i+1]<-BOUND) ibuf[i+1]=-BOUND;if(ibuf[i+1]>BOUND) ibuf[i+1]=BOUND;
  168.     }
  169. }
  170.  
  171.         /* scale without displacement */
  172. void scale2(double xin, double yin, double *xout, double *yout)
  173. {
  174.     if(transform) {
  175.         *xout=xin*matrix[0]+yin*matrix[1];
  176.         *yout=xin*matrix[2]+yin*matrix[3];
  177.     }
  178.     else {
  179.         *xout=xin; *yout=yin;
  180.     }
  181.     *xout*=xscale; *yout*=yscale;
  182. }
  183.  
  184.