gpt4 book ai didi

c++ - StepTimer.GetTotalSeconds() 产生的值并不总是增加

转载 作者:行者123 更新时间:2023-11-28 04:59:57 26 4
gpt4 key购买 nike

我使用的是 DirectX 11。为简单起见并重现问题,我将问题缩小为以下步骤:

  1. 在 Visual Studio 中创建一个新的“DirectX 和 XAML 应用程序 (UWP)”(我使用的是 VS 2017)。
  2. 用以下代码替换 Sample3DSceneRenderer::Update 方法:

    void Sample3DSceneRenderer::Update(DX::StepTimer const& timer)
    {
    if (!m_tracking)
    {
    double total = timer.GetTotalSeconds();

    // Convert degrees to radians, then convert seconds to rotation angle
    float radiansPerSecond = XMConvertToRadians(m_degreesPerSecond);
    double totalRotation = total * radiansPerSecond;
    float radians = static_cast<float>(fmod(totalRotation, XM_2PI));

    DX::DebugTrace(L"num = %4.2f\t%4.2f\n", radians, total);

    Rotate(radians);
    }
    }
  3. 添加以下函数以跟踪“输出”窗口中的值:

    inline void DebugTrace(const wchar_t *format, ...)
    {
    // Generate the message string.
    va_list args;
    va_start(args, format); // initialize the argument list
    wchar_t buffer[1024];
    va_end(args);

    OutputDebugStringW(buffer); // this is a Windows function
    }

当我运行应用程序时,“输出”窗口显示以下值:

    num = 0.01  0.01
num = 0.02 0.02
num = 0.00 0.00 // decreased
num = 0.00 0.01 // decreased
num = 0.03 0.04
num = 0.05 0.06
num = 0.02 0.02 // decreased
num = 0.06 0.07
num = 0.03 0.04 // decreased
num = 0.07 0.09
num = 0.04 0.06 // decreased
num = 0.08 0.11
num = 0.06 0.07 // decreased
num = 0.10 0.12
num = 0.07 0.09 // decreased
num = 0.11 0.14
num = 0.08 0.11 // decreased
num = 0.12 0.16
num = 0.10 0.12 // decreased
num = 0.11 0.14
num = 0.14 0.17
num = 0.12 0.16 // decreased
num = 0.15 0.19
num = 0.16 0.21
num = 0.14 0.17 // decreased
num = 0.18 0.22
num = 0.15 0.19 // decreased
num = 0.16 0.21
num = 0.19 0.24
num = 0.20 0.26
num = 0.18 0.22 // decreased
etc.

问题:为什么 TotalSeconds 值会先增加然后减少然后再增加等等?例如:0.01、0.02、0.00、0.01。它们不应该一直增加吗?

最佳答案

错误在 App::OnLaunched 中。模板中的版本正在创建两个 DirectXPage:

  • 直接赋值:m_directXPage = ref new DirectXPage();
  • 导航:rootFrame->Navigate(TypeName(DirectXPage::typeid), e->Arguments);

我有一个修改后的版本,它仅通过引用导航期间创建的版本来创建一个版本,但我没有花太多时间检查错误情况。

void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e)
{
#if _DEBUG
if (IsDebuggerPresent())
{
DebugSettings->EnableFrameRateCounter = true;
}
#endif

auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);

// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == nullptr)
{
// Create a Frame to act as the navigation context and associate it with
// a SuspensionManager key
rootFrame = ref new Frame();

rootFrame->NavigationFailed += ref new Windows::UI::Xaml::Navigation::NavigationFailedEventHandler(this, &App::OnNavigationFailed);

// Place the frame in the current Window
Window::Current->Content = rootFrame;
}

if (rootFrame->Content == nullptr)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame->Navigate(TypeName(DirectXPage::typeid), e->Arguments);
}

if (m_directXPage == nullptr)
{
m_directXPage = dynamic_cast<DirectXPage^>(rootFrame->Content);
}

if (e->PreviousExecutionState == ApplicationExecutionState::Terminated)
{
m_directXPage->LoadInternalState(ApplicationData::Current->LocalSettings->Values);
}

// Ensure the current window is active
Window::Current->Activate();
}

关于c++ - StepTimer.GetTotalSeconds() 产生的值并不总是增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46311103/

26 4 0
文章推荐: c++ - Signal readyRead() 不执行槽
文章推荐: javascript - 如何在
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com