- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/Documents and Settings/jegan/Desktop/projectX/Game.cpp

Go to the documentation of this file.

    00001 #include "Game.h"
    00002 #include "Parser.h"
    00003 #include "List.h"
    00004 #include "Control.h"
    00005 #include "Player.h"
    00006
    00007 using namespace std;
    00008
    00009 Game::Game(int levelnum)
    00010 :_currentLevel( NULL )
    00011 {
    00012         /*glfuncs* f = glfuncs::instance();
    00013
    00014         _scramSurface = f->load_image("scramble.png");
    00015         _scram = f->SDL_GL_LoadTexture(_scramSurface, _scramCoords);*/
    00016
    00017         // Load in the level list text file
    00018         List* levelList = Parser::parse( "levels/levellist.txt" );
    00019         while( !levelList->empty() )
    00020         {
    00021                 _levelList.push( levelList->firstList()->firstString() );
    00022                 levelList = levelList->rest();
    00023         }
    00024         // End loading level list
    00025
    00026         for(int i=0; i<levelnum; i++)
    00027                 _levelList.pop();
    00028
    00029         // Load the first level
    00030         loadLevel( _levelList.front() );
    00031 }
    00032
    00033 Game::~Game()
    00034 {
    00035         delete _currentLevel;
    00036 }
    00037
    00038 bool Game::nextLevel()
    00039 {
    00040         _levelList.pop();
    00041         if( _levelList.empty() )
    00042         {
    00043                 cerr << "Error loading level: No next level" << endl;
    00044                 return false;
    00045         }
    00046         /*Player * oldPlayer = NULL;
    00047         if((Player *) _currentLevel->getPlayer())
    00048                 oldPlayer =(Player *) _currentLevel->getPlayer()->copy();
    00049         else
    00050                 oldPlayer = NULL;*/
    00051
    00052         delete _currentLevel;
    00053         _currentLevel = new Level();
    00054         _currentLevel->levelLoad( _levelList.front());
    00055         return true;
    00056 }
    00057
    00058 void Game::loadLevel( std::string filename )
    00059 {
    00060         if(!_currentLevel)
    00061                 delete _currentLevel;
    00062         _currentLevel = new Level();
    00063         _currentLevel->levelLoad( filename );
    00064 }
    00065
    00066 /*
    00067 * Game main loop
    00068 * returns true if the game ended normally
    00069 * returns false if the escape key was pressed
    00070 */
    00071 bool Game::mainLoop()
    00072 {
    00073         SDL_Event event;
    00074         Screen* screen = Screen::instance();
    00075         glfuncs* f = glfuncs::instance();
    00076         bool leave = false;
    00077         int ticks = SDL_GetTicks();     // used to limit the frame rate
    00078         for(;;){
    00079                 while(!leave)
    00080                 {
    00081                         // Clear OpenGL screen
    00082                         f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    00083
    00084
    00085                         //This runs if we are trying to run frames in too rapid succession
    00086                         while((SDL_GetTicks() - ticks) < 1000/Screen::FRAMERATE)
    00087                         {
    00088                                 //Just wait until we can do a frame again
    00089                                 Sleep(0);
    00090                         }
    00091
    00092
    00093                         if(!_currentLevel->update())
    00094                                 return true;
    00095
    00096                         _currentLevel->draw();
    00097
    00098                         //reset for the next time we want to update
    00099                         ticks = SDL_GetTicks();
    00100
    00101                         // Draw Team Scramble logo
    00102                         /*SDL_Rect scramRect;
    00103                         scramRect.w = 51;
    00104                         scramRect.h = 81;
    00105                         scramRect.x = 5;
    00106                         scramRect.y = screen->getScreenRect().h - 80;
    00107                         screen->drawScreen(_scram,_scramCoords,scramRect);*/
    00108
    00109                         Screen::drawScramble();
    00110
    00111                         // Clean up
    00112                         f->glFinish();
    00113                         SDL_GL_SwapBuffers();
    00114
    00115                         // Check that escape key isn't being pressed
    00116                         while(SDL_PollEvent(&event))
    00117                         {
    00118                                 if(event.type == SDL_KEYDOWN){
    00119                                         if( event.key.keysym.sym == Control::quitKey)
    00120                                                 leave = true;
    00121                                 }
    00122                                 //if user has Xed out
    00123                                 else if( event.type == SDL_QUIT )
    00124                                 {
    00125                                         return false;
    00126                                 }
    00127                         }
    00128                 }
    00129
    00130                 f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    00131                 //This runs if we are trying to run frames in too rapid succession
    00132                 while((SDL_GetTicks() - ticks) < 1000/Screen::FRAMERATE)
    00133                 {
    00134                         //Just wait until we can do a frame again
    00135                         Sleep(0);
    00136                 }
    00137                 ticks = SDL_GetTicks();
    00138
    00139                 Screen::drawPause();
    00140
    00141                 Screen::drawScramble();
    00142
    00143                 // Clean up
    00144                 f->glFinish();
    00145                 SDL_GL_SwapBuffers();
    00146
    00147                 //user has pressed esc.
    00148                 while(SDL_PollEvent(&event))
    00149                 {
    00150                         if(event.type == SDL_KEYDOWN){
    00151                                 if( event.key.keysym.sym == Control::quitKey)
    00152                                         leave = false;
    00153                                 if( event.key.keysym.sym == Control::selectKey)
    00154                                         return false;
    00155                         }
    00156                                 //if user has Xed out
    00157                         else if( event.type == SDL_QUIT )
    00158                         {
    00159                                 return false;
    00160                         }
    00161                 }
    00162         }
    00163 }