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;
};