fix texture loading
[cltracer.git] / conf.c
blob9f31779faf6921f381fcda424c0e8ad016bb2f92
1 /*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 * Copyright (C) 2010 Pavel Herrmann (morpheus.ibis@gmail.com)
19 #include "types.h"
20 #include "shared.h"
21 #include "conf.h"
22 #include <stdio.h>
23 #include <string.h>
25 size_t tilesize[2];
26 size_t lwsize[2];
27 struct frameconfig_s * frames=NULL;
28 struct frameconfig_s * current_frame=NULL;
29 struct frameconfig_s last_frame;
30 int framecount;
31 cl_int lightcount;
32 cl_int2 tiles;
33 struct light_s * lightlist=NULL;
34 float density;
35 char * outname=NULL;
36 char * scenefile=NULL;
37 int accelstruct;
38 int interactive=0;
40 #define ISWHITE(x) ((x==' ')||(x=='\t'))
41 #define ISEOL(x) ((x=='0')||(x=='#')||(x=='\n'))
42 #define MAX(x,y) (x>y?x:y)
45 int config_load(char* filename)
47 FILE * f = fopen(filename, "r");
48 if (f==NULL)
49 return 1;
50 char * line=NULL;
51 char * tline=NULL;
52 size_t linelength=0;
53 int read;
54 while ( (read=getline(&line,&linelength,f))!=-1)
56 //don't want the newline char at the end of lines
57 if(line[read-1]=='\n')
58 line[read-1]=0;
59 if(read<1)
60 continue;
61 tline=line;
62 while(ISWHITE(tline[0]))
63 tline++;
64 if (ISEOL(tline[0]))
65 continue;
67 if(!strncmp(tline,"target",6))
69 tline+=7;
70 while(ISWHITE(tline[0]))
71 tline++;
72 int len = strlen(tline);
73 outname = malloc(sizeof(char)*(len+1));
74 strncpy(outname,tline,len+1);
75 continue;
78 if(!strncmp(tline,"objfile",7))
80 tline+=8;
81 while(ISWHITE(tline[0]))
82 tline++;
83 int len = strlen(tline);
84 scenefile = malloc(sizeof(char)*(len+1));
85 strncpy(scenefile,tline,len+1);
86 continue;
89 if(!strncmp(tline,"density",7))
91 tline+=8;
92 while(ISWHITE(tline[0]))
93 tline++;
94 sscanf(tline,"%f",&density);
95 continue;
98 if(!strncmp(tline,"tiles",5))
100 tline+=6;
101 while(ISWHITE(tline[0]))
102 tline++;
103 sscanf(tline,"%d %d",&(tiles.x),&(tiles.y) );
104 continue;
107 if(!strncmp(tline,"resolution",10))
109 tline+=11;
110 while(ISWHITE(tline[0]))
111 tline++;
112 sscanf(tline,"%zd %zd",&(tilesize[0]),&(tilesize[1]) );
113 continue;
116 if(!strncmp(tline,"worksize",8))
118 tline+=9;
119 while(ISWHITE(tline[0]))
120 tline++;
121 sscanf(tline,"%zd %zd",&(lwsize[0]),&(lwsize[1]) );
122 continue;
125 if(!strncmp(tline,"frames",6))
127 tline+=7;
128 while(ISWHITE(tline[0]))
129 tline++;
130 if (!strncmp(tline,"interactive",11))
132 framecount = 1;
133 interactive = 1;
135 else
137 sscanf(tline,"%d",&framecount );
138 interactive = 0;
140 frames = malloc(sizeof(struct frameconfig_s)*framecount);
141 current_frame = frames;
142 continue;
145 if(!strncmp(tline,"frame",5))
147 int framenum;
148 tline+=6;
149 while(ISWHITE(tline[0]))
150 tline++;
151 sscanf(tline,"%d",&framenum );
152 if (current_frame != NULL)
154 last_frame = *current_frame;
155 current_frame = &(frames[framenum]);
156 *current_frame = last_frame;
158 else
159 current_frame = &(frames[framenum]);
160 continue;
163 if(!strncmp(tline,"lookat",6))
165 tline+=7;
166 while(ISWHITE(tline[0]))
167 tline++;
168 sscanf(tline,"%f %f %f",&(current_frame->lookat.x),&(current_frame->lookat.y),&(current_frame->lookat.z) );
169 continue;
172 if(!strncmp(tline,"eyepoint",8))
174 tline+=9;
175 while(ISWHITE(tline[0]))
176 tline++;
177 sscanf(tline,"%f %f %f",&(current_frame->eyepoint.x),&(current_frame->eyepoint.y),&(current_frame->eyepoint.z) );
178 continue;
181 if(!strncmp(tline,"upvector",8))
183 tline+=9;
184 while(ISWHITE(tline[0]))
185 tline++;
186 sscanf(tline,"%f %f %f",&(current_frame->upvector.x),&(current_frame->upvector.y),&(current_frame->upvector.z) );
187 continue;
190 if(!strncmp(tline,"viewangle",9))
192 tline+=10;
193 while(ISWHITE(tline[0]))
194 tline++;
195 sscanf(tline,"%f %f",&(current_frame->angles.x),&(current_frame->angles.y) );
196 continue;
199 if(!strncmp(tline,"accelstruct",11))
201 tline+=12;
202 while(ISWHITE(tline[0]))
203 tline++;
204 if (!strncmp(tline,"grid",4))
206 accelstruct=ACCEL_GRID;
207 continue;
209 if (!strncmp(tline,"dummy",5))
211 accelstruct=ACCEL_DUMMY;
212 continue;
214 return 1;
217 if(!strncmp(tline,"lights",6))
219 tline+=7;
220 while(ISWHITE(tline[0]))
221 tline++;
222 sscanf(tline,"%d",&lightcount );
223 lightlist = malloc(sizeof(struct light_s)*lightcount);
224 continue;
227 if(!strncmp(tline,"light",5))
229 int lightnum;
230 int charcount=0;
231 struct light_s * currlight=NULL;
232 tline+=6;
233 while(ISWHITE(tline[0]))
234 tline++;
235 sscanf(tline,"%d %n",&lightnum,&charcount );
236 currlight=&(lightlist[lightnum]);
237 tline+=charcount;
238 while(ISWHITE(tline[0]))
239 tline++;
240 if (!strncmp(tline,"ambient",7))
242 currlight->type=LIGHT_AMBIENT;
243 tline+=8;
245 else if (!strncmp(tline,"omni",4))
247 currlight->type=LIGHT_OMNI;
248 tline+=5;
250 else if (!strncmp(tline,"directional",11))
252 currlight->type=LIGHT_DIRECTIONAL;
253 tline+=12;
255 else if (!strncmp(tline,"spot",4))
257 currlight->type=LIGHT_SPOT;
258 tline+=5;
260 while(ISWHITE(tline[0]))
261 tline++;
262 sscanf(tline,"%f [%f %f %f] %n", &currlight->intensity, &currlight->color.x, &currlight->color.y, &currlight->color.z,&charcount);
263 tline+=charcount;
264 while(ISWHITE(tline[0]))
265 tline++;
266 switch (currlight->type)
268 case LIGHT_AMBIENT:
269 break;
270 case LIGHT_OMNI:
271 sscanf(tline,"[%f %f %f]", &currlight->omni.position.x, &currlight->omni.position.y, &currlight->omni.position.z);
272 break;
273 case LIGHT_DIRECTIONAL:
274 sscanf(tline,"[%f %f %f]", &currlight->directional.direction.x, &currlight->directional.direction.y, &currlight->directional.direction.z);
275 break;
276 case LIGHT_SPOT:
277 sscanf(tline,"[%f %f %f] [%f %f %f] %f %f", &currlight->omni.position.x, &currlight->omni.position.y, &currlight->omni.position.z,
278 &currlight->spot.direction.x, &currlight->spot.direction.y, &currlight->spot.direction.z, &currlight->spot.coneangle, &currlight->spot.cutoff);
279 break;
280 default:
281 return 1;
283 continue;
288 free(line);
289 return 0;
292 void config_cleanup(void)
294 free(outname);
295 free(scenefile);
296 free(lightlist);
297 free(frames);