2008-09-15 00:34:46 -04:00
|
|
|
// $Id: main_loop.cpp 855 2006-11-17 01:50:37Z coz $
|
2007-05-27 12:01:53 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
2008-06-12 20:53:52 -04:00
|
|
|
// as published by the Free Software Foundation; either version 3
|
2007-05-27 12:01:53 -04:00
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
2008-09-15 00:34:46 -04:00
|
|
|
#include "main_loop.hpp"
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
#include <SDL/SDL.h>
|
|
|
|
#include <assert.h>
|
2009-02-10 00:30:59 -05:00
|
|
|
#include "history.hpp"
|
2007-05-27 12:01:53 -04:00
|
|
|
#include "sdldrv.hpp"
|
|
|
|
#include "material_manager.hpp"
|
|
|
|
#include "race_manager.hpp"
|
2009-01-22 17:27:13 -05:00
|
|
|
#include "audio/sound_manager.hpp"
|
2009-02-10 00:30:59 -05:00
|
|
|
#include "graphics/irr_driver.hpp"
|
2009-01-22 17:27:13 -05:00
|
|
|
#include "graphics/scene.hpp"
|
|
|
|
#include "gui/menu_manager.hpp"
|
2009-02-10 00:30:59 -05:00
|
|
|
#include "modes/world.hpp"
|
|
|
|
#include "user_config.hpp"
|
2008-09-07 09:38:46 -04:00
|
|
|
#include "network/network_manager.hpp"
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2008-09-15 00:34:46 -04:00
|
|
|
MainLoop* main_loop = 0;
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2008-09-15 00:34:46 -04:00
|
|
|
MainLoop::MainLoop() :
|
2007-05-27 12:01:53 -04:00
|
|
|
m_abort(false),
|
|
|
|
m_frame_count(0),
|
|
|
|
m_curr_time(m_prev_time),
|
|
|
|
m_prev_time(SDL_GetTicks())
|
|
|
|
{
|
2008-09-15 00:34:46 -04:00
|
|
|
} // MainLoop
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-15 00:34:46 -04:00
|
|
|
MainLoop::~MainLoop()
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2008-09-15 00:34:46 -04:00
|
|
|
} // ~MainLoop
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2009-01-12 07:29:31 -05:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void MainLoop::loadBackgroundImages()
|
|
|
|
{
|
|
|
|
int ind = user_config->getBackgroundIndex();
|
|
|
|
const std::string &main = stk_config->getMainMenuPicture(ind);
|
2009-02-26 19:15:09 -05:00
|
|
|
#ifndef HAVE_IRRLICHT
|
2009-01-12 07:29:31 -05:00
|
|
|
m_title_screen_texture = material_manager->getMaterial(main)->getState()->getTextureHandle();
|
2009-02-26 19:15:09 -05:00
|
|
|
#endif
|
2009-01-12 07:29:31 -05:00
|
|
|
|
|
|
|
const std::string &background = stk_config->getBackgroundPicture(ind);
|
2009-02-26 19:15:09 -05:00
|
|
|
#ifndef HAVE_IRRLICHT
|
2009-01-12 07:29:31 -05:00
|
|
|
m_bg_texture = material_manager->getMaterial(background)->getState()->getTextureHandle();
|
2009-02-26 19:15:09 -05:00
|
|
|
#endif
|
2009-01-12 07:29:31 -05:00
|
|
|
} // loadBackgroundImages
|
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-15 00:34:46 -04:00
|
|
|
/** Run the actual main loop.
|
|
|
|
*/
|
|
|
|
void MainLoop::run()
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2009-01-12 07:29:31 -05:00
|
|
|
loadBackgroundImages();
|
2009-01-11 19:49:25 -05:00
|
|
|
|
2008-02-11 23:35:39 -05:00
|
|
|
bool music_on = false;
|
2008-02-12 20:02:02 -05:00
|
|
|
m_curr_time = SDL_GetTicks();
|
2008-05-03 20:08:49 -04:00
|
|
|
float dt;
|
2007-05-27 12:01:53 -04:00
|
|
|
while(!m_abort)
|
|
|
|
{
|
2008-03-30 11:18:19 -04:00
|
|
|
inputDriver->input();
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2008-02-12 20:02:02 -05:00
|
|
|
m_prev_time = m_curr_time;
|
2008-05-03 20:08:49 -04:00
|
|
|
|
2008-05-04 13:34:40 -04:00
|
|
|
while( 1 )
|
2008-05-03 20:08:49 -04:00
|
|
|
{
|
2008-05-04 13:34:40 -04:00
|
|
|
m_curr_time = SDL_GetTicks();
|
2008-05-05 03:06:44 -04:00
|
|
|
dt =(float)(m_curr_time - m_prev_time);
|
2008-05-07 21:48:51 -04:00
|
|
|
|
|
|
|
// don't allow the game to run slower than a certain amount.
|
|
|
|
// when the computer can't keep it up, slow down the shown time instead
|
|
|
|
static const float max_elapsed_time = 3.0f*1.0f/60.0f*1000.0f; /* time 3 internal substeps take */
|
|
|
|
if(dt > max_elapsed_time) dt=max_elapsed_time;
|
|
|
|
|
2008-05-11 20:26:35 -04:00
|
|
|
// Throttle fps if more than maximum, which can reduce
|
|
|
|
// the noise the fan on a graphics card makes
|
|
|
|
if( dt*user_config->m_max_fps < 1000.0f)
|
2008-05-04 13:34:40 -04:00
|
|
|
{
|
|
|
|
//SDL_Delay has a granularity of 10ms on most platforms, so
|
|
|
|
//most likely when frames go faster than 125 frames, at times
|
|
|
|
//it might limit the frames to even 55 frames. On some cases,
|
|
|
|
//SDL_Delay(1) will just cause the program to give up the
|
|
|
|
//rest of it's timeslice.
|
|
|
|
SDL_Delay(1);
|
|
|
|
}
|
|
|
|
else break;
|
2008-05-03 20:08:49 -04:00
|
|
|
}
|
|
|
|
dt *= 0.001f;
|
2008-03-17 01:18:26 -04:00
|
|
|
|
2008-05-07 21:28:07 -04:00
|
|
|
if (!music_on && !race_manager->raceIsActive())
|
|
|
|
{
|
|
|
|
sound_manager->stopMusic(); // stop potential 'left over' music from race
|
|
|
|
sound_manager->startMusic(stk_config->m_title_music);
|
2008-02-11 23:35:39 -05:00
|
|
|
music_on = true;
|
2008-05-07 21:28:07 -04:00
|
|
|
}
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2008-09-07 09:27:00 -04:00
|
|
|
network_manager->update(dt);
|
2008-09-07 10:34:04 -04:00
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
if (race_manager->raceIsActive())
|
|
|
|
{
|
2008-09-07 09:58:37 -04:00
|
|
|
// Busy wait if race_manager is active (i.e. creating of world is done)
|
|
|
|
// till all clients have reached this state.
|
|
|
|
if(network_manager->getState()==NetworkManager::NS_READY_SET_GO_BARRIER) continue;
|
2008-09-07 10:34:04 -04:00
|
|
|
|
|
|
|
// Server: Send the current position and previous controls to all clients
|
|
|
|
// Client: send current controls to server
|
2008-09-28 22:29:33 -04:00
|
|
|
// But don't do this if the race is in finish phase (otherwise
|
|
|
|
// messages can be mixed up in the race manager)
|
2008-10-04 13:32:40 -04:00
|
|
|
if(!race_manager->getWorld()->isFinishPhase())
|
2008-09-28 22:29:33 -04:00
|
|
|
network_manager->sendUpdates();
|
2008-02-12 04:27:38 -05:00
|
|
|
music_on = false;
|
2008-06-09 22:35:10 -04:00
|
|
|
if(user_config->m_profile) dt=1.0f/60.0f;
|
2008-02-12 20:18:51 -05:00
|
|
|
// In the first call dt might be large (includes loading time),
|
|
|
|
// which can cause the camera to significantly tilt
|
2009-01-27 19:38:28 -05:00
|
|
|
stk_scene->draw(RaceManager::getWorld()->getPhase()==SETUP_PHASE ? 0.0f : dt);
|
2008-09-07 10:34:04 -04:00
|
|
|
|
2008-09-28 22:29:33 -04:00
|
|
|
// Again, only receive updates if the race isn't over - once the
|
|
|
|
// race results are displayed (i.e. game is in finish phase)
|
|
|
|
// messages must be handled by the normal update of the network
|
|
|
|
// manager
|
2008-10-04 13:32:40 -04:00
|
|
|
if(!race_manager->getWorld()->isFinishPhase())
|
2008-09-28 22:29:33 -04:00
|
|
|
network_manager->receiveUpdates();
|
2008-09-07 10:34:04 -04:00
|
|
|
|
2008-09-21 12:07:56 -04:00
|
|
|
if ( RaceManager::getWorld()->getPhase() != LIMBO_PHASE)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2008-10-06 09:40:11 -04:00
|
|
|
history->update(dt);
|
2008-09-21 12:07:56 -04:00
|
|
|
RaceManager::getWorld()->update(dt);
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2008-02-17 07:58:12 -05:00
|
|
|
if(user_config->m_profile>0)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
m_frame_count++;
|
2008-09-21 12:07:56 -04:00
|
|
|
if (RaceManager::getWorld()->getTime()>user_config->m_profile)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
//FIXME: SDL_GetTicks() includes the loading time,
|
|
|
|
//so the FPS will be skewed for now.
|
|
|
|
printf("Number of frames: %d time %f, Average FPS: %f\n",
|
|
|
|
m_frame_count, SDL_GetTicks() * 0.001,
|
|
|
|
(float)m_frame_count/(SDL_GetTicks() * 0.001));
|
2008-10-06 09:40:11 -04:00
|
|
|
if(!history->replayHistory()) history->Save();
|
2007-05-27 12:01:53 -04:00
|
|
|
std::exit(-2);
|
2008-06-09 22:35:10 -04:00
|
|
|
} // if profile finished
|
2007-05-27 12:01:53 -04:00
|
|
|
} // if m_profile
|
2008-06-09 22:35:10 -04:00
|
|
|
} // phase != limbo phase
|
|
|
|
} // if race is active
|
2007-05-27 12:01:53 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
glMatrixMode ( GL_PROJECTION ) ;
|
|
|
|
glLoadIdentity () ;
|
|
|
|
glMatrixMode ( GL_MODELVIEW ) ;
|
|
|
|
glLoadIdentity () ;
|
|
|
|
glDisable ( GL_DEPTH_TEST ) ;
|
|
|
|
glDisable ( GL_LIGHTING ) ;
|
|
|
|
glDisable ( GL_FOG ) ;
|
|
|
|
glDisable ( GL_CULL_FACE ) ;
|
|
|
|
glDisable ( GL_ALPHA_TEST ) ;
|
|
|
|
glEnable ( GL_TEXTURE_2D ) ;
|
|
|
|
|
|
|
|
// On at least one platform the X server apparently gets overloaded
|
|
|
|
// by the large texture, resulting in buffering of key events. This
|
|
|
|
// results in the menu being very unresponsive/slow - it can sometimes
|
|
|
|
// take (say) half a second before the menu reacts to a pressed key.
|
|
|
|
// This is caused by X buffering the key events, delivering them
|
|
|
|
// later (and sometimes even several at the same frame). This issue
|
|
|
|
// could either be solved by a lazy drawing of the background picture
|
|
|
|
// (i.e. draw the background only if something has changed) - which is
|
|
|
|
// a lot of implementation work ... or by sleeping for a little while,
|
|
|
|
// which apparently reduces the load for the X server, so that no
|
|
|
|
// buffering is done --> all key events are handled in time.
|
|
|
|
#if !defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
// usleep(2000);
|
|
|
|
#endif
|
2008-09-07 10:05:48 -04:00
|
|
|
|
2009-01-11 19:49:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
//Draw the splash screen
|
2009-01-11 19:49:25 -05:00
|
|
|
|
|
|
|
if(menu_manager->isMainMenuActive())
|
2009-01-12 07:29:31 -05:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_title_screen_texture);
|
2009-01-11 19:49:25 -05:00
|
|
|
else
|
2009-01-12 07:29:31 -05:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bg_texture);
|
2009-01-11 19:49:25 -05:00
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
glBegin ( GL_QUADS ) ;
|
|
|
|
glColor3f (1, 1, 1 ) ;
|
|
|
|
glTexCoord2f(0, 0); glVertex2i(-1, -1);
|
|
|
|
glTexCoord2f(1, 0); glVertex2i( 1, -1);
|
|
|
|
glTexCoord2f(1, 1); glVertex2i( 1, 1);
|
|
|
|
glTexCoord2f(0, 1); glVertex2i(-1, 1);
|
|
|
|
glEnd () ;
|
|
|
|
}
|
|
|
|
|
|
|
|
menu_manager->update();
|
2008-03-17 01:18:26 -04:00
|
|
|
sound_manager->update(dt);
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2009-02-10 00:30:59 -05:00
|
|
|
#ifdef HAVE_IRRLICHT
|
2009-02-16 21:57:28 -05:00
|
|
|
if(!user_config->m_bullet_debug)
|
|
|
|
irr_driver->update(dt);
|
2009-02-10 00:30:59 -05:00
|
|
|
#else
|
2007-05-27 12:01:53 -04:00
|
|
|
glFlush();
|
|
|
|
SDL_GL_SwapBuffers();
|
2009-02-10 00:30:59 -05:00
|
|
|
#endif
|
2007-05-27 12:01:53 -04:00
|
|
|
} // while !m_exit
|
|
|
|
} // run
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-15 00:34:46 -04:00
|
|
|
/** Set the abort flag, causing the mainloop to be left.
|
|
|
|
*/
|
|
|
|
void MainLoop::abort()
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
m_abort = true;
|
2008-09-15 00:34:46 -04:00
|
|
|
} // abort
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
/* EOF */
|