Merge branch 'fix-timestep' into game_protocol

This commit is contained in:
hiker 2018-02-13 08:41:49 +11:00
commit 26401972d2
9 changed files with 126 additions and 95 deletions

View File

@ -575,6 +575,7 @@ s32 CIrrDeviceAndroid::handleKeyboard(AInputEvent* androidEvent)
int32_t keyAction = AKeyEvent_getAction(androidEvent); int32_t keyAction = AKeyEvent_getAction(androidEvent);
int32_t keyMetaState = AKeyEvent_getMetaState(androidEvent); int32_t keyMetaState = AKeyEvent_getMetaState(androidEvent);
int32_t keyRepeat = AKeyEvent_getRepeatCount(androidEvent); int32_t keyRepeat = AKeyEvent_getRepeatCount(androidEvent);
int32_t scanCode = AKeyEvent_getScanCode(androidEvent);
if (keyAction == AKEY_EVENT_ACTION_DOWN) if (keyAction == AKEY_EVENT_ACTION_DOWN)
{ {
@ -610,21 +611,27 @@ s32 CIrrDeviceAndroid::handleKeyboard(AInputEvent* androidEvent)
getKeyChar(event); getKeyChar(event);
} }
// If button doesn't return key code, then at least use device-specific
// scan code, because it's better than nothing
if (event.KeyInput.Key == 0)
{
event.KeyInput.Key = (EKEY_CODE)scanCode;
}
// Handle an event when back button in pressed just like an escape key // Handle an event when back button in pressed just like an escape key
// and also avoid repeating the event to avoid some strange behaviour // and also avoid repeating the event to avoid some strange behaviour
if (event.KeyInput.SystemKeyCode == AKEYCODE_BACK) if (event.KeyInput.SystemKeyCode == AKEYCODE_BACK &&
{ (event.KeyInput.PressedDown == false || keyRepeat > 0))
status = 1;
if (event.KeyInput.PressedDown == false || keyRepeat > 0)
{ {
ignore_event = true; ignore_event = true;
} }
}
// Mark escape key event as handled by application to avoid receiving // Mark escape key and gamepad buttons as handled by application to avoid
// AKEYCODE_BACK key event // receiving duplicated events
if (event.KeyInput.SystemKeyCode == AKEYCODE_ESCAPE) if (event.KeyInput.SystemKeyCode == AKEYCODE_ESCAPE ||
event.KeyInput.SystemKeyCode == AKEYCODE_BACK ||
(event.KeyInput.SystemKeyCode >= AKEYCODE_BUTTON_A &&
event.KeyInput.SystemKeyCode <= AKEYCODE_BUTTON_MODE))
{ {
status = 1; status = 1;
} }
@ -740,6 +747,11 @@ s32 CIrrDeviceAndroid::handleGamepad(AInputEvent* androidEvent)
int32_t keyCode = AKeyEvent_getKeyCode(androidEvent); int32_t keyCode = AKeyEvent_getKeyCode(androidEvent);
int32_t keyAction = AKeyEvent_getAction(androidEvent); int32_t keyAction = AKeyEvent_getAction(androidEvent);
int32_t keyRepeat = AKeyEvent_getRepeatCount(androidEvent); int32_t keyRepeat = AKeyEvent_getRepeatCount(androidEvent);
int32_t scanCode = AKeyEvent_getScanCode(androidEvent);
if (keyRepeat == 0)
{
bool ignore_event = false;
SEvent event; SEvent event;
event.EventType = EET_KEY_INPUT_EVENT; event.EventType = EET_KEY_INPUT_EVENT;
@ -750,15 +762,15 @@ s32 CIrrDeviceAndroid::handleGamepad(AInputEvent* androidEvent)
event.KeyInput.SystemKeyCode = (u32)keyCode; event.KeyInput.SystemKeyCode = (u32)keyCode;
event.KeyInput.Key = KeyMap[keyCode]; event.KeyInput.Key = KeyMap[keyCode];
// Handle an event when back button in pressed just like an escape key if (event.KeyInput.Key == 0)
// and also avoid repeating the event to avoid some strange behaviour
if (event.KeyInput.SystemKeyCode == AKEYCODE_BACK)
{ {
status = 1; event.KeyInput.Key = (EKEY_CODE)scanCode;
ignore = (event.KeyInput.PressedDown == false || keyRepeat > 0);
} }
postEventFromUser(event); postEventFromUser(event);
}
status = 1;
break; break;
} }
default: default:
@ -874,7 +886,7 @@ void CIrrDeviceAndroid::createKeyMap()
// following look like controller inputs // following look like controller inputs
KeyMap[AKEYCODE_BUTTON_A] = IRR_KEY_RETURN; KeyMap[AKEYCODE_BUTTON_A] = IRR_KEY_RETURN;
KeyMap[AKEYCODE_BUTTON_B] = IRR_KEY_BACK; KeyMap[AKEYCODE_BUTTON_B] = IRR_KEY_ESCAPE;
KeyMap[AKEYCODE_BUTTON_C] = IRR_KEY_2; KeyMap[AKEYCODE_BUTTON_C] = IRR_KEY_2;
KeyMap[AKEYCODE_BUTTON_X] = IRR_KEY_3; KeyMap[AKEYCODE_BUTTON_X] = IRR_KEY_3;
KeyMap[AKEYCODE_BUTTON_Y] = IRR_KEY_4; KeyMap[AKEYCODE_BUTTON_Y] = IRR_KEY_4;
@ -886,7 +898,7 @@ void CIrrDeviceAndroid::createKeyMap()
KeyMap[AKEYCODE_BUTTON_THUMBL] = IRR_KEY_RETURN; KeyMap[AKEYCODE_BUTTON_THUMBL] = IRR_KEY_RETURN;
KeyMap[AKEYCODE_BUTTON_THUMBR] = IRR_KEY_RETURN; KeyMap[AKEYCODE_BUTTON_THUMBR] = IRR_KEY_RETURN;
KeyMap[AKEYCODE_BUTTON_START] = IRR_KEY_RETURN; KeyMap[AKEYCODE_BUTTON_START] = IRR_KEY_RETURN;
KeyMap[AKEYCODE_BUTTON_SELECT] = IRR_KEY_BACK; KeyMap[AKEYCODE_BUTTON_SELECT] = IRR_KEY_ESCAPE;
KeyMap[AKEYCODE_BUTTON_MODE] = IRR_KEY_MENU; KeyMap[AKEYCODE_BUTTON_MODE] = IRR_KEY_MENU;
KeyMap[AKEYCODE_ESCAPE] = IRR_KEY_ESCAPE; KeyMap[AKEYCODE_ESCAPE] = IRR_KEY_ESCAPE;

View File

@ -150,7 +150,6 @@ void STKConfig::load(const std::string &filename)
CHECK_NEG(m_default_track_friction, "physics default-track-friction"); CHECK_NEG(m_default_track_friction, "physics default-track-friction");
CHECK_NEG(m_physics_fps, "physics fps" ); CHECK_NEG(m_physics_fps, "physics fps" );
CHECK_NEG(m_network_state_frequeny, "network state-frequency" ); CHECK_NEG(m_network_state_frequeny, "network state-frequency" );
CHECK_NEG(m_network_state_frequeny, "network state-frequency" );
CHECK_NEG(m_default_moveable_friction, "physics default-moveable-friction"); CHECK_NEG(m_default_moveable_friction, "physics default-moveable-friction");
// Square distance to make distance checks cheaper (no sqrt) // Square distance to make distance checks cheaper (no sqrt)

View File

@ -233,19 +233,27 @@ void IrrDriver::updateConfigIfRelevant()
core::recti IrrDriver::getSplitscreenWindow(int WindowNum) core::recti IrrDriver::getSplitscreenWindow(int WindowNum)
{ {
const int playernum = race_manager->getNumLocalPlayers(); const int playernum = race_manager->getNumLocalPlayers();
const float playernum_sqrt = sqrt(playernum); const float playernum_sqrt = sqrtf((float)playernum);
int rows = UserConfigParams::split_screen_horizontally ? ceil(playernum_sqrt) : round(playernum_sqrt); int rows = int( UserConfigParams::split_screen_horizontally
int cols = UserConfigParams::split_screen_horizontally ? round(playernum_sqrt) : ceil(playernum_sqrt); ? ceil(playernum_sqrt)
: round(playernum_sqrt) );
int cols = int( UserConfigParams::split_screen_horizontally
? round(playernum_sqrt)
: ceil(playernum_sqrt) );
if (rows == 0){rows = 1;} if (rows == 0){rows = 1;}
if (cols == 0) {cols = 1;} if (cols == 0) {cols = 1;}
//This could add a bit of overhang //This could add a bit of overhang
const int width_of_space = ceil((float)irr_driver->getActualScreenSize().Width / (float)cols); const int width_of_space =
const int height_of_space = ceil((float)irr_driver->getActualScreenSize().Height / (float)rows); int(ceil( (float)irr_driver->getActualScreenSize().Width
/ (float)cols) );
const int height_of_space =
int (ceil( (float)irr_driver->getActualScreenSize().Height
/ (float)rows) );
const int x_grid_Position = WindowNum % cols; const int x_grid_Position = WindowNum % cols;
const int y_grid_Position = floor((WindowNum) / cols); const int y_grid_Position = int(floor((WindowNum) / cols));
int wid = (int)irr_driver->getActualScreenSize().Width; int wid = (int)irr_driver->getActualScreenSize().Width;
//To prevent the viewport going over the right side, we use std::min to ensure the right corners are never larger than the total width //To prevent the viewport going over the right side, we use std::min to ensure the right corners are never larger than the total width

View File

@ -60,6 +60,7 @@
#include "karts/max_speed.hpp" #include "karts/max_speed.hpp"
#include "karts/rescue_animation.hpp" #include "karts/rescue_animation.hpp"
#include "karts/skidding.hpp" #include "karts/skidding.hpp"
#include "main_loop.hpp"
#include "modes/overworld.hpp" #include "modes/overworld.hpp"
#include "modes/soccer_world.hpp" #include "modes/soccer_world.hpp"
#include "modes/world.hpp" #include "modes/world.hpp"
@ -1771,7 +1772,7 @@ void Kart::handleMaterialSFX(const Material *material)
// terrain sound is not necessarily a looping sound so check its status before // terrain sound is not necessarily a looping sound so check its status before
// setting its speed, to avoid 'ressuscitating' sounds that had already stopped // setting its speed, to avoid 'ressuscitating' sounds that had already stopped
if(m_terrain_sound && if(m_terrain_sound && main_loop->isLstSubstep() &&
(m_terrain_sound->getStatus()==SFXBase::SFX_PLAYING || (m_terrain_sound->getStatus()==SFXBase::SFX_PLAYING ||
m_terrain_sound->getStatus()==SFXBase::SFX_PAUSED)) m_terrain_sound->getStatus()==SFXBase::SFX_PAUSED))
{ {
@ -2403,10 +2404,13 @@ void Kart::updatePhysics(float dt)
*/ */
void Kart::updateEngineSFX(float dt) void Kart::updateEngineSFX(float dt)
{ {
// when going faster, use higher pitch for engine // Only update SFX during the last substep (otherwise too many SFX commands
if(!m_engine_sound || !SFXManager::get()->sfxAllowed()) // in one frame), and if sfx are enabled
if(!m_engine_sound || !SFXManager::get()->sfxAllowed() ||
!main_loop->isLstSubstep() )
return; return;
// when going faster, use higher pitch for engine
if(isOnGround()) if(isOnGround())
{ {
float max_speed = m_kart_properties->getEngineMaxSpeed(); float max_speed = m_kart_properties->getEngineMaxSpeed();

View File

@ -49,6 +49,7 @@ m_abort(false)
m_curr_time = 0; m_curr_time = 0;
m_prev_time = 0; m_prev_time = 0;
m_throttle_fps = true; m_throttle_fps = true;
m_is_last_substep = false;
} // MainLoop } // MainLoop
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -63,6 +64,7 @@ MainLoop::~MainLoop()
*/ */
float MainLoop::getLimitedDt() float MainLoop::getLimitedDt()
{ {
m_prev_time = m_curr_time;
float dt = 0; float dt = 0;
// In profile mode without graphics, run with a fixed dt of 1/60 // In profile mode without graphics, run with a fixed dt of 1/60
@ -73,7 +75,6 @@ float MainLoop::getLimitedDt()
} }
IrrlichtDevice* device = irr_driver->getDevice(); IrrlichtDevice* device = irr_driver->getDevice();
m_prev_time = m_curr_time;
while( 1 ) while( 1 )
{ {
@ -260,12 +261,18 @@ void MainLoop::run()
IrrlichtDevice* device = irr_driver->getDevice(); IrrlichtDevice* device = irr_driver->getDevice();
m_curr_time = device->getTimer()->getRealTime(); m_curr_time = device->getTimer()->getRealTime();
// DT keeps track of the leftover time, since the race update
// happens in fixed timesteps
float left_over_time = 0;
while(!m_abort) while(!m_abort)
{ {
m_is_last_substep = false;
PROFILER_PUSH_CPU_MARKER("Main loop", 0xFF, 0x00, 0xF7); PROFILER_PUSH_CPU_MARKER("Main loop", 0xFF, 0x00, 0xF7);
m_prev_time = m_curr_time; left_over_time += getLimitedDt();
float dt = getLimitedDt(); int num_steps = int(left_over_time * stk_config->m_physics_fps);
float dt = 1.0f / stk_config->m_physics_fps;
left_over_time -= num_steps * dt ;
// Add a Time step entry to the rewind list, which can store all // Add a Time step entry to the rewind list, which can store all
// all input ecents being issued during the driver update. // all input ecents being issued during the driver update.
@ -277,38 +284,21 @@ void MainLoop::run()
if (!m_abort && !ProfileWorld::isNoGraphics()) if (!m_abort && !ProfileWorld::isNoGraphics())
{ {
float frame_duration = num_steps * dt;
// 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(dt); irr_driver->update(frame_duration);
PROFILER_POP_CPU_MARKER();
}
PROFILER_PUSH_CPU_MARKER("Update race", 0, 255, 255);
updateRace(dt); // Doesn't do anything if race is not active
PROFILER_POP_CPU_MARKER(); PROFILER_POP_CPU_MARKER();
// We need to check again because update_race may have requested
// the main loop to abort; and it's not a good idea to continue
// since the GUI engine is no more to be called then.
if (m_abort) break;
// Only do music, input, and graphics update if graphics are
// enabled.
if (!ProfileWorld::isNoGraphics())
{
PROFILER_PUSH_CPU_MARKER("Input/GUI", 0x7F, 0x00, 0x00); PROFILER_PUSH_CPU_MARKER("Input/GUI", 0x7F, 0x00, 0x00);
input_manager->update(dt); #ifdef ENABLE_WIIUSE
#ifdef ENABLE_WIIUSE
wiimote_manager->update(); wiimote_manager->update();
#endif #endif
input_manager->update(frame_duration);
GUIEngine::update(dt); GUIEngine::update(frame_duration);
PROFILER_POP_CPU_MARKER(); PROFILER_POP_CPU_MARKER();
// Update sfx and music after graphics, so that graphics code
// can use as many threads as possible without interfering
// with audio
PROFILER_PUSH_CPU_MARKER("Music", 0x7F, 0x00, 0x00); PROFILER_PUSH_CPU_MARKER("Music", 0x7F, 0x00, 0x00);
SFXManager::get()->update(); SFXManager::get()->update();
PROFILER_POP_CPU_MARKER(); PROFILER_POP_CPU_MARKER();
@ -323,25 +313,42 @@ void MainLoop::run()
} }
PROFILER_POP_CPU_MARKER(); PROFILER_POP_CPU_MARKER();
PROFILER_PUSH_CPU_MARKER("Database polling update", 0x00, 0x7F, 0x7F);
Online::RequestManager::get()->update(frame_duration);
PROFILER_POP_CPU_MARKER();
} }
else
for(int i=0; i<num_steps; i++)
{ {
PROFILER_PUSH_CPU_MARKER("Protocol manager update", 0x7F, 0x00, 0x7F); // Enable last substep in last iteration
if(NetworkConfig::get()->isNetworking()) m_is_last_substep = (i == num_steps - 1);
PROFILER_PUSH_CPU_MARKER("Update race", 0, 255, 255);
if (World::getWorld()) updateRace(dt);
PROFILER_POP_CPU_MARKER();
// We need to check again because update_race may have requested
// the main loop to abort; and it's not a good idea to continue
// since the GUI engine is no more to be called then.
if (m_abort) break;
if (!ProfileWorld::isNoGraphics() && STKHost::existHost() &&
STKHost::get()->requestedShutdown() )
{
STKHost::get()->shutdown();
}
PROFILER_PUSH_CPU_MARKER("Protocol manager update",
0x7F, 0x00, 0x7F);
if (NetworkConfig::get()->isNetworking())
ProtocolManager::getInstance()->update(dt); ProtocolManager::getInstance()->update(dt);
PROFILER_POP_CPU_MARKER(); PROFILER_POP_CPU_MARKER();
} if (World::getWorld()) World::getWorld()->updateTime(dt);
PROFILER_PUSH_CPU_MARKER("Database polling update", 0x00, 0x7F, 0x7F); } // for i < num_steps
Online::RequestManager::get()->update(dt); m_is_last_substep = false;
PROFILER_POP_CPU_MARKER(); PROFILER_POP_CPU_MARKER(); // MainLoop pop
if (World::getWorld() )
{
World::getWorld()->updateTime(dt);
}
PROFILER_POP_CPU_MARKER();
PROFILER_SYNC_FRAME(); PROFILER_SYNC_FRAME();
} // while !m_abort } // while !m_abort

View File

@ -34,6 +34,10 @@ private:
/** True if the frame rate should be throttled. */ /** True if the frame rate should be throttled. */
bool m_throttle_fps; bool m_throttle_fps;
/** True during the last substep of the inner main loop (where world
* is updated). Used to reduce amount of updates (e.g. sfx positions
* etc). */
bool m_is_last_substep;
Uint32 m_curr_time; Uint32 m_curr_time;
Uint32 m_prev_time; Uint32 m_prev_time;
float getLimitedDt(); float getLimitedDt();
@ -47,6 +51,10 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns true if STK is to be stoppe. */ /** Returns true if STK is to be stoppe. */
bool isAborted() const { return m_abort; } bool isAborted() const { return m_abort; }
// ------------------------------------------------------------------------
/** Returns if this is the last substep. Used to reduce the amount
* of updates (e.g. to sfx position) to once per rendered frame. */
bool isLstSubstep() const { return m_is_last_substep; }
}; // MainLoop }; // MainLoop
extern MainLoop* main_loop; extern MainLoop* main_loop;

View File

@ -160,7 +160,7 @@ void RewindQueue::insertRewindInfo(RewindInfo *ri)
// takes a long time - e.g. in networking startup), i.e. before a TimeStep // takes a long time - e.g. in networking startup), i.e. before a TimeStep
// info was added. Since this is mostly for debugging, just ignore this // info was added. Since this is mostly for debugging, just ignore this
// this for now. // this for now.
if(bucket!=m_time_step_info.end())) if(bucket!=m_time_step_info.end())
(*bucket)->insert(ri); (*bucket)->insert(ri);
} // insertRewindInfo } // insertRewindInfo

View File

@ -151,17 +151,10 @@ void Physics::update(float dt)
// of objects. // of objects.
m_all_collisions.clear(); m_all_collisions.clear();
// Maximum of three substeps. This will work for framerate down to // Since the world update (which calls physics update) is called at the
// 20 FPS (bullet default frequency is 60 HZ). // fixed frequency necessary for the physics update, we need to do exactly
int max_num_steps = 6; // one physic step only.
if (NetworkConfig::get()->isNetworking()) m_dynamics_world->stepSimulation(dt, 1, 1.0f / stk_config->m_physics_fps);
{
// In networking we have to make sure that we run the right number
// of physics step, otherwise the results diverge too much
max_num_steps = 999;
}
m_dynamics_world->stepSimulation(dt, max_num_steps,
1.0f/stk_config->m_physics_fps);
// Now handle the actual collision. Note: flyables can not be removed // Now handle the actual collision. Note: flyables can not be removed
// inside of this loop, since the same flyables might hit more than one // inside of this loop, since the same flyables might hit more than one