diff options
author | Ian C <ianc@noddybox.co.uk> | 2010-08-18 14:38:34 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2010-08-18 14:38:34 +0000 |
commit | 19bbd5822593c7198dd54eb0f5d38fa1ed8d8fb7 (patch) | |
tree | 97f7f8849c3a8282ba571a64ff2a655cdf5d41aa /config.cpp | |
parent | 0d2f672482fee91a9642a7cb6eb01fcdded79159 (diff) |
Reformatted code and altered the attraction rule to something a bit more
correct. Also added two tests to prove it works.
Diffstat (limited to 'config.cpp')
-rw-r--r-- | config.cpp | 647 |
1 files changed, 349 insertions, 298 deletions
@@ -36,60 +36,66 @@ Config::Config() { int f; - m_error=""; + m_error = ""; for(f=0;f<num_flags;f++) - m_flag[f]=false; + { + m_flag[f] = false; + } - m_collide=none; + m_collide = none; - m_light=""; - m_lr=m_lg=m_lb=1.0f; + m_light = ""; + m_lr = 1.0f; + m_lg = 1.0f; + m_lb = 1.0f; - m_const=0.0; + m_const = 0.0; - m_x=0.0f; - m_y=0.0f; - m_z=0.0f; - m_yaw=0.0f; + m_x = 0.0f; + m_y = 0.0f; + m_z = 0.0f; + m_yaw = 0.0f; - m_glid=0; - m_glid_no=0; - m_glid_idx=0; + m_glid = 0; + m_glid_no = 0; + m_glid_idx = 0; - m_scale=1.0; + m_scale = 1.0; - m_particles=false; + m_particles = false; - m_plane=xy_plane; + m_plane = xy_plane; - m_look_at=-1; - m_travel_as=-1; + m_look_at = -1; + m_travel_as = -1; - m_s_no=10; - m_s_delta=5.0; - m_s_min=10.0; - m_s_max=1000.0; - m_s_shield=100; + m_s_no = 10; + m_s_delta = 5.0; + m_s_min = 10.0; + m_s_max = 1000.0; + m_s_shield = 100; - m_sparks=false; + m_sparks = false; - m_winw=640; - m_winh=480; + m_winw = 640; + m_winh = 480; - m_max_mass=1000000; + m_max_mass = 1000000; - m_fog_dist=0.0f; - m_fog_r=0.0f; - m_fog_g=0.0f; - m_fog_b=0.0f; - m_fog_light=false; + m_fog_dist = 0.0f; + m_fog_r = 0.0f; + m_fog_g = 0.0f; + m_fog_b = 0.0f; + m_fog_light = false; } Config::~Config() { if (m_glid) + { delete[] m_glid; + } } @@ -98,24 +104,24 @@ Config::~Config() bool Config::read(const string& fname,vector<Mass>& mass) { static struct - { + { Command cmd; string token; int val; - } enum_list[]= - { - {enable_cmd,"solid",static_cast<int>(solid)}, - {enable_cmd,"lighting",static_cast<int>(lighting)}, - {enable_cmd,"texture",static_cast<int>(texture)}, - {collide_cmd,"none",static_cast<int>(none)}, - {collide_cmd,"merge",static_cast<int>(merge)}, - {collide_cmd,"delta_merge",static_cast<int>(delta_merge)}, - {collide_cmd,"shatter",static_cast<int>(shatter)}, - {plane_cmd,"xy",static_cast<int>(xy_plane)}, - {plane_cmd,"xz",static_cast<int>(xz_plane)}, - {plane_cmd,"yz",static_cast<int>(yz_plane)}, - {cmd_error,"",0} - }; + } enum_list[]= + { + {enable_cmd,"solid",static_cast<int>(solid)}, + {enable_cmd,"lighting",static_cast<int>(lighting)}, + {enable_cmd,"texture",static_cast<int>(texture)}, + {collide_cmd,"none",static_cast<int>(none)}, + {collide_cmd,"merge",static_cast<int>(merge)}, + {collide_cmd,"delta_merge",static_cast<int>(delta_merge)}, + {collide_cmd,"shatter",static_cast<int>(shatter)}, + {plane_cmd,"xy",static_cast<int>(xy_plane)}, + {plane_cmd,"xz",static_cast<int>(xz_plane)}, + {plane_cmd,"yz",static_cast<int>(yz_plane)}, + {cmd_error,"",0} + }; Mass parent; Mass *nm; @@ -128,37 +134,37 @@ bool Config::read(const string& fname,vector<Mass>& mass) vector<string>::size_type sidx; if (!file) - { + { m_error="File not found : " + fname; return false; - } + } try - { + { while(getCommand(file,cmd,arg)) - { + { switch(cmd) - { + { case enable_cmd: f=0; found=false; while(enum_list[f].token!="") - { + { if (enum_list[f].cmd==enable_cmd && CompareNoCase(enum_list[f].token,arg[0])) - { + { found=true; m_flag[static_cast<Flag>(enum_list[f].val)]=true; break; - } - f++; } + f++; + } if (!found) - { + { m_error="Illegal argument to enable : " + arg[0]; throw m_error; - } + } break; @@ -166,22 +172,22 @@ bool Config::read(const string& fname,vector<Mass>& mass) f=0; found=false; while(enum_list[f].token!="") - { + { if (enum_list[f].cmd==enable_cmd && CompareNoCase(enum_list[f].token,arg[0])) - { + { found=true; m_flag[static_cast<Flag>(enum_list[f].val)]=false; break; - } - f++; } + f++; + } if (!found) - { + { m_error="Illegal argument to disable : " + arg[0]; throw m_error; - } + } break; @@ -189,22 +195,22 @@ bool Config::read(const string& fname,vector<Mass>& mass) f=0; found=false; while(enum_list[f].token!="") - { + { if (enum_list[f].cmd==collide_cmd && CompareNoCase(enum_list[f].token,arg[0])) - { + { found=true; m_collide=static_cast<Collide>(enum_list[f].val); break; - } - f++; } + f++; + } if (!found) - { + { m_error="Illegal argument to collide : " + arg[0]; throw m_error; - } + } break; @@ -222,14 +228,16 @@ bool Config::read(const string& fname,vector<Mass>& mass) toGLfloat(arg[10]), toDouble(arg[11])); if (mass.size()<m_max_mass) + { mass.push_back(*nm); + } delete nm; break; case radial_cmd: if (findMass(mass,arg[2],parent)) - { + { genRadial(&nm,parent, arg[0],toDouble(arg[1]), toDouble(arg[3]),RAD(toDouble(arg[4])), @@ -239,20 +247,22 @@ bool Config::read(const string& fname,vector<Mass>& mass) toDouble(arg[9])); if (mass.size()<m_max_mass) + { mass.push_back(*nm); + } delete nm; - } + } else - { + { m_error="Unknown parent in radial : " + arg[2]; throw m_error; - } + } break; case multiple_cmd: if (findMass(mass,arg[4],parent)) - { + { string name=arg[0]; int num=toInt(arg[1]); double ms=toDouble(arg[2]); @@ -271,7 +281,7 @@ bool Config::read(const string& fname,vector<Mass>& mass) int f; for(f=0;f<num;f++) - { + { genRadial(&nm,parent, makeName(name,f+1),ms, dist,ang,spd, @@ -284,16 +294,18 @@ bool Config::read(const string& fname,vector<Mass>& mass) spd+=spd_i; if (mass.size()<m_max_mass) + { mass.push_back(*nm); + } delete nm; - } } + } else - { + { m_error="Unknown parent in multiple : " + arg[4]; throw m_error; - } + } break; case light_cmd: @@ -312,34 +324,36 @@ bool Config::read(const string& fname,vector<Mass>& mass) case look_cmd: if (findMass(mass,arg[0],parent)) + { m_look.push_back(arg[0]); + } else - { + { m_error="Unknown object in look : " + arg[0]; throw m_error; - } + } break; case mlook_cmd: { - int num=toInt(arg[1]); - string name=arg[0]; - int f; + int num=toInt(arg[1]); + string name=arg[0]; + int f; - for(f=0;f<num;f++) + for(f=0;f<num;f++) { - string ss; + string ss; - ss=makeName(name,f+1); + ss=makeName(name,f+1); - if (findMass(mass,ss,idx)) + if (findMass(mass,ss,idx)) { - m_look.push_back(ss); + m_look.push_back(ss); } - else + else { - m_error="Unknown object in mlook : " + ss; - throw m_error; + m_error="Unknown object in mlook : " + ss; + throw m_error; } } } @@ -347,42 +361,44 @@ bool Config::read(const string& fname,vector<Mass>& mass) case texture_cmd: if (!m_glid) - { + { m_error="texture used without preceeding num_texture"; throw m_error; - } + } if (findMass(mass,arg[0],idx)) - { + { if (m_glid_no==m_glid_idx) - { + { m_error="more textures than expected"; throw m_error; - } + } GLuint id=m_glid[m_glid_idx++]; if (bindTexture(arg[1],toInt(arg[2]),toInt(arg[3]),id)) + { mass[idx].setTexture(id); + } else - { + { m_error="Failed to bind texture : " + m_error; throw m_error; - } } + } else - { + { m_error="Unknown object in texture : " + arg[0]; throw m_error; - } + } break; case num_texture_cmd: if (m_glid) - { + { m_error="num_texture used more than once"; throw m_error; - } + } m_glid_no=toInt(arg[0]); m_glid=new GLuint[m_glid_no]; @@ -392,76 +408,80 @@ bool Config::read(const string& fname,vector<Mass>& mass) case reuse_cmd: if (findMass(mass,arg[0],idx)) - { + { if (findMass(mass,arg[1],parent)) - { + { GLuint id; if (parent.getTexture(id) || parent.getRingTexture(id)) + { mass[idx].setTexture(id); + } else - { + { m_error="reuse object has no texture : " + arg[1]; throw m_error; - } } + } else - { + { m_error="Unknown object in reuse : " + arg[1]; throw m_error; - } } + } else - { + { m_error="Unknown object in reuse : " + arg[0]; throw m_error; - } + } break; case mreuse_cmd: if (findMass(mass,arg[2],parent)) - { + { int num=toInt(arg[1]); string name=arg[0]; int f; for(f=0;f<num;f++) - { + { string ss=makeName(name,f+1); if (findMass(mass,ss,idx)) - { + { GLuint id; if (parent.getTexture(id) || parent.getRingTexture(id)) + { mass[idx].setTexture(id); + } else - { + { m_error="mreuse object has no texture : " + arg[2]; throw m_error; - } } + } else - { + { m_error="Unknown object in mreuse : " + ss; throw m_error; - } } } + } else - { + { m_error="Unknown object in mreuse : " + arg[2]; throw m_error; - } + } break; case ring_cmd: if (findMass(mass,arg[0],idx)) - { + { mass[idx].setRing(toDouble(arg[1]),toDouble(arg[2]), toGLfloat(arg[3]), toGLfloat(arg[4]), @@ -469,38 +489,39 @@ bool Config::read(const string& fname,vector<Mass>& mass) toGLfloat(arg[6]), toDouble(arg[7]), toDouble(arg[8])); - } + } else - { + { m_error="Unknown object in ring : " + arg[0]; throw m_error; - } + } break; case mring_cmd: { - int num=toInt(arg[1]); - string name=arg[0]; - int f; + int num=toInt(arg[1]); + string name=arg[0]; + int f; - for(f=0;f<num;f++) + for(f=0;f<num;f++) { - string ss=makeName(name,f+1); + string ss=makeName(name,f+1); - if (findMass(mass,ss,idx)) + if (findMass(mass,ss,idx)) { - mass[idx].setRing(toDouble(arg[2]),toDouble(arg[3]), - toGLfloat(arg[4]), - toGLfloat(arg[5]), - toGLfloat(arg[6]), - toGLfloat(arg[7]), - toDouble(arg[8]), - toDouble(arg[9])); + mass[idx].setRing(toDouble(arg[2]), + toDouble(arg[3]), + toGLfloat(arg[4]), + toGLfloat(arg[5]), + toGLfloat(arg[6]), + toGLfloat(arg[7]), + toDouble(arg[8]), + toDouble(arg[9])); } - else + else { - m_error="Unknown object in mring : " + ss; - throw m_error; + m_error="Unknown object in mring : " + ss; + throw m_error; } } } @@ -508,103 +529,103 @@ bool Config::read(const string& fname,vector<Mass>& mass) case ring_texture_cmd: if (!m_glid) - { + { m_error="ring used without preceeding num_texture"; throw m_error; - } + } if (findMass(mass,arg[0],idx)) - { + { if (m_glid_no==m_glid_idx) - { + { m_error="more textures than expected"; throw m_error; - } + } GLuint id=m_glid[m_glid_idx++]; if (bindTexture(arg[1],toInt(arg[2]),toInt(arg[3]),id)) mass[idx].setRingTexture(id); else - { + { m_error="Failed to bind texture : " + m_error; throw m_error; - } } + } else - { + { m_error="Unknown object in ring : " + arg[0]; throw m_error; - } + } break; case ring_reuse_cmd: if (findMass(mass,arg[0],idx)) - { + { if (findMass(mass,arg[1],parent)) - { + { GLuint id; if (parent.getRingTexture(id) || - parent.getTexture(id)) + parent.getTexture(id)) mass[idx].setRingTexture(id); else - { + { m_error="ring_reuse object has no texture : " + - arg[1]; + arg[1]; throw m_error; - } } + } else - { + { m_error="Unknown object in ring_reuse : " + arg[1]; throw m_error; - } } + } else - { + { m_error="Unknown object in ring_reuse : " + arg[0]; throw m_error; - } + } break; case ring_mreuse_cmd: if (findMass(mass,arg[2],parent)) - { + { int num=toInt(arg[1]); string name=arg[0]; int f; for(f=0;f<num;f++) - { + { string ss=makeName(name,f+1); if (findMass(mass,ss,idx)) - { + { GLuint id; if (parent.getRingTexture(id) || - parent.getTexture(id)) + parent.getTexture(id)) mass[idx].setRingTexture(id); else - { + { m_error="ring_mreuse object has " - "no texture : " + arg[2]; + "no texture : " + arg[2]; throw m_error; - } } + } else - { + { m_error="Unknown object in ring_mreuse : " + ss; throw m_error; - } } } + } else - { + { m_error="Unknown object in ring_mreuse : " + arg[2]; throw m_error; - } + } break; case particles_cmd: @@ -618,22 +639,22 @@ bool Config::read(const string& fname,vector<Mass>& mass) f=0; found=false; while(enum_list[f].token!="") - { + { if (enum_list[f].cmd==plane_cmd && CompareNoCase(enum_list[f].token,arg[0])) - { + { found=true; m_plane=static_cast<Plane>(enum_list[f].val); break; - } - f++; } + f++; + } if (!found) - { + { m_error="Illegal argument to plane : " + arg[0]; throw m_error; - } + } break; @@ -653,34 +674,36 @@ bool Config::read(const string& fname,vector<Mass>& mass) found=false; for(sidx=0;sidx<m_look.size();sidx++) - if (m_look[sidx]==arg[0]) - { + if (m_look[sidx]==arg[0]) + { m_look_at=sidx; found=true; - } + } if (!found) - { + { m_error="Illegal argument to look_at : " + arg[0]; throw m_error; - } + } break; case travel_as_cmd: found=false; for(sidx=0;sidx<m_look.size();sidx++) - if (m_look[sidx]==arg[0]) - { + { + if (m_look[sidx]==arg[0]) + { m_travel_as=sidx; found=true; - } + } + } if (!found) - { + { m_error="Illegal argument to travel_as : " + arg[0]; throw m_error; - } + } break; case window_size_cmd: @@ -712,22 +735,24 @@ bool Config::read(const string& fname,vector<Mass>& mass) default: break; - } } } + } catch(...) - { + { file.close(); return false; - } + } if (cmd==cmd_eof) - { + { file.close(); return true; - } + } else + { return false; + } } void Config::getCamera(GLfloat& x, GLfloat& y, GLfloat& z, GLfloat& yaw) const @@ -858,15 +883,19 @@ bool Config::CompareNoCase(const string& s1, const string& s2) p2=s2.c_str(); while((*p1)&&(tolower(*p1)==tolower(*p2))) - { + { p1++; p2++; - } + } if (tolower(*p1)-tolower(*p2)) + { return false; + } else + { return true; + } } @@ -880,64 +909,84 @@ Config::GetWordStatus Config::getWord(ifstream& file, string& word, char c; if (term==";") + { not_allowed=","; + } else if (term==",") + { not_allowed=";"; + } else + { not_allowed=",;"; + } word=""; while(!done) - { + { file.get(c); if (file.eof()) + { return GWEof; + } if (in_comment) - { + { if (c=='\n') - in_comment=false; + { + in_comment=false; } + } else - { + { if (c=='#') - in_comment=true; - else - if (in_word) - { - bool at_term=(term.find(c)!=string::npos); - bool in_error=(not_allowed.find(c)!=string::npos); - - if (in_error) - { - if (term==",") - return GWTooFewArgs; - else if (term==";") - return GWTooManyArgs; - else - return GWSyntax; - } + { + in_comment=true; + } + else if (in_word) + { + bool at_term=(term.find(c)!=string::npos); + bool in_error=(not_allowed.find(c)!=string::npos); - if (at_term) - done=true; - else - if (c!='\n') - word+=c; + if (in_error) + { + if (term==",") + { + return GWTooFewArgs; } - else + else if (term==";") { - bool in_ignore=(ignore.find(c)!=string::npos); - - if (!in_ignore) - { - word+=c; - in_word=true; - } + return GWTooManyArgs; } + else + { + return GWSyntax; + } + } + + if (at_term) + { + done=true; + } + else if (c!='\n') + { + word+=c; + } + } + else + { + bool in_ignore=(ignore.find(c)!=string::npos); + + if (!in_ignore) + { + word+=c; + in_word=true; + } } } + } return GWOK; } @@ -947,44 +996,44 @@ bool Config::getCommand(ifstream& file, Config::Command& cmd, vector<string>& arg) { static struct - { - string cmd; - Command token; - int args; - } cmd_list[]= - { - {"enable",enable_cmd,1}, - {"disable",disable_cmd,1}, - {"collide",collide_cmd,1}, - {"const",const_cmd,1}, - {"body",body_cmd,12}, - {"radial",radial_cmd,10}, - {"light",light_cmd,4}, - {"camera",camera_cmd,4}, - {"look",look_cmd,1}, - {"texture",texture_cmd,4}, - {"num_texture",num_texture_cmd,1}, - {"reuse",reuse_cmd,2}, - {"particles",particles_cmd,3}, - {"shatter",shatter_cmd,5}, - {"multiple",multiple_cmd,15}, - {"mreuse",mreuse_cmd,3}, - {"plane",plane_cmd,1}, - {"scale",scale_cmd,1}, - {"look_at",look_at_cmd,1}, - {"travel_as",travel_as_cmd,1}, - {"window_size",window_size_cmd,2}, - {"sparks",sparks_cmd,5}, - {"max_mass",max_mass_cmd,1}, - {"mlook",mlook_cmd,2}, - {"ring",ring_cmd,9}, - {"mring",mring_cmd,10}, - {"ring_texture",ring_texture_cmd,4}, - {"ring_reuse",ring_reuse_cmd,2}, - {"ring_mreuse",ring_mreuse_cmd,3}, - {"fog",fog_cmd,5}, - {"",cmd_error,1} - }; + { + string cmd; + Command token; + int args; + } cmd_list[]= + { + {"enable",enable_cmd,1}, + {"disable",disable_cmd,1}, + {"collide",collide_cmd,1}, + {"const",const_cmd,1}, + {"body",body_cmd,12}, + {"radial",radial_cmd,10}, + {"light",light_cmd,4}, + {"camera",camera_cmd,4}, + {"look",look_cmd,1}, + {"texture",texture_cmd,4}, + {"num_texture",num_texture_cmd,1}, + {"reuse",reuse_cmd,2}, + {"particles",particles_cmd,3}, + {"shatter",shatter_cmd,5}, + {"multiple",multiple_cmd,15}, + {"mreuse",mreuse_cmd,3}, + {"plane",plane_cmd,1}, + {"scale",scale_cmd,1}, + {"look_at",look_at_cmd,1}, + {"travel_as",travel_as_cmd,1}, + {"window_size",window_size_cmd,2}, + {"sparks",sparks_cmd,5}, + {"max_mass",max_mass_cmd,1}, + {"mlook",mlook_cmd,2}, + {"ring",ring_cmd,9}, + {"mring",mring_cmd,10}, + {"ring_texture",ring_texture_cmd,4}, + {"ring_reuse",ring_reuse_cmd,2}, + {"ring_mreuse",ring_mreuse_cmd,3}, + {"fog",fog_cmd,5}, + {"",cmd_error,1} + }; string word; string cmd_string; @@ -992,7 +1041,7 @@ bool Config::getCommand(ifstream& file, Config::Command& cmd, int f; switch (getWord(file,word," \t\n"," \t\n")) - { + { case GWEof: cmd=cmd_eof; return false; @@ -1001,40 +1050,40 @@ bool Config::getCommand(ifstream& file, Config::Command& cmd, return false; default: break; - } + } f=0; cmd=cmd_error; while(cmd_list[f].cmd!="") - { + { if (CompareNoCase(cmd_list[f].cmd,word)) - { + { cmd_string=word; cmd=cmd_list[f].token; expect=cmd_list[f].args; break; - } + } f++; - } + } if (cmd==cmd_error) - { + { m_error="Unrecognised command " + word; - return false; - } + return false; + } arg.clear(); for(f=0;f<expect;f++) - { + { string error; switch (getWord(file,word," \t\n",(f==expect-1) ? ";" : ",")) - { + { case GWOK: - break; + break; case GWTooManyArgs: cmd=cmd_error; @@ -1055,10 +1104,10 @@ bool Config::getCommand(ifstream& file, Config::Command& cmd, cmd=cmd_error; m_error="Syntax error in " + cmd_string; return false; - } + } arg.push_back(word); - } + } return true; } @@ -1071,10 +1120,10 @@ double Config::toDouble(const string& s) r=strtod(s.c_str(),&p); if (*p) - { + { m_error="Invalid floating point number : " + s; - throw m_error; - } + throw m_error; + } return r; } @@ -1094,10 +1143,10 @@ int Config::toInt(const string& s) i=static_cast<int>(strtol(s.c_str(),&p,0)); if (*p) - { + { m_error="Invalid integer : " + s; - throw m_error; - } + throw m_error; + } return i; } @@ -1111,10 +1160,10 @@ size_t Config::toSize(const string& s) i=static_cast<size_t>(strtol(s.c_str(),&p,0)); if (*p) - { + { m_error="Invalid integer : " + s; - throw m_error; - } + throw m_error; + } return i; } @@ -1123,10 +1172,10 @@ size_t Config::toSize(const string& s) bool Config::toYesNo(const string& s) { if (s!="yes" && s!="no") - { + { m_error="Invalid yes/no : " + s; throw m_error; - } + } return s=="yes"; } @@ -1140,28 +1189,30 @@ bool Config::bindTexture(const string& name, int w, int h, GLuint id) int x,y; if (!(fp=fopen(name.c_str(),"r"))) - { + { m_error="file " + name + " not found"; - return false; - } + return false; + } mem=new GLubyte[w*h*3]; for(y=0;y<h;y++) - for(x=w-1;x>=0;x--) - { + { + for(x=w-1;x>=0;x--) + { mem[x*3+y*w*3]=fgetc(fp); mem[x*3+y*w*3+1]=fgetc(fp); mem[x*3+y*w*3+2]=fgetc(fp); if (feof(fp)) - { + { fclose(fp); m_error="file " + name + " to short"; delete[] mem; return false; - } } + } + } fclose(fp); @@ -1189,7 +1240,7 @@ void Config::genRadial(Mass **newMass, const Mass& parent, parent.getDelta(pdx,pdy,pdz); switch(m_plane) - { + { case xy_plane: ox=px+dist*sin(ang); oy=py-dist*cos(ang); @@ -1223,7 +1274,7 @@ void Config::genRadial(Mass **newMass, const Mass& parent, default: m_error="INTERNAL ERROR: invalid plane defined"; throw m_error; - } + } dx+=pdx; dy+=pdy; |