Project

General

Profile

Revision 1041

Added by Rich Hong about 15 years ago

load world from input file, changed create poly to take an array of doubles

View differences:

world.c
155 155
    p->pts = malloc((argc) * sizeof(point_t));
156 156
    p->type = poly_type;
157 157

  
158
    double* pts = (double*) malloc((argc) * sizeof(double) * 2);
159
    pts = va_arg(ap,double*);
160

  
158 161
    for(i=0;i < argc; i++) {
159
	p->pts[i].x = va_arg(ap, double);
160
	p->pts[i].y = va_arg(ap, double);
162
	p->pts[i].x = pts[2*i];
163
	p->pts[i].y = pts[2*i+1];
161 164
    }
162 165
    obj->id = ID_POLY;
163 166
    obj->bbox = NULL;
......
210 213
    }
211 214
}
212 215

  
216
int starts_with (const char* line, const char* word) {
217
  do {
218
    line++;
219
    word++;
220
  }while (*line == *word);
221
  if (*word == '\n' || *word == '\0') return 0;
222
  return 1;
223
}
213 224

  
225
int load_object (const char* line) {
226
  char buf[BUF_SIZE];
227
  int i = 0;
228
  int j;
229
  int id;
230
  if (starts_with(line,"POLYGON") == 0){
231
    int num;
232
    int type;
233
    double *pts;
234
    id = ID_POLY;
235
    i = strlen("POLYGON")+1;
236

  
237
    sscanf(line+i,"%s",buf);
238
    num = strtol(buf,NULL,0);
239
    i += strlen(buf) + 1;
240

  
241
    if (starts_with(line+i,"CONNECTED") == 0){
242
      i += strlen("CONNECTED") + 1;
243
      type = POLY_CONNECTED;
244
    }else if (starts_with(line+i,"DISCONNECTED") == 0){
245
      i += strlen("DISCONNECTED") + 1;
246
      type = POLY_DISCONNECTED;
247
    }else if (starts_with(line+i,"RECT") == 0){
248
      i += strlen("RECT") + 1;
249
      type = POLY_RECT;
250
    }else{
251
      // invalid input
252
	fprintf(stderr,"Invalid input for POLYGON\n");
253
      return -1;
254
    }
255

  
256
    char *c1,*c2;
257
    c1 = (char*)(line+i);
258
    c2 = (char*)(line+i);
259
    pts = (double*) malloc(sizeof(double)*num*2);
260
    for (j = 0;j < num;j++){
261
      pts[2*j] = strtod(c1,&c2);
262
      pts[2*j+1] = strtod(c2,&c1);
263
      printf("%lf %lf\n",pts[2*j],pts[2*j+1]);
264
    }
265

  
266
    create(id,num,type,pts);
267
    free(pts);
268
  }
269
  return 0;
270
}
271

  
272
int load_world (const char* filename) {
273
  FILE* fin;
274
  char buf[BUF_SIZE];
275
  bbox_t bbox = {0,0,0,0};
276
  if ((fin = fopen(filename,"r"))==NULL){
277
    // open file failed
278
	perror("Fail to open file");
279
    return -1;
280
  }
281
  if (fgets(buf,512,fin) != NULL){
282
    sscanf(buf,"WORLD %lf %lf %lf %lf",&bbox.p1.x,&bbox.p1.y,&bbox.p2.x,&bbox.p2.y);
283
    init_world(MAX_OBJS, bbox);
284
    while (fgets(buf,512,fin) != NULL){
285
      load_object(buf);
286
    }
287
  }
288
  fclose(fin);
289
  return 0;
290
}
291

  

Also available in: Unified diff