Fix 2658 by separating networking code from main loop.

This commit is contained in:
hiker 2016-10-27 21:33:42 +11:00
parent 94346ae64d
commit fda3afad93
6 changed files with 44 additions and 26 deletions

View File

@ -34,7 +34,6 @@
#include "network/protocol_manager.hpp" #include "network/protocol_manager.hpp"
#include "network/race_event_manager.hpp" #include "network/race_event_manager.hpp"
#include "network/stk_host.hpp" #include "network/stk_host.hpp"
#include "network/protocols/synchronization_protocol.hpp"
#include "online/request_manager.hpp" #include "online/request_manager.hpp"
#include "race/history.hpp" #include "race/history.hpp"
#include "race/race_manager.hpp" #include "race/race_manager.hpp"
@ -291,15 +290,7 @@ void MainLoop::run()
if (World::getWorld() ) if (World::getWorld() )
{ {
// In case of networking world we can only start the timing once the World::getWorld()->updateTime(dt);
// SynchronizationProtocol has disappeared (which indicates that all
// other protocols necessary for running a game are running).
SynchronizationProtocol* protocol = static_cast<SynchronizationProtocol*>(
ProtocolManager::getInstance()->getProtocol(PROTOCOL_SYNCHRONIZATION));
if (!protocol)
{
World::getWorld()->updateTime(dt);
}
} }
PROFILER_POP_CPU_MARKER(); PROFILER_POP_CPU_MARKER();

View File

@ -28,7 +28,10 @@ typedef unsigned long Uint32;
class MainLoop class MainLoop
{ {
private: private:
/** True if the main loop should exit. */
bool m_abort; bool m_abort;
/** True if the frame rate should be throttled. */
bool m_throttle_fps; bool m_throttle_fps;
Uint32 m_curr_time; Uint32 m_curr_time;

View File

@ -17,6 +17,7 @@
#include "modes/world_status.hpp" #include "modes/world_status.hpp"
#include "main_loop.hpp"
#include "audio/music_manager.hpp" #include "audio/music_manager.hpp"
#include "audio/sfx_base.hpp" #include "audio/sfx_base.hpp"
#include "audio/sfx_manager.hpp" #include "audio/sfx_manager.hpp"
@ -32,21 +33,6 @@
#include <irrlicht.h> #include <irrlicht.h>
//-----------------------------------------------------------------------------
/** Starts the kart engines.
*/
void WorldStatus::startEngines()
{
if (m_engines_started)
return;
m_engines_started = true;
for (unsigned int i = 0; i < World::getWorld()->getNumKarts(); i++)
{
World::getWorld()->getKart(i)->startEngineSFX();
}
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
WorldStatus::WorldStatus() WorldStatus::WorldStatus()
{ {
@ -65,6 +51,7 @@ WorldStatus::WorldStatus()
if (device->getTimer()->isStopped()) if (device->getTimer()->isStopped())
device->getTimer()->start(); device->getTimer()->start();
m_ready_to_race = false;
} // WorldStatus } // WorldStatus
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -105,6 +92,10 @@ void WorldStatus::reset()
// Set the right music // Set the right music
World::getWorld()->getTrack()->startMusic(); World::getWorld()->getTrack()->startMusic();
// In case of a networked race the race can only start once
// all protocols are up. This flag waits for that, and is
// set by
m_ready_to_race = !NetworkConfig::get()->isNetworking();
} // reset } // reset
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -121,6 +112,21 @@ WorldStatus::~WorldStatus()
device->getTimer()->start(); device->getTimer()->start();
} // ~WorldStatus } // ~WorldStatus
//-----------------------------------------------------------------------------
/** Starts the kart engines.
*/
void WorldStatus::startEngines()
{
if (m_engines_started)
return;
m_engines_started = true;
for (unsigned int i = 0; i < World::getWorld()->getNumKarts(); i++)
{
World::getWorld()->getKart(i)->startEngineSFX();
}
} // startEngines
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Sets the clock mode and the initial time of the world clock. /** Sets the clock mode and the initial time of the world clock.
* \param mode The new clock mode. * \param mode The new clock mode.
@ -173,6 +179,10 @@ void WorldStatus::update(float dt)
*/ */
void WorldStatus::updateTime(const float dt) void WorldStatus::updateTime(const float dt)
{ {
// In case of a networked race wait till all necessary protocols are
// ready before progressing the timer
if (!m_ready_to_race) return;
switch (m_phase) switch (m_phase)
{ {
// Note: setup phase must be a separate phase, since the race_manager // Note: setup phase must be a separate phase, since the race_manager

View File

@ -94,6 +94,9 @@ protected:
/** If the start race should be played, disabled in cutscenes. */ /** If the start race should be played, disabled in cutscenes. */
bool m_play_racestart_sounds; bool m_play_racestart_sounds;
/** A flag that causes the world to wait in case of a networking race
* till all protocols are up and running. */
bool m_ready_to_race;
private: private:
/** Sound to play at the beginning of a race, during which a /** Sound to play at the beginning of a race, during which a
* a camera intro of the track can be shown. */ * a camera intro of the track can be shown. */
@ -194,6 +197,8 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Get the time since start regardless of which way the clock counts */ /** Get the time since start regardless of which way the clock counts */
float getTimeSinceStart() const { return m_count_up_timer; } float getTimeSinceStart() const { return m_count_up_timer; }
// ------------------------------------------------------------------------
void setReadyToRace() { m_ready_to_race = true; }
}; // WorldStatus }; // WorldStatus

View File

@ -33,6 +33,15 @@ GameEventsProtocol::~GameEventsProtocol()
{ {
} // ~GameEventsProtocol } // ~GameEventsProtocol
// ----------------------------------------------------------------------------
/** Once the GameEventsProtocol is ready, signal the world that the timer
* can start.
*/
void GameEventsProtocol::setup()
{
World::getWorld()->setReadyToRace();
} // setup
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool GameEventsProtocol::notifyEvent(Event* event) bool GameEventsProtocol::notifyEvent(Event* event)
{ {

View File

@ -27,7 +27,7 @@ public:
void kartFinishedRace(const NetworkString &ns); void kartFinishedRace(const NetworkString &ns);
void startReadySetGo(); void startReadySetGo();
void receivedReadySetGo(); void receivedReadySetGo();
virtual void setup() OVERRIDE {}; virtual void setup() OVERRIDE;
virtual void update(float dt) OVERRIDE {}; virtual void update(float dt) OVERRIDE {};
virtual void asynchronousUpdate() OVERRIDE{} virtual void asynchronousUpdate() OVERRIDE{}
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------