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

/home/rdbrown/doxygen/cse125g1/src/game/client/testgs.cpp

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <math.h>
00004 #include <ctype.h>
00005 
00006 #include "testgs.h"
00007 
00008 static Camera camera;
00009 static int mousexPos, mouseyPos;
00010 static LevelGen *levelGen;
00011 
00012 
00013 ArrowSystem arrows;
00014 Axes axes;
00015 PlayerControl player;
00016 WindowControl windowControl;
00017 Fog fog;
00018 Timer timer;
00019 Lighting lighting;
00020 
00021 
00022 char fpsMessage[50];
00023 int fpsCounter = 0;
00024 
00025 static void backToMenu() {
00026   exit(0);
00027 }
00028 
00029 static void checkInput ()
00030 {
00031   if (InputKeyIsDown('w'))
00032     camera.Forward();
00033   if (InputKeyIsDown('a'))
00034     camera.Left();
00035   if (InputKeyIsDown('s'))
00036     camera.Backward();
00037   if (InputKeyIsDown('d'))
00038     camera.Right();
00039 
00040 }
00041 
00042 static void TriangleMouseMotion(int xPos, int yPos)
00043 {
00044         if(mousexPos < 0 && mouseyPos < 0)
00045                 mousexPos = xPos, mouseyPos = yPos;
00046 
00047         int dx = xPos - mousexPos;
00048         int dy = yPos - mouseyPos;
00049 
00050         mousexPos = xPos;
00051         mouseyPos = yPos;
00052 
00053         if(InputMouseIsDown(GLUT_LEFT_BUTTON))
00054         {
00055                 camera.RotateLR(((float)dx)/300.0);
00056                 camera.RotateUD(-((float)dy)/300.0);
00057         }
00058         
00059 }
00060 
00061 static void redrawMap() {
00062   levelGen->createMap((int)time(NULL));
00063 }
00064 
00065 void
00066 ToggleArrowStorm(void)
00067 {
00068         static bool doShower = false;
00069         doShower = !doShower;
00070         //arrows.DoArrowShower(doShower);
00071 
00072 }
00073 
00074 void
00075 MouseMotion(int x, int y)
00076 {
00077         player.MouseMotion(x,y);
00078 }
00079 
00080 
00081 void TestGS::init() {
00082         glutKeyboardFunc( InputSystemKeyDown );
00083         glutKeyboardUpFunc ( InputSystemKeyUp );
00084 
00085   mousexPos = -1;
00086   mouseyPos = -1;
00087   InputInit();
00088         //arrows.Init(700, 1.0f, 0.0);
00089         axes.Init(5.0f);
00090         
00091         windowControl.Init();
00092         player.Init(0.0f,1.0f,30.0f,&arrows);
00093         InputSetMouseMotion(MouseMotion);
00094         timer.Init();
00095 
00096         InputSetKeyDown('t', ToggleArrowStorm);
00097 
00098         glPointSize(2);
00099         glLineWidth(2);
00100 
00101         glEnable(GL_NORMALIZE);
00102         glShadeModel(GL_SMOOTH);
00103 
00104 
00105   levelGen = new LevelGen(TEXTURE_DIRT, TEXTURE_WALL, TEXTURE_DIRT);
00106 
00107   glEnable( GL_CULL_FACE );
00108 
00109   redrawMap();
00110   
00111   //InputSetMouseMotion(TriangleMouseMotion);
00112   InputSetKeyDown('x', backToMenu);
00113   InputSetKeyDown('r', redrawMap);
00114   InputSetKeyDown('i', toggleMiniMap);
00115 
00116   camera.Reset();
00117         glClearColor(0.3, 0.3, 0.8, 1.0);
00118         //fog.Init();
00119 }
00120 
00121 void TestGS::display()
00122 {
00123         // erase the background:                
00124         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00125 
00126         // enable z-buffer
00127         glEnable( GL_DEPTH_TEST );
00128 
00129 
00130         /* set the viewing volume:                                      */
00131         /* remember that the eye is at the origin looking in -Z         */
00132         /* remember that the Z values are actually                      */
00133         /* given as DISTANCES IN FRONT OF THE EYE                       */
00134         //Do Camera Stuff
00135         glMatrixMode( GL_PROJECTION );
00136         glLoadIdentity();
00137         gluPerspective( 60., 800.0/600.0,       0.1, 100. );
00138 
00139         glMatrixMode( GL_MODELVIEW );
00140         glLoadIdentity();
00141 
00142         float Yrot, Xrot, PosX, PosY, PosZ;
00143         Yrot = player.GetYrot();
00144         Xrot = player.GetXrot();
00145         PosX = player.GetPosX();
00146         PosY = player.GetPosY();
00147         PosZ = player.GetPosZ();
00148         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. );
00149 
00150         fog.Init();
00151         lighting.Init();
00152         
00153 
00154 
00155         fog.Enable();
00156         lighting.Enable();
00157         axes.Draw();
00158 
00159         /* place the objects into the viewing volume:                   */
00160         //Draw guy
00161         glPushMatrix();
00162                 glTranslatef(PosX, PosY, PosZ);
00163                 glRotatef( -Yrot, 0., 1., 0. );
00164                 glColor3f( 1.0, 1.0, 1.0);
00165                 glutSolidCube(1.0);
00166         glPopMatrix();
00167 
00168         //Draw Crummy Tree
00169         glPushMatrix();
00170                 glTranslatef(20.0f, 30.0, 20.0f);
00171                 glScalef(1.0, 30.0, 1.0);
00172                 glColor3f(0.56f,0.27f,.07f);
00173                 glutSolidCube(4.0);
00174         glPopMatrix();
00175 
00176         //Draw crummy target
00177         glPushMatrix();
00178                 glTranslatef(0.0f, 20.0, 21.0f);
00179                 glColor3f(0.56f,0.27f,1.0f);
00180                 glutSolidSphere(4.0,15,15);
00181         glPopMatrix();
00182 
00183         if (1.0/(timer.GetTimeChange()) < 30)
00184         {
00185                 timer.Update();
00186                 return;
00187         }
00188 
00189         glPushMatrix();
00190         glScalef(15.0,15.0,15.0);
00191         //Draw level
00192         levelGen->draw();
00193         glPopMatrix();
00194 
00195         // makes arrows + axis immune to lighting
00196         lighting.Disable();
00197         glDisable(GL_LIGHTING);
00198 
00199         /* this is here because we are going to do scaling: */
00200         // XXX: Optimization-> place outside display loop
00201         glEnable(GL_NORMALIZE);
00202         glShadeModel(GL_SMOOTH);
00203 
00204         glLineWidth(2.0);
00205         //arrows.Update(timer.GetTimeChange());
00206         //arrows.Draw();
00207         //arrows.stickySphere(Vector3(0.0,20.0,21.0),4);
00208         //arrows.stickyCylinder(Vector3(20.0f,0.0f,20.0f), 2.0f, 60.1f, -10.1f);
00209 
00210         fog.Disable();
00211         player.DrawTarget();
00212 
00213         fpsCounter++;
00214         if (fpsCounter >= 9) {
00215                 fpsCounter = 0;
00216                 sprintf(fpsMessage, "fps: %f", 1.0/(timer.GetTimeChange()) );
00217         }
00218 
00219         draw2dString(5.0f, 5.0f, fpsMessage);
00220         //drawMiniMap(200, 20, PosX, PosY, PosZ, Yrot, arrows); // XXX: Player player, arrows, trees, rocks, bushes
00221         timer.Update();
00222         glutSwapBuffers();
00223 }
00224 
00225 
00226 void TestGS::update() {
00227         player.HandleInput(timer.GetTimeChange());
00228         windowControl.Update();
00229         glutPostRedisplay();
00230 }
00231 
00232 void TestGS::exit() {
00233   delete levelGen;
00234   levelGen = NULL;
00235 }

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