Moved graphical weather update into separate update function that

is only called once per rendered frame.
This commit is contained in:
hiker 2018-03-20 09:58:34 +11:00
parent 950fc7a14f
commit 4fd205773e
5 changed files with 42 additions and 39 deletions

View File

@ -30,7 +30,7 @@ Weather::Weather()
{ {
m_thunder_sound = NULL; m_thunder_sound = NULL;
m_weather_sound = NULL; m_weather_sound = NULL;
m_lightning = 0; m_lightning = 0.0f;
if (Track::getCurrentTrack()->getWeatherLightning()) if (Track::getCurrentTrack()->getWeatherLightning())
{ {
@ -44,7 +44,7 @@ Weather::Weather()
} }
RandomGenerator g; RandomGenerator g;
m_next_lightning = stk_config->time2Ticks((float)g.get(35)); m_next_lightning = (float)g.get(35);
} // Weather } // Weather
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -60,7 +60,7 @@ Weather::~Weather()
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void Weather::update(int ticks) void Weather::update(float dt)
{ {
if (!Track::getCurrentTrack()->getWeatherLightning()) if (!Track::getCurrentTrack()->getWeatherLightning())
return; return;
@ -68,9 +68,9 @@ void Weather::update(int ticks)
if (World::getWorld()->getRaceGUI() == NULL) if (World::getWorld()->getRaceGUI() == NULL)
return; return;
m_next_lightning -= ticks; m_next_lightning -= dt;
if (m_next_lightning < 0) if (m_next_lightning < 0.0f)
{ {
startLightning(); startLightning();
@ -80,12 +80,12 @@ void Weather::update(int ticks)
} }
RandomGenerator g; RandomGenerator g;
m_next_lightning = stk_config->time2Ticks(35.0f + (float)g.get(35)); m_next_lightning = 35 + (float)g.get(35);
} }
if (m_lightning > 0) if (m_lightning > 0.0f)
{ {
m_lightning -= ticks; m_lightning -= dt;
} }
} // update } // update
@ -100,20 +100,11 @@ void Weather::playSound()
} }
} }
// ----------------------------------------------------------------------------
/** Set the flag that a lightning should be shown. */
void Weather::startLightning()
{
m_lightning = stk_config->time2Ticks(1.0f);
} // startLightning
// ----------------------------------------------------------------------------
irr::core::vector3df Weather::getIntensity() irr::core::vector3df Weather::getIntensity()
{ {
float light = stk_config->ticks2Time(m_lightning); irr::core::vector3df value = {0.7f * m_lightning,
irr::core::vector3df value = {0.7f * light, 0.7f * m_lightning,
0.7f * light, 0.7f * std::min(1.0f, m_lightning * 1.5f)};
0.7f * std::min(1.0f, light * 1.5f)};
return value; return value;
} // getIntensity }

View File

@ -26,11 +26,8 @@ class SFXBase;
class Weather : public AbstractSingleton<Weather> class Weather : public AbstractSingleton<Weather>
{ {
/** Counts ticks till the next lighting appears. */ float m_next_lightning;
int m_next_lightning; float m_lightning;
/** Counts the ticks for displaying the current lighting. */
int m_lightning;
SFXBase* m_thunder_sound; SFXBase* m_thunder_sound;
SFXBase* m_weather_sound; SFXBase* m_weather_sound;
@ -39,13 +36,14 @@ public:
Weather(); Weather();
virtual ~Weather(); virtual ~Weather();
void update(int ticks); void update(float dt);
void playSound(); void playSound();
void startLightning();
irr::core::vector3df getIntensity();
bool shouldLightning() { return m_lightning > 0; }
}; // class Weather /** Set the flag that a lightning should be shown. */
void startLightning() { m_lightning = 1.0f; }
bool shouldLightning() { return m_lightning > 0.0f; }
irr::core::vector3df getIntensity();
};
#endif #endif

View File

@ -317,6 +317,11 @@ void MainLoop::run()
float frame_duration = num_steps * dt; float frame_duration = num_steps * dt;
if (!ProfileWorld::isNoGraphics()) if (!ProfileWorld::isNoGraphics())
{ {
PROFILER_PUSH_CPU_MARKER("Update race", 0, 255, 255);
if (World::getWorld())
World::getWorld()->updateGraphics(frame_duration);
PROFILER_POP_CPU_MARKER();
// Render the previous frame, and also handle all user input. // Render the previous frame, and also handle all user input.
PROFILER_PUSH_CPU_MARKER("IrrDriver update", 0x00, 0x00, 0x7F); PROFILER_PUSH_CPU_MARKER("IrrDriver update", 0x00, 0x00, 0x7F);
irr_driver->update(frame_duration); irr_driver->update(frame_duration);

View File

@ -958,6 +958,21 @@ void World::scheduleTutorial()
m_schedule_tutorial = true; m_schedule_tutorial = true;
} // scheduleTutorial } // scheduleTutorial
//-----------------------------------------------------------------------------
/** This updates all only graphical elements. It is only called once per
* rendered frame, not once per time step.
* float dt Time since last rame.
*/
void World::updateGraphics(float dt)
{
PROFILER_PUSH_CPU_MARKER("World::update (weather)", 0x80, 0x7F, 0x00);
if (UserConfigParams::m_particles_effects > 1 && Weather::getInstance())
{
Weather::getInstance()->update(dt);
}
PROFILER_POP_CPU_MARKER();
} // updateGraphics
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Updates the physics, all karts, the track, and projectile manager. /** Updates the physics, all karts, the track, and projectile manager.
* \param ticks Number of physics time steps - should be 1. * \param ticks Number of physics time steps - should be 1.
@ -1024,13 +1039,6 @@ void World::update(int ticks)
Physics::getInstance()->update(ticks); Physics::getInstance()->update(ticks);
} }
PROFILER_PUSH_CPU_MARKER("World::update (weather)", 0x80, 0x7F, 0x00);
if (UserConfigParams::m_particles_effects > 1 && Weather::getInstance())
{
Weather::getInstance()->update(ticks);
}
PROFILER_POP_CPU_MARKER();
PROFILER_PUSH_CPU_MARKER("World::update (projectiles)", 0xa0, 0x7F, 0x00); PROFILER_PUSH_CPU_MARKER("World::update (projectiles)", 0xa0, 0x7F, 0x00);
projectile_manager->update(ticks); projectile_manager->update(ticks);
PROFILER_POP_CPU_MARKER(); PROFILER_POP_CPU_MARKER();

View File

@ -236,6 +236,7 @@ public:
// Virtual functions // Virtual functions
// ================= // =================
virtual void init(); virtual void init();
virtual void updateGraphics(float dt);
virtual void terminateRace() OVERRIDE; virtual void terminateRace() OVERRIDE;
virtual void reset() OVERRIDE; virtual void reset() OVERRIDE;
virtual void pause(Phase phase) OVERRIDE; virtual void pause(Phase phase) OVERRIDE;