Subversion Repositories wimsdev

Rev

Rev 7675 | Rev 8134 | 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. #include "flydraw.h"
  20.  
  21. void error(char *p)
  22. {
  23.     fprintf(stderr,"%s %d\n",p,linecnt);
  24. }
  25.  
  26. /* Get a line in a stored working file.
  27.  * Buffer length is always MAX_LINELEN. */
  28.  
  29. /* 1/2012
  30. J.M. Evers
  31. Added ';' as additional command seperator
  32. */
  33. int ggetline(char buf[])
  34. {
  35.     int c;
  36.     int i;
  37.     for(i=0,c=getchar();i<MAX_LINELEN;i++,c=getchar()) {
  38.       if(c=='\n' || c==EOF || c==';') break;
  39.       buf[i]=c;
  40.     }
  41.     buf[i]=0;
  42.     if(linecnt!=-100000) linecnt++;
  43.     return c;
  44. }
  45.  
  46. double getvar(char *p)
  47. {
  48.     int i;
  49.     for(i=0;i<varcnt && strcmp(p,vartab[i].name)!=0;i++);
  50.     if(i<varcnt) return vartab[i].value;
  51.     else return 0;
  52. }
  53.  
  54. void setvar(char *p, double v)
  55. {
  56.     int i;
  57.     if((strlen(p)>2 && strcmp(p,"animstep")!=0) || !isalpha(*p)) return;
  58.     for(i=0;i<varcnt && strcmp(p,vartab[i].name)!=0;i++);
  59.     if(i<varcnt) {vartab[i].value=v; return;}
  60.     else {
  61.       if(varcnt>=MAX_VARS || varnameptr>=varnamebuf+sizeof(varnamebuf)-1) return;
  62.       ovlstrcpy(varnameptr,p);
  63.       vartab[varcnt].name=varnameptr; vartab[varcnt].value=v;
  64.       varnameptr+=strlen(varnameptr)+1; (varcnt)++;
  65.     }
  66. }
  67.  
  68. /* Points to the end of a name */
  69. char *find_name_end(char *p)
  70. {
  71.     int i;
  72.     for(i=0;isalnum(*p) && i<MAX_LINELEN; p++,i++);
  73.     return p;
  74. }
  75.  
  76. /* Find the beginning of a name */
  77. char *find_name_start(char *p)
  78. {
  79.     int i;
  80.     for(i=0; !isalpha(*p) && i<MAX_LINELEN; p++,i++);
  81.     return p;
  82. }
  83.  
  84. void collapse_item(char *p, int n)
  85. {
  86.     int i;
  87.     char *pp;
  88.     if(n<1) return;
  89.     for(i=1,pp=strchr(p,','); i<n && pp!=NULL; i++,pp=strchr(pp+1,','));
  90.     if(pp==NULL) *p=0;
  91.     else ovlstrcpy(p,pp+1);
  92. }
  93.  
  94. int getcolor(int r, int g, int b)
  95. {
  96.     int col;
  97.     if(r>255) r=255; if(r<0) r=0;
  98.     if(g>255) g=255; if(g<0) g=0;
  99.     if(b>255) b=255; if(b<0) b=0;
  100.     col=gdImageColorExact(image, r, g, b);
  101.     if(col==-1) col=gdImageColorAllocate(image,r,g,b);
  102.     return col;
  103. }
  104.  
  105. int widthcolor(int w, int color)
  106. {
  107.     int bg,fg,sh,e;
  108.     /* already allocated */
  109.     if(wimg!=NULL && savew==w && wcolor==color) goto end;
  110.     if(wimg!=NULL) gdImageDestroy(wimg);
  111.     wimg=gdImageCreate(w,w);
  112.     if(wimg==NULL) {
  113.       error("width_creation_failure"); return color;
  114.     }
  115.     bg=gdImageColorAllocate(wimg,255,255,255);
  116.     gdImageColorTransparent(wimg,bg);
  117.     fg=gdImageColorAllocate(wimg,gdImageRed(image,color),
  118.                       gdImageGreen(image,color),
  119.                       gdImageBlue(image,color));
  120.     e=w-1;sh=e/3;
  121.     switch(w) {
  122.       case 2: {
  123.           gdImageFill(wimg,0,0,fg); break;
  124.       }
  125.       case 3: {
  126.           gdImageFill(wimg,0,0,fg);
  127.           gdImageSetPixel(wimg,2,0,bg);gdImageSetPixel(wimg,2,2,bg);
  128.           break;
  129.       }
  130.       case 4:
  131.       case 5:
  132.       case 6:
  133.       case 7:
  134.       case 8:
  135.       case 9:
  136.       case 10:
  137.       case 11:
  138.       case 12: {
  139.           int pl[]={0,sh, sh,0, e-sh,0, e,sh,
  140.               e,e-sh, e-sh,e, sh,e, 0,e-sh};
  141.           gdImageFilledPolygon(wimg,(gdPointPtr) pl,8,fg);
  142.           break;
  143.       }
  144.       default: {
  145.           gdImageArc(wimg,w/2,w/2,w,w,0,360,fg);
  146.           gdImageFillToBorder(wimg,w/2,w/2,fg,fg);
  147.           break;
  148.       }
  149.     }
  150.     savew=w; wcolor=color;
  151.     end: gdImageSetBrush(image,wimg); return gdBrushed;
  152. }
  153.  
  154. /* scale coordinates, x then y */
  155. void scale(double dbuf[], int ibuf[], int cnt)
  156. {
  157.     int i; double x,y;
  158.     for(i=0;i<cnt*2 && i<MAX_PARMS;i+=2) {
  159.       if(transform) {
  160.           x=dbuf[i]*matrix[0]+dbuf[i+1]*matrix[1];
  161.           y=dbuf[i]*matrix[2]+dbuf[i+1]*matrix[3];
  162.       }
  163.       else {x=dbuf[i]; y=dbuf[i+1];}
  164.       scale_buf[i]=x+transx; scale_buf[i+1]=y+transy;
  165.       ibuf[i]=rint((x-xstart+transx)*xscale);
  166.       ibuf[i+1]=rint((y-ystart+transy)*yscale);
  167.       if(ibuf[i]<-BOUND) ibuf[i]=-BOUND;if(ibuf[i]>BOUND) ibuf[i]=BOUND;
  168.       if(ibuf[i+1]<-BOUND) ibuf[i+1]=-BOUND;if(ibuf[i+1]>BOUND) ibuf[i+1]=BOUND;
  169.     }
  170. }
  171.  
  172. /* scale without displacement */
  173. void scale2(double xin, double yin, double *xout, double *yout)
  174. {
  175.     if(transform) {
  176.       *xout=xin*matrix[0]+yin*matrix[1];
  177.       *yout=xin*matrix[2]+yin*matrix[3];
  178.     }
  179.     else {
  180.       *xout=xin; *yout=yin;
  181.     }
  182.     *xout*=xscale; *yout*=yscale;
  183. }
  184.  
  185.