walbourn / directx-vs-templates

Direct3D Visual Studio Templates
https://walbourn.github.io/direct3d-game-visual-studio-templates-redux/
MIT License
402 stars 53 forks source link

Move setting game object for user pointer into WM_CREATE #52

Closed walbourn closed 1 year ago

walbourn commented 2 years ago

The existing templates for Win32 use SetWindowsLongPtr after creating the window and showing it.

The official DirectX-Graphics-Samples projects use WM_CREATE and the lpParam on CreateWindow instead.

HWND hwnd = CreateWindowExW(0, ..., g_game.get());
    switch (message)
    {
    case WM_CREATE:
        if (lParam)
        {
            auto pCreateStruct = reinterpret_cast<LPCREATESTRUCTW>(lParam);
            SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pCreateStruct->lpCreateParams));
        }
        return 0;

Note this works fine, but needs one tweak to DeviceResources:

bool DeviceResources::WindowSizeChanged(int width, int height)
{
    if (!m_window)
        return false;
...
walbourn commented 1 year ago

https://github.com/walbourn/directx-vs-templates/commit/6fa457f4466e5e59d4362b4e4b4d2c45a2e56c80