proj home

Files   Classes   Functions   Hierarchy  

mazedisp03.cpp

Go to the documentation of this file.
00001 #include <cassert>
00002 #include <sstream>
00003 #include <string>
00004 using namespace std;
00005 
00006 #include <fnobjTfn.h>
00007 #include <gobj.h>
00008 #include <mazedisp03.h>
00009 #include <pointsdisplay.h>
00010 #include <stringconvert.h>
00011 #include <typedefs.h>
00012 #include <zprmouse.h>
00013 
00014 
00015 mazedisp03::mazedisp03( mazegameD2state01 & mg_ ) 
00016   : mg(mg_), mousecallback(*this,&mazedisp03::mouseevent), zm(0)
00017 {
00018 }
00019 
00020 mazedisp03::~mazedisp03()
00021 {
00022   delete zm;
00023   zm=0;
00024 }
00025 
00026 void mazedisp03::game01default()
00027 {
00028   dx=0.1;
00029   displaycellid=false;
00030   pipes=false;
00031   pipecolor=point3<double>(1.0,0.0,0.0);
00032   walls=true;
00033   wallcolor=point3<double>(0.0,0.0,1.0);
00034   origin=point2<double>(0.0,0.0);
00035   backgroundcolor=point3<double>(0.0,0.0,0.0);
00036   endpointratio=0.25;
00037   endpointcolor=point3<double>(0.0,1.0,0.0);
00038   currentposcolor=point3<double>(0.0,1.0,205.0/255.0);
00039 }
00040 
00041 void mazedisp03::draw()
00042 {
00043 //cout << "mazedisp03::draw()" << endl;
00044   gx.draw();
00045 }
00046 
00047 void mazedisp03::construct()
00048 {
00049   //gx.push(staticgraphicsindex,*this,&mazedisp03::staticgraphics);
00050   gx.pushdeferred(staticgraphicsindex,*this,&mazedisp03::staticgraphics);
00051   gx.update();
00052 
00053   uint index;
00054   
00055   gx.pushimmediate(index,gobjcallbackcreatenew(*this,&mazedisp03::currentposdraw));
00056 
00057 
00058 // Turned off mouse button
00059   bp02 = new buttonpanel02(zm);
00060 //  gx.pushimmediate(index,bp02);
00061 
00062 }
00063 
00064 //void mazedisp03::construct()
00065 //{
00066 /*
00067   gx.graphicsDeferred.globalpush();
00068   staticgraphics();
00069   gobjContainer::globalpop();
00070   gx.update();
00071 */
00072 //}
00073 
00074 /*
00075 void mazedisp03::staticgraphics()
00076 {
00077   gobjpush( new gobjglPushMatrix() );
00078   gobjpush( new gobjglTranslated(point3<double>()) );
00079   gobjpush( new gobjglutSolidSphere(dx*3.0,35,35) );
00080   gobjpush( new gobjglPopMatrix() );
00081 }
00082 */
00083 
00084 
00085 point2<double> const mazedisp03::cellmidpoint(uintc id) const
00086 {
00087   uint k = id-1;
00088   uint m=mg.mz.dim[0];
00089   uint n=mg.mz.dim[1];
00090 
00091   point2<uint> pos1;
00092   point2<double> pos2;
00093 
00094   pos1.x = k % n;
00095   pos1.y = (m*n-1)/n-k/n;
00096   pos2.x = dx*pos1.x;
00097   pos2.y = dx*pos1.y;
00098 
00099   return pos2+point2<double>(dx*0.5,dx*0.5);
00100 }
00101 
00102 void mazedisp03::staticgraphics()
00103 {
00104   assert(dx!=0.0);
00105   assert(mg.mz.valid());
00106   assert(mg.mz.vi.size()==(1+mg.mz.dim[2]));
00107 
00108   gobjpush(new gobjglDisable(GL_LIGHTING));
00109   gobjpush(new gobjglBegin(GL_LINES));
00110 
00111   uint m=mg.mz.dim[0];
00112   uint n=mg.mz.dim[1];
00113 
00114   uint id;
00115   point2<uint> pos1;
00116   point2<double> pos2;
00117   uint K=m*n-1;
00118 
00119   if (walls)
00120   {
00121     gobjpush(new gobjglColor3d(wallcolor));
00122 
00123     for (uint k=0; k<mg.mz.dim[2]; ++k)
00124     {
00125       id=k+1;
00126       pos1.x = k % n;
00127       pos1.y = K/n-k/n;
00128       pos2.x = dx*pos1.x;
00129       pos2.y = dx*pos1.y;
00130       celldraw(mg.mz.vi[id],pos2);    
00131     }
00132   } 
00133 
00134   if (pipes)
00135   {
00136     gobjpush(new gobjglColor3d(pipecolor));
00137     for (uint k=0; k<mg.mz.dim[2]; ++k)
00138     {
00139       id=k+1;
00140       pos1.x = k % n;
00141       pos1.y = K/n-k/n;
00142       pos2.x = dx*pos1.x;
00143       pos2.y = dx*pos1.y;
00144       celldraw2(mg.mz.vi[id],pos2);
00145     }
00146   }
00147   
00148   gobjpush(new gobjglEnd()); 
00149 
00150   // Each cell has a midpoint in the maze, calculate it.
00151   vector< point2<double> > pi;
00152   pi.push_back(point2<double>());
00153 
00154   for (uint k=0; k<mg.mz.dim[2]; ++k)
00155   {
00156     pi.push_back(cellmidpoint(k+1));
00157 /*
00158     id=k+1;
00159     pos1.x = k % n;
00160     pos1.y = K/n-k/n;
00161     pos2.x = dx*pos1.x;
00162     pos2.y = dx*pos1.y;
00163 
00164     pi.push_back(pos2+point2<double>(dx*0.5,dx*0.5));
00165 */
00166   }
00167 
00168   if (displaycellid)
00169   {
00170     assert(gobjContainer::global);
00171     gobjpush(new gobjglColor3f(point3<float>(1.0,0.0,0.0)));
00172     pointsdisplay2D< point2<double> > pd(*gobjContainer::global,pi,false,true,true);
00173   }
00174 
00175   gobjpush( new gobjglEnable(GL_LIGHTING) ); 
00176   gobjpush( new gobjglColor3d(endpointcolor) );
00177 
00178   //double endpointsratio=0.25;
00179 
00180   gobjpush( new gobjglPushMatrix() );
00181   gobjpush( new gobjglTranslated( pi[mg.gamestart] ) );
00182   gobjpush( new gobjglutSolidSphere(dx*endpointratio,35,35) );
00183   gobjpush( new gobjglPopMatrix() );
00184 
00185   gobjpush( new gobjglPushMatrix() );
00186   gobjpush( new gobjglTranslated( pi[mg.gamefinish] ) );
00187   gobjpush( new gobjglutSolidSphere(dx*endpointratio,35,35) );
00188   gobjpush( new gobjglPopMatrix() );
00189 
00190 }
00191 
00192 
00193 void mazedisp03::celldraw
00194 ( 
00195   cellD2<uint> const & x,
00196   point2<double> const & p00
00197 )
00198 {
00199   point2<double> p10 = p00;
00200   p10.x += dx;
00201   point2<double> p01 = p00;
00202   p01.y += dx;
00203   point2<double> p11 = p00;
00204   p11.x += dx;
00205   p11.y += dx;
00206 
00207   if (x.ni[1]==0)
00208   {
00209     gobjpush(new gobjglVertex2d(p10));
00210     gobjpush(new gobjglVertex2d(p11));
00211   }
00212 
00213   if (x.ni[3]==0)
00214   {
00215     gobjpush(new gobjglVertex2d(p01));
00216     gobjpush(new gobjglVertex2d(p00));
00217   }
00218   
00219   if (x.ni[0]==0)
00220   {
00221     gobjpush(new gobjglVertex2d(p01));
00222     gobjpush(new gobjglVertex2d(p11));
00223   }
00224   
00225   if (x.ni[2]==0)
00226   {
00227     gobjpush(new gobjglVertex2d(p00));
00228     gobjpush(new gobjglVertex2d(p10));
00229   }
00230 }
00231 
00232 void mazedisp03::celldraw2
00233 ( 
00234   cellD2<uint> const & x,
00235   point2<double> const & p00
00236 )
00237 {
00238   double dx2 = dx*0.5;
00239 
00240   point2<double> p3 = p00;
00241   p3.y += dx2;
00242   point2<double> p1 = p00;
00243   p1.x += dx;
00244   p1.y += dx2;
00245   point2<double> p0 = p00;
00246   p0.x += dx2;
00247   p0.y += dx;
00248   point2<double> p2 = p00;
00249   p2.x += dx2;
00250 
00251   point2<double> pcenter = p00;
00252   pcenter.x += dx2;
00253   pcenter.y += dx2;
00254 
00255   if (x.ni[1]!=0)
00256   {
00257     gobjpush(new gobjglVertex2d(pcenter));
00258     gobjpush(new gobjglVertex2d(p1));
00259   }
00260 
00261   if (x.ni[3]!=0)
00262   {
00263     gobjpush(new gobjglVertex2d(pcenter));
00264     gobjpush(new gobjglVertex2d(p3));
00265   }
00266   
00267   if (x.ni[0]!=0)
00268   {
00269     gobjpush(new gobjglVertex2d(pcenter));
00270     gobjpush(new gobjglVertex2d(p0));
00271   }
00272   
00273   if (x.ni[2]!=0)
00274   {
00275     gobjpush(new gobjglVertex2d(pcenter));
00276     gobjpush(new gobjglVertex2d(p2));
00277   }
00278 }
00279 
00280 stringc mazedisp03::settings() const
00281 {
00282   string options="";
00283   string s;
00284 
00285   options += ( "dx=" + stringconvert::tostring(dx) + " ");
00286   options += ( "id=" + stringconvert::tostring(displaycellid) + " ");
00287   options += ( "pipes=" + stringconvert::tostring(pipes) + " ");
00288   options += ( "pipecolor=" + stringconvert::findandreplacefn(stringto(pipecolor*255.0)," ",",") + " ");
00289   options += ( "walls=" + stringconvert::tostring(walls) + " ");
00290   options += ( "wallcolor=" + stringconvert::findandreplacefn(stringto(wallcolor*255.0)," ",",") + " ");
00291   options += ( "origin=" + stringconvert::findandreplacefn(origin," ",",") + " ");
00292   options += ( "backgroundcolor=" + stringconvert::findandreplacefn(stringto(backgroundcolor*255.0)," ",",") + " ");
00293   options += ( "endpointratio=" + stringconvert::tostring(endpointratio) + " ");
00294   options += ( "endpointcolor=" + stringconvert::findandreplacefn(stringto(endpointcolor*255.0)," ",",") + " ");
00295   options += ( "currentposcolor=" + stringconvert::findandreplacefn(stringto(currentposcolor*255.0)," ",",") + " ");
00296 
00297   return options;
00298 }
00299 
00300 void mazedisp03::currentposdraw()
00301 {
00302   assert(mg.path.empty()==false);
00303   uint cp = mg.path[mg.path.size()-1];
00304 
00305   point2<double> cpx = cellmidpoint(cp);
00306 
00307   glPushAttrib(GL_CURRENT_BIT);
00308   glColor3d(currentposcolor.x,currentposcolor.y,currentposcolor.z);
00309 
00310   // Assume model view
00311   glPushMatrix();
00312   glTranslated(cpx.x,cpx.y,0.0);
00313   glutSolidSphere(dx*endpointratio,35,35);
00314   glPopMatrix();
00315 
00316   for (uint i=0; i<mg.path.size(); ++i)
00317   {
00318     point2<double> cppos = cellmidpoint(mg.path[i]);
00319     glPushMatrix();
00320     glTranslated(cppos.x,cppos.y,0.0);
00321     glutSolidSphere(dx*endpointratio*0.5,35,35);
00322     glPopMatrix();
00323   }
00324 
00325   glPopAttrib();
00326 
00327   //f01();
00328 }
00329 
00330 void mazedisp03::f01()
00331 {
00332   assert(zm);
00333 
00334   // Code taken from tutorial
00335   //   http://basic4gl.wikispaces.com/2D+Drawing+in+OpenGL
00336 
00337   glMatrixMode(GL_PROJECTION);
00338   glPushMatrix();
00339   glLoadIdentity();
00340   glOrtho(0,zm->zz.width,zm->zz.height,0,0,1);
00341   glDisable(GL_DEPTH_TEST);
00342   glMatrixMode(GL_MODELVIEW);
00343   glPushMatrix();
00344   glLoadIdentity();
00345   glTranslatef(0.375,0.375,0);
00346 
00347   int radius=5;
00348   int x1=320;
00349   int y1=240;
00350 
00351 //cout << "Drawing in 2D" << endl;
00352 
00353   glPushAttrib(GL_CURRENT_BIT);
00354   glPushAttrib(GL_LIGHTING_BIT);
00355   glDisable(GL_LIGHTING);
00356   glColor3f(1.0,0.0,0.0);
00357 
00358   double rad=3.141592*2.0/360.0;
00359 
00360   glBegin(GL_TRIANGLE_FAN);
00361   glVertex2f(x1,y1);
00362   for (double angle=0; angle<=360; angle+=2)
00363   {
00364     point2<double> x2(x1+ sin(rad*angle)*radius, y1 + cos(rad*angle)*radius);
00365     cout << SHOW(x2) << " ";
00366     glVertex2d(x2.x,x2.y);
00367     //glVertex2f(x1 + sin(rad*angle) * radius, y1 + cos(rad*angle) * radius);
00368     //glVertex2f(x1 + sin(angle)*radius, y1 + cos(angle)*radius);
00369   }
00370   glEnd();
00371 
00372 cout << endl;
00373 
00374   glPopAttrib();
00375   glPopAttrib();
00376 
00377   // Returning to previous state.
00378   glPopMatrix();
00379   glMatrixMode(GL_PROJECTION);
00380   glPopMatrix();
00381   glMatrixMode(GL_MODELVIEW);
00382 
00383   glEnable(GL_DEPTH_TEST);
00384 }
00385 
00386 
00387 void mazedisp03::mouseevent()
00388 {
00389   assert(zm);
00390   if (zm==0)
00391     return;
00392 
00393   zm->update();
00394 
00395   //cout << "mouse() " << zm->mouse() << "  " << zm->world() << endl;
00396 
00397 // Turned off button code because of bad usablity
00398 /*
00399   assert(bp02);
00400   bool res(false);
00401   uint index;
00402   bp02->process(res,index,zm->mouseint());
00403 
00404   if (res==false)
00405     return;
00406 
00407   assertreturnfalse(index<4);
00408 
00409   mg.currentmove(index);
00410 
00411   glutPostRedisplay();
00412 */
00413 }
00414 
00415 
00416 

Generated on Fri Mar 4 00:49:29 2011 for Chelton Evans Source by  doxygen 1.5.8