Subversion Repositories wimsdev

Rev

Rev 11120 | Rev 16344 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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