Direct2D and render update (help)

Code Wanderer 396 Reputation points
2021-05-13T18:48:30.47+00:00

I am learning Direct2D from MSD documentation. I created one object which is "animated" (resizing to desired size and back in infinite loop) and want update it. Problem is, I can't find any info about that, therefore I start experimenting.

What I did:

// main loop
while (RUN)
 {
 if (ok = GetMessage(&msg, NULL, 0, 0) > 0)
 {
 TranslateMessage(&msg);
 DispatchMessage(&msg);
 }
 else RUN = false;
 mw.Update();
 mw.Render();
 }

// HandleMessage(...)
case WM_PAINT:
{
m_program.Update();
m_renderer.Render();
return 0;
}
break;

I don't call any SendMessage, InvalidateRect inside my application. I am satisfied with this because I can resize or move with window and animated object is still animated during resizing or moving with window.

I have question about if I am doing it right, or are there better ways to achieve animated object still animated while windows is resizing and moving?

What about multithreading way, where second thread will rendering and main thread working with windows messages? Can Direct2D update window from second thread automatically (with desired effect where animated object still will be animated while window resizeing)?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,355 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,443 questions
0 comments No comments
{count} votes

Accepted answer
  1. Code Wanderer 396 Reputation points
    2021-05-16T12:58:56.7+00:00

    I found answert to my question:

    Can Direct2D update window from second thread automatically (with desired effect where animated object still will be animated while window resizeing)?

    When rendering is done in second thread, it automatically update the window (if hwndRenderTarget is used or UpdateLayeredWindow) but for resizeing there should be lock used (if std::thread is used), if Direct2D is used as single thread, othervise renderTarget.Resize(x, y) can be called from any thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Song Zhu - MSFT 906 Reputation points
    2021-05-14T06:46:42.36+00:00

    This is already a good way to achieve this feature.

    Using multithreading may bring you better performance and responsiveness. But implementing it is very complicated, you can refer to: Multithreaded Direct2D Apps, About multithreading in a Direct2D drawing call


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.