locked
Where to put animation loop in Direct 2D app (XAML)

    Question

  • Environment: Visual Studio 2012 RC, Direct2D App (XAML) template.

    I create a project using the Direct2D App (XAML) template. I want to have a loop to run my animation. I am not sure where to put that loop in this type of project. When I do a non-XAML Direct2D metro app (like many of the samples) I just put the loop in Run:

    void D2DBasicAnimation::Run()
    {
     while (!m_windowClosed)
     {
      m_window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
      if (m_run) 
      {
       m_timer->Update();
       Render();
       Present();
      }
     }
    }
    

    What is the proper way to do this in the Direct2D App (XAML) template?

    Thank you!

    Wednesday, June 27, 2012 10:28 PM

Answers

  • Hi, the Direct2D App (XAML) template subscribe to the CompositionTarget::Rendering event in DirectXPage.xaml.cpp file.
    • Marked as answer by mcngraphics Tuesday, July 03, 2012 2:50 PM
    Thursday, June 28, 2012 12:52 AM
  • CompositionTarget::Rendering fires on the XAML app's UI thread every time the app is rendering a new frame of XAML content (which caps at the screen's refresh rate, in this case the 60 fps you're seeing).  This makes it a convenient callback to synchronize updates between DX and XAML content since you can only modify XAML elements on the XAML UI thread.  The frequency can be lower than 60fps if you overload your UI thread with heavy synchronous processing.  This topic has some details about keeping the UI thread responsive:

    http://msdn.microsoft.com/en-us/library/windows/apps/Hh994635.aspx

    You should alternatively be able to render and present your DX content to an ISwapChainBackgroundPanelNative element from another thread, but that introduces the usual cross-thread considerations if you need to synchronize updates between the DX and XAML content.

    • Marked as answer by mcngraphics Tuesday, July 03, 2012 2:49 PM
    Monday, July 02, 2012 5:00 PM

All replies

  • Hi, the Direct2D App (XAML) template subscribe to the CompositionTarget::Rendering event in DirectXPage.xaml.cpp file.
    • Marked as answer by mcngraphics Tuesday, July 03, 2012 2:50 PM
    Thursday, June 28, 2012 12:52 AM
  • Ahhh...

    I see the Direct2D App (XAML) template (C++) adds:

    void DirectXPage::OnRendering(Platform::Object^ sender, Platform::Object^ args)
    {
     if (m_renderNeeded)
     {
      m_timer->Update();
      m_renderer->Update(m_timer->Total, m_timer->Delta);
      m_renderer->Render();
      m_renderer->Present();
      m_renderNeeded = false;
     }
    }

    OK that is great and I will use that for my animation loop.

    So what is going on here? It seems like that event is being called 60x/second? How does the event decide to use (in my case) 60Hz? Is it the system's high-resolution performance counter? If so what if the app runs on a system without a high-resolution performance counter -- would this event fire? (or is that a non issue for most systems running Windows 8)?

    Thank you for your time and help!


    • Edited by mcngraphics Thursday, June 28, 2012 4:28 AM
    Thursday, June 28, 2012 4:18 AM
  • Hello,

     

    I think you can follow this Direct2D magazine app sample to finish the loop.

    http://code.msdn.microsoft.com/windowsapps/Magazine-Sample-2a657289

     

    Best regards,

    Jesse



    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, June 28, 2012 7:20 AM
    Moderator
  • CompositionTarget::Rendering fires on the XAML app's UI thread every time the app is rendering a new frame of XAML content (which caps at the screen's refresh rate, in this case the 60 fps you're seeing).  This makes it a convenient callback to synchronize updates between DX and XAML content since you can only modify XAML elements on the XAML UI thread.  The frequency can be lower than 60fps if you overload your UI thread with heavy synchronous processing.  This topic has some details about keeping the UI thread responsive:

    http://msdn.microsoft.com/en-us/library/windows/apps/Hh994635.aspx

    You should alternatively be able to render and present your DX content to an ISwapChainBackgroundPanelNative element from another thread, but that introduces the usual cross-thread considerations if you need to synchronize updates between the DX and XAML content.

    • Marked as answer by mcngraphics Tuesday, July 03, 2012 2:49 PM
    Monday, July 02, 2012 5:00 PM