/*! -------------------------------------------------------------------- \file game.cpp \brief Encapsulate a game \author James Kuffner \date 2002-09-17 // ------------------------------------------------------------------*/ #ifndef IGNORE_DEPEND #include #include #include #include #include #include #include #endif #if HAVE_PIC_LIB #include "pic.h" #endif #include "intro.h" #include "texturemap.h" //! default window title const char* Intro::WINDOW_TITLE = "Chobits"; //! instance pointer for callbacks Intro* Intro::s_pIntro = NULL; //! snapshot default filename const char* Intro::SNAPSHOT_FILENAME = "snapshot.jpg"; const char* Intro::INTROFILE = "data/intro/intro.txt"; //! helper function to read strings from a stream inline bool READ_STRING(istream &is, char* pString) { is >> pString; return is.good(); } /* #define menux -0.57 #define menuy 0.099 #define menuxsize 0.5 #define menuysize 0.15 #define menuincrement 0.16 #define menux 50 #define menuy 150 #define menuxsize 40 #define menuysize 100 #define menuincrement 50*/ //--------------------------------------------------------------------- // Intro // // Method: Constructor // //--------------------------------------------------------------------- Intro::Intro() { // initialize instance pointer for callbacks s_pIntro = this; mainWindow = -1; iWindowWidth = 0; iWindowHeight = 0; starttime=time(0);//glutGet wasnt working. if it was it would be =0; timeElapsed = 0; skipintro=false; introstate=1; menuitem=0; menureturned=false; } //--------------------------------------------------------------------- // Intro // // Method: Destructor // //--------------------------------------------------------------------- Intro::~Intro() { if (mainWindow > 0) glutDestroyWindow(mainWindow); } bool Intro::ReadIntro(const char *introfilename) { cerr << "Reading intro description file "<>startmenuimage; is>>numintroframes; //menu stuff is>>menux; is>>menuy; is>>menuxsize; is>>menuysize; is>>menuincrement; for (int x=0; x>introTimes[x]; if (!READ_STRING(is, introFilenames[x])) { cerr << "ERROR" << endl; return false; } } is>>introendtime; return true; } //--------------------------------------------------------------------- // Intro // Method: Init // //! set up game and initialize data structures // //--------------------------------------------------------------------- bool Intro::Init(Game *gameptr) { // parse level description file for (int x=0; x=introTimes[introstate]) { cout<<"next state"<30)||(skipintro)) {//intro is over //first do the startup screen-load that image, switch to startup screen input and rendering (add it to existing functions, enabled with an if skipintro) skipintro=true; if (introstate>=0) { LoadNewFrame(startmenuimage); introstate=-1;//if -1, we're at the startup menu } if (menureturned) { if (menuitem==3) exit(0); if (menuitem==2) { //do the options screen here menureturned=false; } else { //after the startup screen returns a mode: //(it has single and multi that return a mode, an options that run the options pause screen and then return to the startup screen, and exit that exit(0) //change the callbacks to the main game stuff //s_pGame->DisplayCB() is the new function, for example //keep redisplay until after level loading-just draw a loading screen for now //change input, reshape after determining level info //change idle to the class gameloop initializer // set up the primary callbacks //glutReshapeFunc(s_pGame->ReshapeCB);//change this along with display //LoadNewFrame("./loading.tif");//now Game will do this //glutDisplayFunc(s_pGame->DisplayCB);//disabled-switch it from Game->idleCB s_pGame->Init(mainWindow,iWindowWidth,iWindowHeight);//Init is called by Intro since intro knows how the window was set up. Intro is declared as a friend, so after the StartGame call only Game has access to the window data glutReshapeFunc(s_pGame->ReshapeCB);//change this along with display s_pGame->StartGame(menuitem,0); //glutIdleFunc(s_pGame->IdleCB);//this transfers control to Game //glutDisplayFunc(s_pGame->DisplayCB); //pass the parameters //s_pGame->gamemode=menuitem; // keyboard input callbacks /* glutKeyboardFunc(KeyDownCB); glutKeyboardUpFunc(KeyUpCB); glutSpecialFunc(SKeyDownCB); glutSpecialUpFunc(SKeyUpCB); glutIgnoreKeyRepeat(GL_TRUE); */ } } } glutPostRedisplay(); } //--------------------------------------------------------------------- // Intro // Method: _ReshapeWindow // //! reshape the main window // //--------------------------------------------------------------------- void Intro::ReshapeWindow(int w, int h) { if (w <= MIN_WINDOW_SIZE || h <= MIN_WINDOW_SIZE) { cerr << "Intro::Reshape - bad window size: " << w << ", " << h << endl; return; } // reset window size glutSetWindow(mainWindow); glViewport(0,0,w,h); iWindowWidth = w; iWindowHeight = h; gluPerspective(65,1,0.1,100);//fov, aspect,near,farclip /* // aspect ratio for each horizontal split-screen window //_iHalfHeight = h/2; float aspect = ((GLfloat)w / (GLfloat)_iHalfHeight); _cameraTop.SetAspectRatio(aspect); _cameraBot.SetAspectRatio(aspect); */ } //--------------------------------------------------------------------- // Intro // Method: _ProcessKeyDown // //! process keyboard down events // //--------------------------------------------------------------------- void Intro::ProcessKeyDown(unsigned char key, int w, int h) { switch (key) { case 27: case 'q': exit(0); break; case GLUT_KEY_UP: case 'w': if (skipintro) if (menuitem>0) menuitem--; break; case GLUT_KEY_DOWN: case 's': if (skipintro) if (menuitem<3) menuitem++; break; case ' ': cerr << "Saving screen snapshot to: " << SNAPSHOT_FILENAME << endl; CaptureImage(SNAPSHOT_FILENAME); break; case 13://return if (skipintro) {menureturned=true; cerr<<"Menu Selection Made"<= 0; i--) { glReadPixels(0, iWindowHeight-1-i, iWindowWidth, 1, GL_RGB, GL_UNSIGNED_BYTE, &in->pix[i*in->nx*in->bpp]); } // write the result bool bResult = jpeg_write((char *)filename, in); pic_free(in); if (!bResult) cerr << "Intro::_CaptureImage : Error writing image " << filename << endl; return bResult; #else cerr << "Intro::_CaptureImage() needs 'libpicio'" << endl; return false; #endif // HAVE_PIC_LIB }