From 19bbd5822593c7198dd54eb0f5d38fa1ed8d8fb7 Mon Sep 17 00:00:00 2001 From: Ian C Date: Wed, 18 Aug 2010 14:38:34 +0000 Subject: Reformatted code and altered the attraction rule to something a bit more correct. Also added two tests to prove it works. --- glgrav.cpp | 298 +++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 191 insertions(+), 107 deletions(-) (limited to 'glgrav.cpp') diff --git a/glgrav.cpp b/glgrav.cpp index 8e7e6a7..95b00b4 100644 --- a/glgrav.cpp +++ b/glgrav.cpp @@ -84,21 +84,21 @@ static bool fog_light=false; static GLfloat fog_col[4]={0.0f,0.0f,0.0f,1.0f}; static struct - { - GLfloat x,y,z; - GLfloat ang; - GLfloat speed; - } camera={0.0f,0.0f,0.0f,0.0f,0.0f}; +{ + GLfloat x,y,z; + GLfloat ang; + GLfloat speed; +} camera={0.0f,0.0f,0.0f,0.0f,0.0f}; static struct - { - int mx,my; - bool lmb; - bool shift; - bool ctrl; - bool pgup; - bool pgdn; - } control; +{ + int mx,my; + bool lmb; + bool shift; + bool ctrl; + bool pgup; + bool pgdn; +} control; static void Reshape(int w,int h); static void Mouse(int b,int s, int x, int y); @@ -139,10 +139,10 @@ int main(int argc, char *argv[]) mass.clear(); if (!config.read(argc == 2 ? argv[1] : "dat",mass)) - { + { cerr << "Config file reading failed : \n" << config.error() << endl; exit(1); - } + } config.getCamera(camera.x,camera.y,camera.z,camera.ang); @@ -154,41 +154,45 @@ int main(int argc, char *argv[]) fog=config.enabled(Config::fog); if (config.particlesDefined()) - { + { GLfloat adec,amin; config.getParticles(adec,amin,particleSize); particles=new Particles(adec,amin); render_particles=true; - } + } else + { render_particles=false; + } if (config.sparksDefined()) - { + { int num,len; GLfloat adec,amin,delta; config.getSparks(num,adec,amin,len,delta); - sparks=new Sparks(num,adec,amin,len,delta); + sparks=new Sparks(num,adec,amin,len,delta); render_sparks=true; - } + } else + { render_sparks=false; + } config.windowSize(width,height); width=max(width,100); height=max(height,100); -# ifdef DEBUG - { +#ifdef DEBUG + { vector::size_type f; for(f=0;f::size_type f; switch (item) - { + { case DBG_DUMPOBJ: for(f=0;f(camera.z), x,y,z, 0.0,1.0,0.0); - } + } else - { + { glRotatef(360.0-camera.ang,0.0f,1.0f,0.0f); glTranslatef(-camera.x,-camera.y,-camera.z); - } + } } @@ -528,11 +550,11 @@ static void DrawMasses(Mass::DrawItem item) Mass lm; for(i=mass.begin();i!=mass.end();i++) - { + { SetView(); if (light) - { + { GLfloat light[4]; GLfloat pos[4]; string name; @@ -540,7 +562,7 @@ static void DrawMasses(Mass::DrawItem item) name=config.light(light); if (name!=i->getName() && findMass(mass,name,lm)) - { + { double x,y,z; lm.getPosition(x,y,z); @@ -557,17 +579,17 @@ static void DrawMasses(Mass::DrawItem item) if (fog) glEnable(GL_FOG); - } + } else - { + { if (!fog_light) glDisable(GL_FOG); glDisable(GL_LIGHTING); - } } + } i->draw(solid,texture,item); - } + } } @@ -575,10 +597,10 @@ static void Display(void) { Mass lm; - /* Looking up/down doesn't work yet... - */ + // Looking up/down doesn't work yet... + // if ((travel_as!=LOOK_NONE)&&(findMass(mass,look[travel_as],lm))) - { + { double x,y,z; lm.getPosition(x,y,z); @@ -586,19 +608,23 @@ static void Display(void) camera.x=static_cast(x); camera.y=static_cast(y); camera.z=static_cast(z); - } + } else - { + { GLPrint("Pos: %d,%d,%d",(int)camera.x,(int)camera.y,(int)camera.z); GLPrint("Angle: %d",(int)camera.ang); - } + } glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); if (fog) + { glEnable(GL_FOG); + } else + { glDisable(GL_FOG); + } DrawMasses(Mass::eMass); @@ -611,19 +637,21 @@ static void Display(void) glDisable(GL_TEXTURE_2D); if (fog) + { glEnable(GL_FOG); + } if (particles && render_particles) - { + { glPointSize(particleSize); particles->draw(); - } + } if (sparks && render_sparks) - { + { glPointSize(1); - sparks->draw(); - } + sparks->draw(); + } glDisable(GL_BLEND); @@ -655,35 +683,51 @@ static void Shatter(vector& mv, Mass& m,int no,double delta,int shield) texture=m.getTexture(id); for(f=0;f=m2.getMass()) - { + { win=&m1; lose=&m2; del[idx2]=true; - } + } else - { + { win=&m2; lose=&m1; del[idx1]=true; - } + } merge_mass=win->getMass()+lose->getMass(); win->reset(merge_mass); if (sparks) - { + { double x1,y1,z1; double x2,y2,z2; @@ -730,25 +774,25 @@ static void HandleCollision(Config::Collide collide, m2.getPosition(x2,y2,z2); sparks->add(x1+(x2-x1)/2, - y1+(y2-y1)/2, - z1+(z2-z1)/2); - } + y1+(y2-y1)/2, + z1+(z2-z1)/2); + } break; case Config::delta_merge: if (m1.getMass()>=m2.getMass()) - { + { win=&m1; lose=&m2; del[idx2]=true; - } + } else - { + { win=&m2; lose=&m1; del[idx1]=true; - } + } win->getDelta(odx,ody,odz); lose->getDelta(dx,dy,dz); @@ -762,7 +806,7 @@ static void HandleCollision(Config::Collide collide, win->reset(merge_mass,odx,ody,odz); if (sparks) - { + { double x1,y1,z1; double x2,y2,z2; @@ -770,9 +814,9 @@ static void HandleCollision(Config::Collide collide, m2.getPosition(x2,y2,z2); sparks->add(x1+(x2-x1)/2, - y1+(y2-y1)/2, - z1+(z2-z1)/2); - } + y1+(y2-y1)/2, + z1+(z2-z1)/2); + } break; @@ -780,23 +824,23 @@ static void HandleCollision(Config::Collide collide, config.getShatter(no,delta,min,max,shield); if (m1.getMass()=max) || (m2.getMass()=max)) - { + { HandleCollision(Config::merge,del,mv,m1,m2,idx1,idx2); return; - } + } Shatter(mv,m1,no,delta,shield); Shatter(mv,m2,no,delta,shield); if (sparks) - { + { double x1,y1,z1; double x2,y2,z2; @@ -804,9 +848,9 @@ static void HandleCollision(Config::Collide collide, m2.getPosition(x2,y2,z2); sparks->add(x1+(x2-x1)/2, - y1+(y2-y1)/2, - z1+(z2-z1)/2); - } + y1+(y2-y1)/2, + z1+(z2-z1)/2); + } del[idx1]=true; del[idx2]=true; @@ -818,7 +862,7 @@ static void HandleCollision(Config::Collide collide, default: break; - } + } } @@ -832,24 +876,38 @@ static void Update(void) new_mass=mass; for(f=0;fmass.size() || !del_mass[f]) + { mass.push_back(new_mass[f]); + } + } for(f=0;fadd(static_cast(x), static_cast(y), static_cast(z), @@ -868,70 +927,95 @@ static void Update(void) static_cast(py), static_cast(pz), r,g,b); - } + } + } mva=(control.mx-(width/2))/(100.0f*width/800.0); if (mva<-0.5 || mva>0.5) - { + { camera.ang-=mva; if (camera.ang<0.0) + { camera.ang+=360.0; + } else if (camera.ang>=360.0) + { camera.ang-=360.0; } + } if (control.pgup) + { camera.y+=10; + } if (control.pgdn) + { camera.y-=10; + } control.pgup=false; control.pgdn=false; if (control.lmb) - { + { if (camera.speed<20.0) + { camera.speed+=0.5; + } if (control.ctrl) + { camera.speed*=10; + } if (control.shift) - { + { camera.x+=(GLfloat)(sin(camera.ang*M_PI/180.0)*camera.speed); camera.z+=(GLfloat)(cos(camera.ang*M_PI/180.0)*camera.speed); - } + } else - { + { camera.x-=(GLfloat)(sin(camera.ang*M_PI/180.0)*camera.speed); camera.z-=(GLfloat)(cos(camera.ang*M_PI/180.0)*camera.speed); - } + } if (control.ctrl) + { camera.speed/=10; } + } else - { + { if (camera.speed>0.0) + { camera.speed-=0.5; + } if (camera.speed<0.0) + { camera.speed+=0.5; } + } frame++; if (sparks) + { sparks->update(); + } if (particles) + { particles->update(); + } if (!frame_skip || (frame%frame_skip)==0) + { glutPostWindowRedisplay(win); + } } -- cgit v1.2.3