Bugfix/code cleanup: the loading time was counted as countdown time

in certain circumstances (e.g. when setting up a new race from the race menu).


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1474 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2008-02-13 01:02:02 +00:00
parent 646e98684a
commit 7550f687da
7 changed files with 80 additions and 91 deletions

View File

@ -46,7 +46,6 @@ GameManager* game_manager = 0;
GameManager::GameManager() : GameManager::GameManager() :
m_abort(false), m_abort(false),
m_frame_count(0), m_frame_count(0),
m_started(false),
m_curr_time(m_prev_time), m_curr_time(m_prev_time),
m_prev_time(SDL_GetTicks()) m_prev_time(SDL_GetTicks())
{ {
@ -64,19 +63,12 @@ void GameManager::run()
material_manager->getMaterial("st_title_screen.rgb")->getIndex(); material_manager->getMaterial("st_title_screen.rgb")->getIndex();
bool music_on = false; bool music_on = false;
m_curr_time = SDL_GetTicks();
while(!m_abort) while(!m_abort)
{ {
sdl_input(); sdl_input();
// Now the screen may have changed and m_prev_time = m_curr_time;
// needs to be updated.
if(m_started) m_prev_time = m_curr_time;
else if( race_manager->raceIsActive() )
{
m_prev_time = SDL_GetTicks();
m_started = true;
}
m_curr_time = SDL_GetTicks(); m_curr_time = SDL_GetTicks();
if (!music_on && !race_manager->raceIsActive()) if (!music_on && !race_manager->raceIsActive())
@ -88,10 +80,11 @@ void GameManager::run()
if (race_manager->raceIsActive()) if (race_manager->raceIsActive())
{ {
music_on = false; music_on = false;
scene->draw((m_curr_time - m_prev_time ) * 0.001f); float dt = (m_curr_time - m_prev_time ) * 0.001f;
scene->draw(dt);
if ( world->getPhase() != World::LIMBO_PHASE) if ( world->getPhase() != World::LIMBO_PHASE)
{ {
world->update((m_curr_time - m_prev_time ) * 0.001f); world->update(dt);
if(user_config->m_profile) if(user_config->m_profile)
{ {

View File

@ -29,7 +29,6 @@ private:
bool m_abort; bool m_abort;
int m_frame_count; int m_frame_count;
bool m_started;
Uint32 m_curr_time; Uint32 m_curr_time;
Uint32 m_prev_time; Uint32 m_prev_time;

View File

@ -908,23 +908,25 @@ void RaceGUI::drawStatusText (const RaceSetup& raceSetup, const float dt)
glEnable ( GL_BLEND ); glEnable ( GL_BLEND );
glOrtho ( 0, user_config->m_width, 0, user_config->m_height, 0, 100 ) ; glOrtho ( 0, user_config->m_width, 0, user_config->m_height, 0, 100 ) ;
switch (world->m_ready_set_go) switch (world->getPhase())
{ {
case 2: font_race->PrintShadow(_("Ready!"), 90, case World::READY_PHASE: font_race->PrintShadow(_("Ready!"), 90,
Font::CENTER_OF_SCREEN, Font::CENTER_OF_SCREEN,
Font::CENTER_OF_SCREEN, Font::CENTER_OF_SCREEN,
230, 170, 160); 230, 170, 160);
break; break;
case 1: font_race->PrintShadow(_("Set!"), 90, case World::SET_PHASE: font_race->PrintShadow(_("Set!"), 90,
Font::CENTER_OF_SCREEN, Font::CENTER_OF_SCREEN,
Font::CENTER_OF_SCREEN, Font::CENTER_OF_SCREEN,
230, 230, 160); 230, 230, 160);
break; break;
case 0: font_race->PrintShadow(_("Go!"), 90, case World::GO_PHASE: font_race->PrintShadow(_("Go!"), 90,
Font::CENTER_OF_SCREEN, Font::CENTER_OF_SCREEN,
Font::CENTER_OF_SCREEN, Font::CENTER_OF_SCREEN,
100, 210, 100); 100, 210, 100);
break; break;
default:
break;
} // switch } // switch
for(int i = 0; i < 10; ++i) for(int i = 0; i < 10; ++i)
@ -933,7 +935,7 @@ void RaceGUI::drawStatusText (const RaceSetup& raceSetup, const float dt)
font_race->Print(world->m_debug_text[i].c_str(), font_race->Print(world->m_debug_text[i].c_str(),
20, 20, 200 -i*20, 100, 210, 100); 20, 20, 200 -i*20, 100, 210, 100);
} }
if(world->getPhase()==World::START_PHASE) if(world->isStartPhase())
{ {
for(int i=0; i<raceSetup.getNumPlayers(); i++) for(int i=0; i<raceSetup.getNumPlayers(); i++)
{ {

View File

@ -121,7 +121,7 @@ void PlayerKart::update(float dt)
m_controls.accel = m_accel_val / 32768.0f; m_controls.accel = m_accel_val / 32768.0f;
if(world->getPhase()==World::START_PHASE) if(world->isStartPhase())
{ {
if(m_controls.accel!=0.0 || m_controls.brake!=false || if(m_controls.accel!=0.0 || m_controls.brake!=false ||
m_controls.fire|m_controls.wheelie|m_controls.jump) m_controls.fire|m_controls.wheelie|m_controls.jump)

View File

@ -88,7 +88,7 @@ DefaultRobot::DefaultRobot( const KartProperties *kart_properties,
//line, then move forward while turning. //line, then move forward while turning.
void DefaultRobot::update( float delta ) void DefaultRobot::update( float delta )
{ {
if( world->getPhase() == World::START_PHASE ) if( world->isStartPhase())
{ {
handle_race_start(); handle_race_start();
AutoKart::update( delta ); AutoKart::update( delta );
@ -481,7 +481,7 @@ void DefaultRobot::handle_rescue(const float DELTA)
// check if kart is stuck // check if kart is stuck
if(getVehicle()->getRigidBody()->getLinearVelocity().length()<2.0f && if(getVehicle()->getRigidBody()->getLinearVelocity().length()<2.0f &&
!isRescue() && world->getPhase() != World::START_PHASE ) !isRescue() && !world->isStartPhase())
{ {
m_time_since_stuck += DELTA; m_time_since_stuck += DELTA;
if(m_time_since_stuck > 2.0f) if(m_time_since_stuck > 2.0f)

View File

@ -62,7 +62,7 @@ World::World(const RaceSetup& raceSetup_) : m_race_setup(raceSetup_)
{ {
delete world; delete world;
world = this; world = this;
m_phase = START_PHASE; m_phase = SETUP_PHASE;
m_track = NULL; m_track = NULL;
@ -91,7 +91,9 @@ World::World(const RaceSetup& raceSetup_) : m_race_setup(raceSetup_)
// Load the track models - this must be done before the karts so that the // Load the track models - this must be done before the karts so that the
// karts can be positioned properly on (and not in) the tracks. // karts can be positioned properly on (and not in) the tracks.
printf("begin loading track\n");
loadTrack() ; loadTrack() ;
printf("endloading track\n");
int pos = 0; int pos = 0;
int playerIndex = 0; int playerIndex = 0;
@ -158,16 +160,7 @@ World::World(const RaceSetup& raceSetup_) : m_race_setup(raceSetup_)
const std::string& MUSIC_NAME= track_manager->getTrack(m_race_setup.m_track)->getMusic(); const std::string& MUSIC_NAME= track_manager->getTrack(m_race_setup.m_track)->getMusic();
if (MUSIC_NAME.size()>0) sound_manager->playMusic(MUSIC_NAME); if (MUSIC_NAME.size()>0) sound_manager->playMusic(MUSIC_NAME);
if(user_config->m_profile) m_phase = user_config->m_profile ? RACE_PHASE : SETUP_PHASE;
{
m_ready_set_go = -1;
m_phase = RACE_PHASE;
}
else
{
m_phase = START_PHASE; // profile starts without countdown
m_ready_set_go = 3;
}
#ifdef HAVE_GHOST_REPLAY #ifdef HAVE_GHOST_REPLAY
m_replay_recorder.initRecorder( m_race_setup.getNumKarts() ); m_replay_recorder.initRecorder( m_race_setup.getNumKarts() );
@ -254,13 +247,7 @@ void World::resetAllKarts()
void World::update(float dt) void World::update(float dt)
{ {
if(user_config->m_replay_history) dt=history->GetNextDelta(); if(user_config->m_replay_history) dt=history->GetNextDelta();
updateRaceStatus(dt);
checkRaceStatus();
// this line was before checkRaceStatus. but m_clock is set to 0.0 in
// checkRaceStatus on start, so m_clock would not be synchronized and the
// first delta would not be added .. that would cause a gap in
// replay-recording
m_clock += dt;
if( getPhase() == FINISH_PHASE ) if( getPhase() == FINISH_PHASE )
{ {
@ -326,7 +313,7 @@ void World::update(float dt)
#ifdef HAVE_GHOST_REPLAY #ifdef HAVE_GHOST_REPLAY
// we start recording after START_PHASE, since during start-phase m_clock is incremented // we start recording after START_PHASE, since during start-phase m_clock is incremented
// normally, but after switching to RACE_PHASE m_clock is set back to 0.0 // normally, but after switching to RACE_PHASE m_clock is set back to 0.0
if( m_phase != START_PHASE ) if( m_phase == GO_PHASE )
{ {
m_replay_recorder.pushFrame(); m_replay_recorder.pushFrame();
if( m_p_replay_player ) m_p_replay_player->showReplayAt( m_clock ); if( m_p_replay_player ) m_p_replay_player->showReplayAt( m_clock );
@ -423,39 +410,42 @@ bool World::loadReplayHumanReadable( std::string const &filename )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void World::checkRaceStatus() void World::updateRaceStatus(float dt)
{ {
if (m_clock > 1.0 && m_ready_set_go == 0) switch (m_phase) {
{ case SETUP_PHASE: m_clock=0.0f; m_phase=READY_PHASE;
m_ready_set_go = -1; dt = 0.0f; // solves the problem of adding track loading time
} break; // loading time, don't play sound yet
else if (m_clock > 2.0 && m_ready_set_go == 1) case READY_PHASE: if(m_clock==0.0) // play sound at beginning of next frame
{ sound_manager->playSfx(SOUND_PRESTART);
m_ready_set_go = 0; if(m_clock>1.0)
m_phase = RACE_PHASE; {
m_clock = 0.0f; m_phase=SET_PHASE;
sound_manager->playSfx(SOUND_START); sound_manager->playSfx(SOUND_PRESTART);
// Reset the brakes now that the prestart phase is over }
// (braking prevents the karts from sliding downhill) break;
for(unsigned int i=0; i<m_kart.size(); i++) case SET_PHASE : if(m_clock>2.0)
{ {
m_kart[i]->resetBrakes(); m_phase=GO_PHASE;
} m_clock=0.0f;
sound_manager->playSfx(SOUND_START);
// Reset the brakes now that the prestart
// phase is over (braking prevents the karts
// from sliding downhill)
for(unsigned int i=0; i<m_kart.size(); i++)
{
m_kart[i]->resetBrakes();
}
#ifdef HAVE_GHOST_REPLAY #ifdef HAVE_GHOST_REPLAY
// push positions at time 0.0 to replay-data // push positions at time 0.0 to replay-data
m_replay_recorder.pushFrame(); m_replay_recorder.pushFrame();
#endif #endif
} }
else if (m_clock > 1.0 && m_ready_set_go == 2) break;
{ case GO_PHASE : if(m_clock>1.0) m_phase=RACE_PHASE; break;
sound_manager->playSfx(SOUND_PRESTART); default : break;
m_ready_set_go = 1; } // switch
} m_clock += dt;
else if (m_clock > 0.0 && m_ready_set_go == 3)
{
sound_manager->playSfx(SOUND_PRESTART);
m_ready_set_go = 2;
}
/*if all players have finished, or if only one kart is not finished when /*if all players have finished, or if only one kart is not finished when
not in time trial mode, the race is over. Players are the last in the not in time trial mode, the race is over. Players are the last in the
@ -523,7 +513,7 @@ void World::checkRaceStatus()
} // if !raceIsFinished } // if !raceIsFinished
} // for i } // for i
} }
} // checkRaceStatus } // updateRaceStatus
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void World::updateRacePosition ( int k ) void World::updateRacePosition ( int k )
@ -597,9 +587,8 @@ void World::loadTrack()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void World::restartRace() void World::restartRace()
{ {
m_ready_set_go = 3;
m_clock = 0.0f; m_clock = 0.0f;
m_phase = START_PHASE; m_phase = SETUP_PHASE;
for ( Karts::iterator i = m_kart.begin(); i != m_kart.end() ; ++i ) for ( Karts::iterator i = m_kart.begin(); i != m_kart.end() ; ++i )
{ {

View File

@ -46,9 +46,15 @@ public:
RaceSetup m_race_setup; RaceSetup m_race_setup;
enum Phase { enum Phase {
// The traffic light is shown and all players stay in position // Game setup, e.g. track loading
START_PHASE, SETUP_PHASE,
// The traffic light turned green and all driver, drive around the track // 'Ready' is displayed
READY_PHASE,
// 'Set' is displayed
SET_PHASE,
// 'Go' is displayed, but this is already race phase
GO_PHASE,
// the actual race has started, no ready/set/go is displayed anymore
RACE_PHASE, RACE_PHASE,
// All players have finished, now wait a certain amount of time for AI // All players have finished, now wait a certain amount of time for AI
// karts to finish. If they do not finish in that time, finish the race // karts to finish. If they do not finish in that time, finish the race
@ -60,8 +66,6 @@ public:
LIMBO_PHASE, LIMBO_PHASE,
}; };
int m_ready_set_go;
Track* m_track; Track* m_track;
/** debug text that will be overlaid to the screen */ /** debug text that will be overlaid to the screen */
@ -70,6 +74,7 @@ public:
World(const RaceSetup& raceSetup); World(const RaceSetup& raceSetup);
virtual ~World(); virtual ~World();
void update(float delta); void update(float delta);
bool isStartPhase() const {return m_phase<=GO_PHASE;}
void restartRace(); void restartRace();
void disableRace(); // Put race into limbo phase void disableRace(); // Put race into limbo phase
@ -100,6 +105,7 @@ public:
void pause(); void pause();
void unpause(); void unpause();
private: private:
Karts m_kart; Karts m_kart;
float m_finish_delay_start_time; float m_finish_delay_start_time;
@ -112,7 +118,7 @@ private:
void updateRacePosition( int k ); void updateRacePosition( int k );
void loadTrack(); void loadTrack();
void checkRaceStatus(); void updateRaceStatus(float dt);
void resetAllKarts(); void resetAllKarts();
Kart* loadRobot(const KartProperties *kart_properties, int position, Kart* loadRobot(const KartProperties *kart_properties, int position,
sgCoord init_pos); sgCoord init_pos);