C++ animate anything correctly

someone 20 Reputation points
2024-12-27T21:21:56.48+00:00

I want to understand how to correctly display something in win32.

For example, how to make a square that will move with sin() up and down, so that it is correct and not as I did it through CreateCompatibleDC and create a Bitmap and then move all of it and all this in one thread that was slower to respond with each line of code. Without using wm_paint and wm_timer*

I hope the translator got it right : 3

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,704 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,818 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 86,481 Reputation points
    2024-12-28T13:05:49.12+00:00

    For animations, you must use Direct2D (it uses GPU, a lot smoother than GDI/GDI+, using CPU), with a Device Context for recent OS as explained at : https://learn.microsoft.com/en-us/windows/win32/direct2d/devices-and-device-contexts

    It seems complicated the 1rst time, but afterwards you just have to copy/paste all same initializations, and the main drawing code is inside BeginDraw/EndDraw.

    You can see MS SDK samples like SimplePathAnimationSample or TextAnimationSample at https://github.com/microsoft/Windows-classic-samples/tree/main/Samples/Win7Samples/multimedia/Direct2D

    Animation is done "automatically" with InvalidateRect(m_hwnd, NULL, FALSE);

    which generates frames at screen refresh rate (60 Hz on my PC)

    For a lot of bitmaps, an efficient way is with ID2D1SpriteBatch

    (I used sprites in some WinUI 3 / C# test samples (easy to convert into C++)

    like WinUI3_SwapChainPanel_Direct2D or WinUI3_Direct2D_Composition )

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.