Back to index

supertuxkart  0.5+dfsg1
game_manager.cpp
Go to the documentation of this file.
00001 //  $Id: screen_manager.cpp 855 2006-11-17 01:50:37Z coz $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include <cstdlib>
00021 
00022 #ifdef __APPLE__
00023 #  include <OpenGL/gl.h>
00024 #else
00025 #  ifdef WIN32
00026 #    define WIN32_LEAN_AND_MEAN
00027 #    include <windows.h>
00028 #  endif
00029 #  include <GL/gl.h>
00030 #endif
00031 #include <SDL/SDL.h>
00032 #include <assert.h>
00033 #include "sdldrv.hpp"
00034 #include "game_manager.hpp"
00035 #include "gui/menu_manager.hpp"
00036 #include "sound_manager.hpp"
00037 #include "material_manager.hpp"
00038 #include "race_manager.hpp"
00039 #include "world.hpp"
00040 #include "user_config.hpp"
00041 #include "scene.hpp"
00042 #include "history.hpp"
00043 
00044 GameManager* game_manager = 0;
00045 
00046 GameManager::GameManager() :
00047 m_abort(false),
00048 m_frame_count(0),
00049 m_curr_time(m_prev_time),
00050 m_prev_time(SDL_GetTicks())
00051 {
00052 }  // GameManager
00053 
00054 //-----------------------------------------------------------------------------
00055 GameManager::~GameManager()
00056 {
00057 }   // ~GameManager
00058 
00059 //-----------------------------------------------------------------------------
00060 void GameManager::run()
00061 {
00062     const GLuint TITLE_SCREEN_TEXTURE = 
00063         material_manager->getMaterial("st_title_screen.rgb")->getState()->getTextureHandle();
00064        
00065     bool music_on = false;
00066     m_curr_time = SDL_GetTicks();
00067     float dt;
00068     while(!m_abort)
00069     {
00070         inputDriver->input();
00071 
00072         m_prev_time = m_curr_time;
00073 
00074         while( 1 )
00075         {
00076             m_curr_time = SDL_GetTicks();
00077             dt =(float)(m_curr_time - m_prev_time);
00078             
00079             // don't allow the game to run slower than a certain amount.
00080             // when the computer can't keep it up, slow down the shown time instead
00081             static const float max_elapsed_time = 3.0f*1.0f/60.0f*1000.0f; /* time 3 internal substeps take */
00082             if(dt > max_elapsed_time) dt=max_elapsed_time;
00083                                                
00084             // Throttle fps if more than maximum, which can reduce 
00085             // the noise the fan on a graphics card makes
00086             if( dt*user_config->m_max_fps < 1000.0f)
00087             {
00088                 //SDL_Delay has a granularity of 10ms on most platforms, so
00089                 //most likely when frames go faster than 125 frames, at times
00090                 //it might limit the frames to even 55 frames. On some cases,
00091                 //SDL_Delay(1) will just cause the program to give up the
00092                 //rest of it's timeslice.
00093                 SDL_Delay(1);
00094             }
00095             else break;
00096         }
00097         dt *= 0.001f;
00098 
00099         if (!music_on && !race_manager->raceIsActive())
00100         {
00101             sound_manager->stopMusic();   // stop potential 'left over' music from race
00102               sound_manager->startMusic(stk_config->m_title_music);
00103                   music_on = true;
00104         }
00105 
00106         if (race_manager->raceIsActive())
00107         {
00108             music_on = false; 
00109                if(user_config->m_profile) dt=1.0f/60.0f;
00110             // In the first call dt might be large (includes loading time),
00111             // which can cause the camera to significantly tilt
00112             scene->draw(world->getPhase()==World::SETUP_PHASE ? 0.0f : dt);
00113             if ( world->getPhase() != World::LIMBO_PHASE)
00114             {
00115                 world->update(dt);
00116 
00117                 if(user_config->m_profile>0)
00118                 {
00119                     m_frame_count++;
00120                     if (world->getTime()>user_config->m_profile)
00121                     {
00122                         //FIXME: SDL_GetTicks() includes the loading time,
00123                         //so the FPS will be skewed for now.
00124                         printf("Number of frames: %d time %f, Average FPS: %f\n",
00125                                m_frame_count, SDL_GetTicks() * 0.001,
00126                                (float)m_frame_count/(SDL_GetTicks() * 0.001));
00127                         if(!user_config->m_replay_history) history->Save();
00128                         std::exit(-2);
00129                     }
00130                 }   // if m_profile
00131             }
00132         }
00133         else
00134         {
00135             glMatrixMode   ( GL_PROJECTION ) ;
00136             glLoadIdentity () ;
00137             glMatrixMode   ( GL_MODELVIEW ) ;
00138             glLoadIdentity () ;
00139             glDisable      ( GL_DEPTH_TEST ) ;
00140             glDisable      ( GL_LIGHTING   ) ;
00141             glDisable      ( GL_FOG        ) ;
00142             glDisable      ( GL_CULL_FACE  ) ;
00143             glDisable      ( GL_ALPHA_TEST ) ;
00144             glEnable       ( GL_TEXTURE_2D ) ;
00145 
00146             // On at least one platform the X server apparently gets overloaded
00147             // by the large texture, resulting in buffering of key events. This
00148             // results in the menu being very unresponsive/slow - it can sometimes
00149             // take (say) half a second before the menu reacts to a pressed key.
00150             // This is caused by X buffering the key events, delivering them
00151             // later (and sometimes even several at the same frame). This issue
00152             // could either be solved by a lazy drawing of the background picture
00153             // (i.e. draw the background only if something has changed) - which is
00154             // a lot of implementation work ... or by sleeping for a little while,
00155             // which apparently reduces the load for the X server, so that no
00156             // buffering is done --> all key events are handled in time.
00157         #if !defined(WIN32) && !defined(__CYGWIN__)
00158 //                usleep(2000);
00159         #endif
00160             //Draw the splash screen
00161             glBindTexture(GL_TEXTURE_2D,TITLE_SCREEN_TEXTURE);
00162 
00163             glBegin ( GL_QUADS ) ;
00164             glColor3f   (1, 1, 1 ) ;
00165             glTexCoord2f(0, 0); glVertex2i(-1, -1);
00166             glTexCoord2f(1, 0); glVertex2i( 1, -1);
00167             glTexCoord2f(1, 1); glVertex2i( 1,  1);
00168             glTexCoord2f(0, 1); glVertex2i(-1,  1);
00169             glEnd () ;
00170         }
00171 
00172         menu_manager->update();
00173         sound_manager->update(dt);
00174 
00175         glFlush();
00176         SDL_GL_SwapBuffers();
00177     }  // while !m_exit
00178 }   // run
00179 
00180 //-----------------------------------------------------------------------------
00181 void GameManager::abort()
00182 {
00183     m_abort = true;
00184 }
00185 
00186 /* EOF */