I think it has to do with DispatchMessage and the Window Procedure probably wrong though been spending a long time on this I rather ask for help now I might have done something wrong looked everywhere to find out the cause of this I might just be stupid and not realizing what the problem is itself I created a structure to hold everything for my window I am trying to make a wrapper CreateWindowApplication is there so I can create a window everything works except when I close it.
// Header
typedef struct WindowApplication WindowApplication;
struct WindowApplication
{
HINSTANCE hInstance;
HWND hwnd;
const wchar_t* title;
int width;
int height;
UINT flags;
int shouldClose;
WindowApplication* next;
WindowApplication* prev;
WindowApplication* windowdata;
};
// WindowApplication.c
WindowApplication* CreateWindowApplication(const wchar_t* title, int width, int height, UINT flags)
{
WindowApplication* window;
window = (WindowApplication*)calloc(1, sizeof(WindowApplication));
window->title = title;
window->width = width;
window->height = height;
window->flags = flags;
window->next = window->windowdata;
window->windowdata = window;
WindowApplicationWin32(window);
return window;
};
// WindowApplicationWin32.c
void WindowApplicationWin32(WindowApplication* window)
{
WNDCLASSEXW wc = {};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.hInstance = window->hInstance;
wc.lpszClassName = window->title;
wc.lpszMenuName = window->title;
wc.hIcon = (HICON)LoadImageW(window->hInstance, L"icon.ico", IMAGE_ICON, 512, 512, LR_DEFAULTCOLOR);
wc.hIconSm = (HICON)LoadImageW(window->hInstance, L"icon.ico", IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
wc.hCursor = LoadCursorW(window->hInstance, IDC_ARROW);
wc.hbrBackground = 0;
RegisterClassExW(&wc);
window->hwnd = CreateWindowExW(0,
window->title,
window->title,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
window->width,
window->height,
0,
0,
0,
0);
ShowWindow(window->hwnd, SW_SHOW);
};
int WindowApplicationWin32Events(WindowApplication* window)
{
MSG msg = {};
while(PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
};
// main.c
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
WindowApplication* window = WindowApplicationWindow(L"Demo", 640, 480, 0);
while(!WindowApplicationShouldClose(window))
{
WindowEvents(window);
};
return 0;
};
CodePudding user response:
Okay, after a lot of changes I kinda got it running. I think the issue is that after PostQuitMessage(0);
function WindowApplicationWin32Events
does not return anything and even if it returns it should be returning final message in the function WinMain
. This is not happening, so instead execution just goes back to WindowApplicationWin32Events
and gets stuck there.
Solution could be to do return (int) msg.wParam;
at the end of WindowApplicationWin32Events
and then in WinMain
WindowApplicationWin32Events
result should be final return instead of return 0;
You can check it out here: http://www.winprog.org/tutorial/simple_window.html
EDIT:
Another thing is that "Unlike GetMessage, the PeekMessage function does not wait for a message to be posted before returning." So it exits loop quickly after no messages and then keeps coming back to WindowApplicationWin32Events
like crazy and can be triggering error reporting (werfault.exe).
Get vs Peek: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessage