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,7 +575,8 @@ s32 CIrrDeviceAndroid::handleKeyboard(AInputEvent* androidEvent)
int32_t keyAction = AKeyEvent_getAction(androidEvent);
int32_t keyMetaState = AKeyEvent_getMetaState(androidEvent);
int32_t keyRepeat = AKeyEvent_getRepeatCount(androidEvent);
int32_t scanCode = AKeyEvent_getScanCode(androidEvent);
if (keyAction == AKEY_EVENT_ACTION_DOWN)
{
event.KeyInput.PressedDown = true;
@ -609,22 +610,28 @@ s32 CIrrDeviceAndroid::handleKeyboard(AInputEvent* androidEvent)
{
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
// 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
// AKEYCODE_BACK key event
if (event.KeyInput.SystemKeyCode == AKEYCODE_ESCAPE)
// Mark escape key and gamepad buttons as handled by application to avoid
// receiving duplicated events
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;
}
@ -740,25 +747,30 @@ s32 CIrrDeviceAndroid::handleGamepad(AInputEvent* androidEvent)
int32_t keyCode = AKeyEvent_getKeyCode(androidEvent);
int32_t keyAction = AKeyEvent_getAction(androidEvent);
int32_t keyRepeat = AKeyEvent_getRepeatCount(androidEvent);
int32_t scanCode = AKeyEvent_getScanCode(androidEvent);
SEvent event;
event.EventType = EET_KEY_INPUT_EVENT;
event.KeyInput.Char = 0;
event.KeyInput.PressedDown = (keyAction == AKEY_EVENT_ACTION_DOWN);
event.KeyInput.Shift = false;
event.KeyInput.Control = false;
event.KeyInput.SystemKeyCode = (u32)keyCode;
event.KeyInput.Key = KeyMap[keyCode];
// Handle an event when back button in pressed just like an escape key
// and also avoid repeating the event to avoid some strange behaviour
if (event.KeyInput.SystemKeyCode == AKEYCODE_BACK)
if (keyRepeat == 0)
{
status = 1;
ignore = (event.KeyInput.PressedDown == false || keyRepeat > 0);
bool ignore_event = false;
SEvent event;
event.EventType = EET_KEY_INPUT_EVENT;
event.KeyInput.Char = 0;
event.KeyInput.PressedDown = (keyAction == AKEY_EVENT_ACTION_DOWN);
event.KeyInput.Shift = false;
event.KeyInput.Control = false;
event.KeyInput.SystemKeyCode = (u32)keyCode;
event.KeyInput.Key = KeyMap[keyCode];
if (event.KeyInput.Key == 0)
{
event.KeyInput.Key = (EKEY_CODE)scanCode;
}
postEventFromUser(event);
}
postEventFromUser(event);
status = 1;
break;
}
default:
@ -874,7 +886,7 @@ void CIrrDeviceAndroid::createKeyMap()
// following look like controller inputs
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_X] = IRR_KEY_3;
KeyMap[AKEYCODE_BUTTON_Y] = IRR_KEY_4;
@ -886,7 +898,7 @@ void CIrrDeviceAndroid::createKeyMap()
KeyMap[AKEYCODE_BUTTON_THUMBL] = IRR_KEY_RETURN;
KeyMap[AKEYCODE_BUTTON_THUMBR] = 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_ESCAPE] = IRR_KEY_ESCAPE;

View File

@ -1,5 +1,5 @@
# Modify this file to change the last-modified date when you add/remove a file.
# This will then trigger a new cmake run automatically.
# This will then trigger a new cmake run automatically.
file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp")
file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp")
file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*")

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_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_default_moveable_friction, "physics default-moveable-friction");
// Square distance to make distance checks cheaper (no sqrt)

View File

@ -233,19 +233,27 @@ void IrrDriver::updateConfigIfRelevant()
core::recti IrrDriver::getSplitscreenWindow(int WindowNum)
{
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 cols = UserConfigParams::split_screen_horizontally ? round(playernum_sqrt) : ceil(playernum_sqrt);
int rows = int( UserConfigParams::split_screen_horizontally
? 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 (cols == 0) {cols = 1;}
//This could add a bit of overhang
const int width_of_space = ceil((float)irr_driver->getActualScreenSize().Width / (float)cols);
const int height_of_space = ceil((float)irr_driver->getActualScreenSize().Height / (float)rows);
const int width_of_space =
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 y_grid_Position = floor((WindowNum) / cols);
const int y_grid_Position = int(floor((WindowNum) / cols));
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

View File

@ -60,6 +60,7 @@
#include "karts/max_speed.hpp"
#include "karts/rescue_animation.hpp"
#include "karts/skidding.hpp"
#include "main_loop.hpp"
#include "modes/overworld.hpp"
#include "modes/soccer_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
// 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_PAUSED))
{
@ -2403,10 +2404,13 @@ void Kart::updatePhysics(float dt)
*/
void Kart::updateEngineSFX(float dt)
{
// when going faster, use higher pitch for engine
if(!m_engine_sound || !SFXManager::get()->sfxAllowed())
// Only update SFX during the last substep (otherwise too many SFX commands
// in one frame), and if sfx are enabled
if(!m_engine_sound || !SFXManager::get()->sfxAllowed() ||
!main_loop->isLstSubstep() )
return;
// when going faster, use higher pitch for engine
if(isOnGround())
{
float max_speed = m_kart_properties->getEngineMaxSpeed();

View File

@ -46,9 +46,10 @@ MainLoop* main_loop = 0;
MainLoop::MainLoop() :
m_abort(false)
{
m_curr_time = 0;
m_prev_time = 0;
m_throttle_fps = true;
m_curr_time = 0;
m_prev_time = 0;
m_throttle_fps = true;
m_is_last_substep = false;
} // MainLoop
//-----------------------------------------------------------------------------
@ -63,6 +64,7 @@ MainLoop::~MainLoop()
*/
float MainLoop::getLimitedDt()
{
m_prev_time = m_curr_time;
float dt = 0;
// 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();
m_prev_time = m_curr_time;
while( 1 )
{
@ -260,12 +261,18 @@ void MainLoop::run()
IrrlichtDevice* device = irr_driver->getDevice();
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)
{
m_is_last_substep = false;
PROFILER_PUSH_CPU_MARKER("Main loop", 0xFF, 0x00, 0xF7);
m_prev_time = m_curr_time;
float dt = getLimitedDt();
left_over_time += 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
// all input ecents being issued during the driver update.
@ -277,38 +284,21 @@ void MainLoop::run()
if (!m_abort && !ProfileWorld::isNoGraphics())
{
float frame_duration = num_steps * dt;
// Render the previous frame, and also handle all user input.
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();
// 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);
input_manager->update(dt);
#ifdef ENABLE_WIIUSE
wiimote_manager->update();
#endif
GUIEngine::update(dt);
#ifdef ENABLE_WIIUSE
wiimote_manager->update();
#endif
input_manager->update(frame_duration);
GUIEngine::update(frame_duration);
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);
SFXManager::get()->update();
PROFILER_POP_CPU_MARKER();
@ -323,25 +313,42 @@ void MainLoop::run()
}
PROFILER_POP_CPU_MARKER();
}
else
{
PROFILER_PUSH_CPU_MARKER("Protocol manager update", 0x7F, 0x00, 0x7F);
if(NetworkConfig::get()->isNetworking())
ProtocolManager::getInstance()->update(dt);
PROFILER_PUSH_CPU_MARKER("Database polling update", 0x00, 0x7F, 0x7F);
Online::RequestManager::get()->update(frame_duration);
PROFILER_POP_CPU_MARKER();
}
PROFILER_PUSH_CPU_MARKER("Database polling update", 0x00, 0x7F, 0x7F);
Online::RequestManager::get()->update(dt);
PROFILER_POP_CPU_MARKER();
if (World::getWorld() )
for(int i=0; i<num_steps; i++)
{
World::getWorld()->updateTime(dt);
}
// Enable last substep in last iteration
m_is_last_substep = (i == num_steps - 1);
PROFILER_POP_CPU_MARKER();
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);
PROFILER_POP_CPU_MARKER();
if (World::getWorld()) World::getWorld()->updateTime(dt);
} // for i < num_steps
m_is_last_substep = false;
PROFILER_POP_CPU_MARKER(); // MainLoop pop
PROFILER_SYNC_FRAME();
} // while !m_abort

View File

@ -34,6 +34,10 @@ private:
/** True if the frame rate should be throttled. */
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_prev_time;
float getLimitedDt();
@ -47,6 +51,10 @@ public:
// ------------------------------------------------------------------------
/** Returns true if STK is to be stoppe. */
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
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
// info was added. Since this is mostly for debugging, just ignore this
// this for now.
if(bucket!=m_time_step_info.end()))
if(bucket!=m_time_step_info.end())
(*bucket)->insert(ri);
} // insertRewindInfo

View File

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