Got rid of WinMain

This commit is contained in:
2026-07-02 23:19:58 +03:00
parent 8767a14e01
commit ea8c4bba36

View File

@@ -26,8 +26,7 @@
DynArray_DynString wgl_extensions = { 0 }; DynArray_DynString wgl_extensions = { 0 };
typedef struct { typedef struct {
const char* msg; HGLRC gl_ctx;
int num;
} State; } State;
bool has_wgl_extension(const DynString* name) { bool has_wgl_extension(const DynString* name) {
@@ -86,15 +85,15 @@ void OnSize(HWND hwnd, UINT flag, int width, int height) {
} }
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
// State* pState = NULL; State* pState = NULL;
// if (uMsg == WM_CREATE) { if (uMsg == WM_CREATE) {
// CREATESTRUCT* pCreate = (CREATESTRUCT*) lParam; CREATESTRUCT* pCreate = (CREATESTRUCT*) lParam;
// pState = (State*) pCreate->lpCreateParams; pState = (State*) pCreate->lpCreateParams;
// SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pState); SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pState);
// } else { } else {
// LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA); LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA);
// pState = (State*) ptr; pState = (State*) ptr;
// } }
switch (uMsg) { switch (uMsg) {
// case WM_LBUTTONDOWN: { // case WM_LBUTTONDOWN: {
@@ -146,13 +145,13 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
return 1; return 1;
} }
HGLRC gl_ctx = wglCreateContext(hdc); pState->gl_ctx = wglCreateContext(hdc);
if (gl_ctx == NULL) { if (pState->gl_ctx == NULL) {
LOG_ERRORF("Failed to create OpenGL context. Error: %d", GetLastError()); LOG_ERRORF("Failed to create OpenGL context. Error: %d", GetLastError());
return 1; return 1;
} }
if (wglMakeCurrent(hdc, gl_ctx) == FALSE) { if (wglMakeCurrent(hdc, pState->gl_ctx) == FALSE) {
LOG_ERRORF("Failed to make OpenGL context current. Error: %d", GetLastError()); LOG_ERRORF("Failed to make OpenGL context current. Error: %d", GetLastError());
return 1; return 1;
} }
@@ -230,7 +229,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
LOG_FATAL("Failed to set wgl_pixel_format"); LOG_FATAL("Failed to set wgl_pixel_format");
} }
if (wglDeleteContext(gl_ctx) == FALSE) { if (wglDeleteContext(pState->gl_ctx) == FALSE) {
LOG_FATAL("Failed to delete dummy context"); LOG_FATAL("Failed to delete dummy context");
} }
@@ -241,13 +240,13 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB
}; };
gl_ctx = wglCreateContextAttribsARB(hdc, NULL, attribIList); pState->gl_ctx = wglCreateContextAttribsARB(hdc, NULL, attribIList);
if (gl_ctx == NULL) { if (pState->gl_ctx == NULL) {
LOG_FATAL("wglCreateContextAttribsARB did not create a context"); LOG_FATAL("wglCreateContextAttribsARB did not create a context");
} }
} }
if (wglMakeCurrent(hdc, gl_ctx) == FALSE) { if (wglMakeCurrent(hdc, pState->gl_ctx) == FALSE) {
LOG_FATAL("Failed to set proper context as current"); LOG_FATAL("Failed to set proper context as current");
} }
@@ -261,11 +260,12 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
case WM_KEYDOWN: case WM_KEYDOWN:
if (wParam == VK_ESCAPE) { if (wParam == VK_ESCAPE) {
PostQuitMessage(0); PostMessage(hwnd, WM_DESTROY, 0, 0);
} }
return 0; return 0;
case WM_DESTROY: case WM_DESTROY:
wglDeleteContext(pState->gl_ctx);
PostQuitMessage(0); PostQuitMessage(0);
return 0; return 0;
@@ -302,25 +302,27 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
return DefWindowProc(hwnd, uMsg, wParam, lParam); return DefWindowProc(hwnd, uMsg, wParam, lParam);
} }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) { int main() {
// opengl_module = LoadLibraryA("opengl32.dll");
pltf_init(); pltf_init();
HMODULE module = GetModuleHandle(NULL);
if (module == NULL) {
LOG_FATAL("Failed to get module");
}
// Register the window class // Register the window class
const wchar_t* CLASS_NAME = L"Sample Window Class"; const wchar_t* CLASS_NAME = L"Sample Window Class";
WNDCLASS wc = { }; WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc; wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance; wc.hInstance = module;
wc.lpszClassName = CLASS_NAME; wc.lpszClassName = CLASS_NAME;
wc.style = CS_OWNDC; wc.style = CS_OWNDC;
RegisterClass(&wc); RegisterClass(&wc);
State s = { State s = {
.msg = "This is state", .gl_ctx = NULL
.num = 69420
}; };
// Create the window // Create the window
@@ -335,7 +337,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
NULL, // Parent window NULL, // Parent window
NULL, // Menu NULL, // Menu
hInstance, // Instance handle module, // Instance handle
&s // Additional application data &s // Additional application data
); );
@@ -344,7 +346,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
return 1; return 1;
} }
ShowWindow(hwnd, nCmdShow); ShowWindow(hwnd, SW_SHOWDEFAULT);
// Message loop // Message loop
@@ -403,12 +405,14 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
); );
MSG msg = { 0 }; MSG msg = { 0 };
int exit_code = 0;
while (true) { while (true) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
if (msg.message == WM_QUIT) { if (msg.message == WM_QUIT) {
break; exit_code = msg.wParam;
goto cleanup;
} }
} }
@@ -422,13 +426,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
} }
return 0; return 0;
}
// #else cleanup:
// UnregisterClass(CLASS_NAME, module);
// int main() { return exit_code;
// _main();
// return 0;
// } return 0;
// }
// #endif