// returns next non-blank line (without newlines or comments) static string GetCleanLine(CFileHandler& fh) { string::size_type pos; while (true) { if (fh.Eof()) { return ""; // end of file } string line = GetLine(fh); pos = line.find_first_not_of(" \t"); if (pos == string::npos) { continue; // blank line } pos = line.find("//"); if (pos != string::npos) { line.erase(pos); pos = line.find_first_not_of(" \t"); if (pos == string::npos) { continue; // blank line (after removing comments) } } return line; } }
static void GetWord(CFileHandler& fh, std::string &s) { char a = fh.Peek(); while (a == ' ' || a == '\n' || a == '\r') { fh.Read(&a, 1); a = fh.Peek(); if (fh.Eof()) break; } s = ""; fh.Read(&a, 1); while (a != ',' && a != ' ' && a != '\n' && a != '\r') { s += a; fh.Read(&a, 1); if (fh.Eof()) break; } }
std::string CSpawnScript::LoadToken(CFileHandler& file) { std::string s; char c; while (!file.Eof()) { file.Read(&c,1); if(c>='0' && c<='z') break; } s += c; while (!file.Eof()) { file.Read(&c,1); if(c<'0' || c>'z') return s; s+=c; } return s; }
string CUnit3DLoader::GetWord(CFileHandler& fh) { char a=fh.Peek(); while(a==' ' || a=='\xd' || a=='\xa'){ fh.Read(&a,1); a=fh.Peek(); if(fh.Eof()) break; } string s=""; fh.Read(&a,1); while(a!=',' && a!=' ' && a!='\xd' && a!='\xa'){ s+=a; fh.Read(&a,1); if(fh.Eof()) break; } return s; }
// returns next line (without newlines) string SimpleParser::GetLine(CFileHandler& fh) { lineNumber++; char a; string s = ""; while (!fh.Eof()) { fh.Read(&a, 1); if (a == '\n') { break; } if (a != '\r') { s += a; } } return s; }
string CUnit3DLoader::GetLine(CFileHandler& fh) { string s=""; char a; fh.Read(&a,1); while(a!='\xd' && a!='\xa'){ s+=a; fh.Read(&a,1); if(fh.Eof()) break; } return s; }
static std::string GetLine(CFileHandler& fh) { std::string s = ""; char a; fh.Read(&a, 1); while (a!='\n' && a!='\r') { s += a; fh.Read(&a, 1); if (fh.Eof()) break; } return s; }
int CUnit3DLoader::ParseSub(CFileHandler& ifs, UnitModel &model,const string& filename,float3 offset,const string& treename) { string s; bool inComment=false; Animation* curAnim=0; int curAnimFrame=0; bool firstGeometryPass=false; if(geometryModels.find(treename)==geometryModels.end()){ geometryModels[treename]=new UnitModelGeometry(); UnitModelGeometry& geometry=*geometryModels[treename]; firstGeometryPass=true; geometry.normalBuffer=0; geometry.indexBuffer=0; geometry.numIndeces=0; geometry.numVerteces=0; } model.geometry=geometryModels[treename]; UnitModelGeometry& geometry=*model.geometry; geometry.radius=1; geometry.height=1; geometry.isAnimated=false; model.texCoordBuffer=0; while(ifs.Peek()!=EOF){ s=GetWord(ifs); MakeLow(s); if(s[0]=='/' && !inComment){ if(s[1]=='/'){ GetLine(ifs); continue; } if(s[1]=='*') inComment=true; } if(inComment){ for(int a=0;a<s.size()-1;++a) if(s[a]=='*' && s[a+1]=='/') inComment=false; if(inComment) continue; } if(ifs.Eof()) break; while((s.c_str()[0]=='/') || (s.c_str()[0]=='\n')){ s=GetLine(ifs); s=GetWord(ifs); MakeLow(s); if(ifs.Eof()) break; } if(s=="vertex"){ int num=atoi(GetWord(ifs).c_str()); float3 v; v.x=atof(GetWord(ifs).c_str()); v.y=atof(GetWord(ifs).c_str()); v.z=atof(GetWord(ifs).c_str()); v-=offset; if(firstGeometryPass){ if(curAnim==0){ while(geometry.vertex.size()<=num){ geometry.vertex.push_back(float3(0,0,0)); geometry.vertexNormal.push_back(float3(0,0,0)); } geometry.vertex[num]=v; } else { AnimFrame* af=&curAnim->frames[curAnimFrame]; while(af->vertex.size()<=num){ af->vertex.push_back(float3()); // model.vertexNormal.push_back(float3(0,0,0)); } af->vertex[num]=v; } } } else if(s=="quad"){ Quad q; QuadTex qt; for(int a=0;a<4;++a) q.verteces[a]=atoi(GetWord(ifs).c_str()); for(int a=0;a<8;++a) qt.texPos[0][a]=atof(GetWord(ifs).c_str()); qt.texName=GetWord(ifs); qt.teamTex=atoi(GetWord(ifs).c_str()); q.normalType=atoi(GetWord(ifs).c_str()); model.quadTex.push_back(qt); if(firstGeometryPass) geometry.quad.push_back(q); } else if(s=="tri"){ Tri t; TriTex tt; for(int a=0;a<3;++a) t.verteces[a]=atoi(GetWord(ifs).c_str()); for(int a=0;a<3;++a){ tt.texPos[0][a*2]=1-atof(GetWord(ifs).c_str()); tt.texPos[0][a*2+1]=1-atof(GetWord(ifs).c_str()); } tt.texName=GetWord(ifs); tt.teamTex=atoi(GetWord(ifs).c_str()); t.normalType=atoi(GetWord(ifs).c_str()); model.triTex.push_back(tt); if(firstGeometryPass) geometry.tri.push_back(t); } else if(s=="propeller"){ Propeller p; for(int a=0;a<3;++a) p.pos[a]=atof(GetWord(ifs).c_str()); p.size=atof(GetWord(ifs).c_str()); if(firstGeometryPass) geometry.propellers.push_back(p); } else if(s=="height"){ geometry.height=atof(GetWord(ifs).c_str()); } else if(s=="radius"){ geometry.radius=atof(GetWord(ifs).c_str()); } else if(s=="subobject"){ UnitModel* um=new UnitModel; model.subModels.push_back(um); um->name=GetWord(ifs); float3 off,rot; for(int a=0;a<3;++a) off[a]=atof(GetWord(ifs).c_str()); for(int a=0;a<3;++a) rot[a]=atof(GetWord(ifs).c_str()); um->team=model.team; char c[100]; sprintf(c,"%s%d",treename.c_str(),model.subModels.size()); ParseSub(ifs,*um,filename,off,c); um->geometry->offset=off; um->geometry->rotVector=rot; } else if(s=="endsubobject"){ return 1; } else if(s=="animation"){ string name=GetWord(ifs); if(name=="end") curAnim=0; else{ if(geometry.animations.find(name)==geometry.animations.end()){ geometry.animations[name]=new Animation; } curAnim=geometry.animations[name]; } curAnimFrame=0; geometry.isAnimated=true; } else if(s=="keyframe"){ string num=GetWord(ifs); if(num=="end"){ curAnimFrame=0; } else { curAnimFrame=atoi(num.c_str()); float length=atoi(GetWord(ifs).c_str())/25.0f*30.0f; if(curAnim){ while(curAnim->frames.size()<=curAnimFrame){ curAnim->frames.push_back(AnimFrame()); } curAnim->frames[curAnimFrame].length=length; } } } else { if(s!="") (*info) << "Unknown token " << s.c_str() << " in " << filename.c_str() << "\n"; } } return 1; }