Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

/home/rdbrown/doxygen/cse125g1/src/modules/arrows/arrowstest/ArrowsTest.cpp

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <math.h>
00004 #include <ctype.h>
00005 
00006 #ifdef WIN32
00007 #include <windows.h>
00008 const float M_PI = { 3.14159265f };
00009 #endif
00010 
00011 #include "axes/axes.h"
00012 #include "arrows/arrows.h"
00013 #include "input/input.h"
00014 #include "playerControl/playerControl.h"
00015 #include "fog/fog.h"
00016 #include "windowControl/windowControl.h"
00017 #include "timer/timer.h"
00018 #include "stringDraw/stringDraw.h"
00019 #include "lighting/lighting.h"
00020 #include "opengl.h"
00021 
00022 
00023 /* Constants and Defines */
00024 const char *WINDOWTITLE = { "Robin Hood and His Merry Men" };
00025 #define ESCAPE          0x1b
00026 const int INIT_WINDOW_SIZE = { 600 };
00027 const float SCLFACT = { 0.005f };
00028 const float MINSCALE = { 0.05f };
00029 /* active mouse buttons (or them together):                             */
00030 
00031 const int LEFT   = { 4 };
00032 const int MIDDLE = { 2 };
00033 const int RIGHT  = { 1 };
00034 
00035 
00036 
00037 /* window background color (rgba):                                      */
00038 const float BACKCOLOR[] = { 0.2, 0.2, 1., 0. };
00039 
00040 
00041 
00045 int     ActiveButton;           /* current button that is down          */
00046 int     Debug;                  /* ON means print debug info            */
00047 int     LeftButton;             /* either ROTATE or SCALE               */
00048 int     MainWindow;             /* window id for main graphics window   */
00049 float   Scale;          /* scaling factors                      */
00050 
00051 int PointsOn;
00052 int LightOn;
00053 int AeroOn;
00054 int FontType;
00055 
00056 
00057 
00058 
00059 ArrowSystem arrows;
00060 Axes axes;
00061 PlayerControl player;
00062 WindowControl windowControl;
00063 Fog fog;
00064 Timer timer;
00065 Lighting lighting;
00066 
00067 //float eyeTargetX = 0.0;
00068 //float eyeTargetY = 0.0;
00069 int fpsCounter = 0;
00070 char fpsMessage[50];
00071 
00072 
00076 void    Display( void );
00077 void    DoRasterString( float, float, float, char * );
00078 void    DoStrokeString( float, float, float, float, char * );
00079 void    InitGraphics( void );
00080 //void  Keyboard( unsigned char, int, int );
00081 
00082 void    Reset( void );
00083 void    Resize( int, int );
00084 void    Visibility( int );
00085 //void  KeyboardSpecial( int c, int x, int y );
00086 
00087 
00088 
00092 int
00093 main( int argc, char *argv[] )
00094 {
00095 //INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
00096 //{
00097         glutInit( &argc, argv );
00098 
00099         InitGraphics();
00100         srand(12);
00101 
00102         /* create the display structures that will not change:          */
00103         Reset();
00104 
00105         glutMainLoop();
00106 
00107         /* this is here to make the compiler happy:                     */
00108         return 0;
00109 }
00110 
00111 
00112 void
00113 MouseMotion(int x, int y)
00114 {
00115         player.MouseMotion(x,y);
00116 }
00117 
00118 
00119 
00123 void
00124 Display( void )
00125 {
00126 
00127         /* set which window we want to do the graphics into:            */
00128         // probably could be moved to init or somewhere else
00129         glutSetWindow( MainWindow );
00130 
00131 
00132         /* erase the background:                                        */
00133         //glDrawBuffer( GL_BACK );
00134         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00135         glEnable( GL_DEPTH_TEST );
00136 
00137 
00138         /* set the viewing volume:                                      */
00139         /* remember that the eye is at the origin looking in -Z         */
00140         /* remember that the Z values are actually                      */
00141         /* given as DISTANCES IN FRONT OF THE EYE                       */
00142 
00143         // XXX: move to init
00144         glMatrixMode( GL_PROJECTION );
00145         glLoadIdentity();
00146         gluPerspective( 90., 1.,        0.1, 1000. );
00147 
00148         //Do Camera Stuff
00149         glMatrixMode( GL_MODELVIEW );
00150         glLoadIdentity();
00151         float Yrot, Xrot, PosX, PosY, PosZ;
00152         Yrot = player.GetYrot();
00153         Xrot = player.GetXrot();
00154         PosX = player.GetPosX();
00155         PosY = player.GetPosY();
00156         PosZ = player.GetPosZ();
00157         gluLookAt( PosX+2*sin((-Yrot-5)*(M_PI/180.0)), PosY+1.0, PosZ+2*cos((-Yrot-5)*(M_PI/180.0) ),     PosX, PosY+1.0-Xrot/15.0, PosZ,     0., 1., 0. );
00158 
00159 
00160 
00161         /* place the objects into the viewing volume:                   */
00162         fog.Enable();
00163         axes.Draw();
00164 
00165         // XXX: move to init
00166         glPointSize(2);
00167         glLineWidth(2);
00168 
00169         lighting.Enable();
00170 
00171         // XXX: Optimization-> place outside display loop
00172         glEnable(GL_NORMALIZE);
00173         glShadeModel(GL_SMOOTH);
00174 
00175 
00176         //Draw guy
00177         glPushMatrix();
00178                 glTranslatef(PosX, PosY, PosZ);
00179                 glRotatef( -Yrot, 0., 1., 0. );
00180                 glColor3f( 1.0, 1.0, 1.0);
00181                 glutSolidCube(1.0);
00182         glPopMatrix();
00183 
00184         //Draw Crummy Tree
00185         glPushMatrix();
00186                 glTranslatef(20.0f, 30.0, 20.0f);
00187                 glScalef(1.0, 30.0, 1.0);
00188                 glColor3f(0.56f,0.27f,.07f);
00189                 glutSolidCube(4.0);
00190         glPopMatrix();
00191 
00192         //Draw crummy target
00193         glPushMatrix();
00194                 glTranslatef(0.0f, 0.0, 21.0f);
00195                 glColor3f(0.56f,0.27f,1.0f);
00196                 glutSolidSphere(4.0,15,15);
00197         glPopMatrix();
00198 
00199         if (1.0/(timer.GetTimeChange()) < 30)
00200         {
00201                 timer.Update();
00202                 return;
00203         }
00204 
00205         if(true)
00206         {
00207                 glNormal3f( 0.0, 1.0, 0.0);
00208                 glColor3f( 0.0, 1.0, 0.0);
00209                 glBegin(GL_QUADS);
00210                         glVertex3f(-200.0f, -10.1f, -200.0f);
00211                         glVertex3f(-200.0f, -10.1f, 200.0f);
00212                         glVertex3f(200.0f, -10.1f, 200.0f);
00213                         glVertex3f(200.0f, -10.1f, -200.0f);
00214                 glEnd();
00215         }
00216 
00217 
00218         // makes arrows + axis immune to lighting
00219         lighting.Disable();
00220 
00221         arrows.Update(timer.GetTimeChange());
00222         arrows.Draw();
00223         arrows.stickySphere(Vector3(0.0,0.0,21.0),4);
00224         arrows.stickyCylinder(Vector3(20.0f,0.0f,20.0f), 2.0f, 60.1f, -10.1f);
00225 
00226         fog.Disable();
00227         player.DrawTarget();
00228 
00229         fpsCounter++;
00230         if (fpsCounter >= 9) {
00231                 fpsCounter = 0;
00232                 sprintf(fpsMessage, "fps: %f", 1.0/(timer.GetTimeChange()) );
00233         }
00234 
00235         timer.Update();
00236 
00237         draw2dString(5.0f, 5.0f, fpsMessage);
00238 
00239         /* swap the double-buffered framebuffers:                       */
00240         glutSwapBuffers();
00241 
00242         /* be sure the graphics buffer has been sent:                   */
00243         glFlush();
00244 }
00245 
00246 void
00247 Animate( void )
00248 {
00249         /* force a call to Display() next time it is convenient:        */
00250         player.HandleInput(timer.GetTimeChange());
00251         windowControl.Update();
00252         glutSetWindow( MainWindow );
00253         glutPostRedisplay();
00254 }
00255 
00256 
00257 void
00258 ToggleArrowStorm(void)
00259 {
00260         static bool doShower = false;
00261         doShower = !doShower;
00262         arrows.DoArrowShower(doShower);
00263 
00264 }
00265 
00266 
00271 void
00272 InitGraphics( void )
00273 {
00274         if( Debug )
00275           //fprintf( stderr, "InitGraphics\n" );
00276 
00277 
00278         /* setup the display mode:                                      */
00279         /* ( *must* be done before call to glutCreateWindow() )         */
00280         /* ask for color, double-buffering, and z-buffering:            */
00281 
00282         glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
00283 
00284 
00285         /* set the initial window configuration:                        */
00286         glutInitWindowSize( INIT_WINDOW_SIZE, INIT_WINDOW_SIZE );
00287 
00288 
00289         /* open the window and set its title:                           */
00290         MainWindow = glutCreateWindow( WINDOWTITLE );
00291         glutSetWindowTitle( WINDOWTITLE );
00292 
00293 
00294         /* setup the clear values:                                      */
00295         glClearColor( BACKCOLOR[0], BACKCOLOR[1], BACKCOLOR[2], BACKCOLOR[3] );
00296 
00297         //Good place to initialize stuff
00298         arrows.Init(700, 1.0f, -10.0);
00299         axes.Init(5.0f);
00300         fog.Init();
00301         windowControl.Init();
00302         player.Init(0.0f,0.0f,30.0f,&arrows);
00303         InputSetMouseMotion(MouseMotion);
00304         lighting.Init();
00305         timer.Init();
00306 
00307         InputSetKeyDown('t', ToggleArrowStorm);
00308 
00309         /* setup the callback routines:                                 */
00310         /* DisplayFunc -- redraw the window                             */
00311         /* ReshapeFunc -- handle the user resizing the window           */
00312         /* KeyboardFunc -- handle a keyboard input                      */
00313         /* MouseFunc -- handle the mouse button going down or up        */
00314         /* MotionFunc -- handle the mouse moving with a button down     */
00315         /* PassiveMotionFunc -- handle the mouse moving with a button up*/
00316         /* VisibilityFunc -- handle a change in window visibility       */
00317         /* EntryFunc    -- handle the cursor entering or leaving the window */
00318         /* SpecialFunc -- handle special keys on the keyboard           */
00319         /* SpaceballMotionFunc -- handle spaceball translation          */
00320         /* SpaceballRotateFunc -- handle spaceball rotation             */
00321         /* SpaceballButtonFunc -- handle spaceball button hits          */
00322         /* ButtonBoxFunc -- handle button box hits                      */
00323         /* DialsFunc -- handle dial rotations                           */
00324         /* TabletMotionFunc -- handle digitizing tablet motion          */
00325         /* TabletButtonFunc -- handle digitizing tablet button hits     */
00326         /* MenuStateFunc -- declare when a pop-up menu is in use        */
00327         /* IdleFunc -- what to do when nothing else is going on         */
00328         /* TimerFunc -- trigger something to happen every so often      */
00329 
00330 
00331         //void InputSystemKeyDown(char key, int xMouse, int yMouse);
00332         //void InputSystemKeyUp(char key, int xMouse, int yMouse);
00333         //void InputSystemMouseMotion(int x, int y);
00334         //void InputSystemMouseButton(int button, int state, int x, int y);
00335 
00336 
00337         glutKeyboardFunc( InputSystemKeyDown );
00338         glutKeyboardUpFunc ( InputSystemKeyUp );
00339         glutMouseFunc( InputSystemMouseButton );
00340         glutPassiveMotionFunc( InputSystemMouseMotion );
00341         glutMotionFunc( InputSystemMouseMotion );
00342 
00343         glutSetWindow( MainWindow );
00344         glutDisplayFunc( Display );
00345         glutReshapeFunc( Resize );
00346 
00347         glutVisibilityFunc( Visibility );
00348         glutEntryFunc( NULL );
00349 
00350         glutSpaceballMotionFunc( NULL );
00351         glutSpaceballRotateFunc( NULL );
00352         glutSpaceballButtonFunc( NULL );
00353         glutButtonBoxFunc( NULL );
00354         glutDialsFunc( NULL );
00355         glutTabletMotionFunc( NULL );
00356         glutTabletButtonFunc( NULL );
00357         glutMenuStateFunc( NULL );
00358         glutIdleFunc( Animate );
00359 
00360         glutTimerFunc( 0, NULL, 0 );
00361 }
00362 
00363 
00364 
00365 void
00366 Reset( void )
00367 {
00368         //AxesOn = true;
00369         Debug = false;
00370         LightOn = true;
00371         printf("Reset called\n");
00372         glutSetWindow( MainWindow );
00373         glutPostRedisplay();
00374 }
00375 
00376 
00377 
00382 void
00383 Resize( int width, int height )
00384 {
00385         if( Debug )
00386                 fprintf( stderr, "ReSize: %d, %d\n", width, height );
00387 
00388         /* don't really need to do anything since window size is        */
00389         /* checked each time in Display():                              */
00390 
00391         float vx, vy, v;
00392         /* handle resizing              */
00393         // XXX: could move to resize function
00394         vx = glutGet( GLUT_WINDOW_WIDTH );
00395         vy = glutGet( GLUT_WINDOW_HEIGHT );
00396         v = vx < vy ? vx : vy;                  /* minimum dimension    */
00397         //xl = ( vx - v ) / 2;
00398         //yb = ( vy - v ) / 2;
00399         glViewport( 0,0,vx,vy);//xl, yb,  v, v );
00400         //Xmouse = 0.0;//vx / 2.0;
00401         //Ymouse = 0.0;//vy / 2.0;
00402 
00403         //Xrot = ( ANGFACT*(150) );
00404         //Yrot = ( ANGFACT*(150) );
00405 
00406         //printf("resize xmouse, ymouse %d, %d\n", Xmouse, Ymouse);
00407         glutSetWindow( MainWindow );
00408         glutPostRedisplay();
00409 }
00410 
00411 
00412 
00413 
00418 void
00419 Visibility(int state)           /* GLUT_VISIBLE or GLUT_NOT_VISIBLE     */
00420 {
00421         if( Debug )
00422                 fprintf( stderr, "Visibility: %d\n", state );
00423 
00424         if( state == GLUT_VISIBLE )
00425         {
00426                 glutSetWindow( MainWindow );
00427                 glutPostRedisplay();
00428         }
00429         else
00430         {
00431                 /* could optimize by keeping track of the fact          */
00432                 /* that the window is not visible and avoid             */
00433                 /* animating or redrawing it ...                        */
00434         }
00435 }
00436 
00437 
00438 

Generated on Thu Aug 18 16:03:09 2005 for Robin Hood: Thieves & Knights by doxygen1.2.18