How to properly close/terminate a simple Windows app

1

Having such a simple DirectX application:

#include <d3dx9.h>
#include <tchar.h>
#include <chrono>
#include "Utils.h"

//#pragma comment (lib, "d3d9.lib")
//#pragma comment (lib, "d3dx9.lib")

using namespace std::chrono_literals;
using namespace std::chrono;

const wchar_t g_szClassName[] = _T("myWindowClass");

// we use a fixed timestep of 1 / (60 fps) = 16 milliseconds
//constexpr std::chrono::nanoseconds timestep(16ms);
constexpr std::chrono::nanoseconds timestep(1000ms);

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

wchar_t msgbuf[100];
int loop_cnt = 0;

bool handle_events() {
    // poll for events

    return false; // true if the user wants to quit the game
}

void update(long long lag) {
    // update game logic here
    swprintf_s(msgbuf, _T("update - loop_cnt: %d\n"), loop_cnt++);
    OutputDebugString(msgbuf);

}

void render() {
    // render stuff here
}

VOID OnPaint(HDC hdc) {
}

// ------------ D3D ------------

IDirect3D9* pD3D;
IDirect3DDevice9* pDev;
IDirect3DVertexBuffer9* pVB;

const DWORD OURVERT_FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;

struct OurVertex {
    float x, y, z;  // pozycja
    float rhw;          // komponent rhw
    D3DCOLOR color; // kolor
};

// -----------------------------

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {

    using clock = std::chrono::high_resolution_clock;

    WNDCLASSEX wc;
    HWND hWnd;
    MSG Msg = {0};

    std::chrono::nanoseconds lag(0ns);
    auto start_time = clock::now();
    bool quit_game = false;

    //Step 1: Registering the Window Class
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hWnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        _T("The title of my window"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        480,    // initial x size
        640,    // initial y size
        NULL, 
        NULL, 
        hInstance, 
        NULL
    );

    OutputDebugString(_T("--> WinMain <-- \n"));

    if (hWnd == NULL) {
        MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    // ------------ D3D ------------

    pD3D = Direct3DCreate9(D3D_SDK_VERSION);
    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.Windowed = true;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;

    OurVertex verts[] = {
        { 20.0f, 20.0f, 0.5f, 1.0f, 0xffff0000, },
        { 100.0f, 20.0f, 0.5f, 1.0f, 0xff00ff00, },
        { 20.0f, 100.0f, 0.5f, 1.0f, 0xff00ff55, },
        { 100.0f, 100.0f, 0.5f, 1.0f, 0xff0000ff},
    };

    pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &pDev);

    HRESULT hr = pDev->CreateVertexBuffer(4*sizeof(OurVertex), D3DUSAGE_DYNAMIC, OURVERT_FVF, D3DPOOL_DEFAULT, &pVB, 0);
    check_HRESULT(_T("CreateVertexBuffer"), hr);

    void* data;

    pVB->Lock(0, 4*sizeof(OurVertex), &data, D3DLOCK_DISCARD);
    memcpy(data, (void*)verts, sizeof(verts));
    pVB->Unlock();

    // -----------------------------


    // Step 3: The Message Loop
    while (TRUE) {

        while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) > 0) {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }

        if (Msg.message == WM_QUIT)
            break;

        //  Run game logic

            auto current_time = clock::now();
            auto frame_time = current_time - start_time;
            start_time = current_time;
            lag += duration_cast<nanoseconds>(frame_time);

            // update game logic as lag permits
            while (lag >= timestep) {
                lag -= timestep;

                pDev->Clear(0, 0, D3DCLEAR_TARGET, 0xff000000, 1, 0);
                pDev->BeginScene();
                pDev->SetFVF(OURVERT_FVF);
                pDev->SetStreamSource(0, pVB, 0, sizeof(OurVertex));
                pDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
                pDev->EndScene();
                pDev->Present(0, 0, 0, 0);

                update(lag.count()); // update at a fixed rate each time
            }

    }

    OutputDebugString(_T("--> AFTER MSG LOOP END <-- \n"));

    return Msg.wParam;
}

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    switch (msg) {

        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }

    return 0;
}

When i run debug (without any brakpoints defined) i get the output:

--> WinMain <-- 
update - loop_cnt: 0
update - loop_cnt: 1
update - loop_cnt: 2
update - loop_cnt: 3
update - loop_cnt: 4
update - loop_cnt: 5
...

The program successfully displays a colored rectangle as expected. But, something is going wrong here, because when I click the standard "X" close button in the right upper corner of the app window, the window closes but the program seems not to end properly. I still receive update - loop_cnt: # messages in the Output window in my Visual Studio, and there is no --> AFTER MSG LOOP END <-- message suggesting proper program termination.

What's more, when I try to run debug again, I get an error:

LINK : fatal error LNK1168: cannot open ...Game_Loop_Windows_1.exe for writing
...
MSB6006: "link.exe" exited with code 1168.
1>Done building project "Game_Loop_Windows_1.vcxproj" -- FAILED

What can be wrong with the code?

PS. I'm using Microsoft Visual Studio Community 2019 on Windows 10.

c++
winapi
visual-c++
asked on Stack Overflow May 19, 2021 by Daros911 • edited May 19, 2021 by Remy Lebeau

1 Answer

2

The message loop likely never terminates.

while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) > 0) {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}

if (Msg.message == WM_QUIT)
    break;

You are handling all messages, but only check that last one whether it was a WM_QUIT message. Instead, you would need to check all messages and properly exit the loop, something like this:

bool running{true};

while (running) {
    while (running && PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) > 0) {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
        running = Msg.message != WM_QUIT;
    }

    // Not strictly required; you can run another render loop and have
    // the outermost loop exit when running is false
    if (!running)
        break;

The link error simply means that the process is still running. An executable image that's mapped into a process cannot be written to. That's what the linker is complaining about.

answered on Stack Overflow May 19, 2021 by IInspectable • edited May 19, 2021 by IInspectable

User contributions licensed under CC BY-SA 3.0