Ver Fonte

Got rid of WinMain

Aaro Saila há 4 meses atrás
pai
commit
ea8c4bba36
1 ficheiros alterados com 39 adições e 37 exclusões
  1. 39 37
      src/main.c

+ 39 - 37
src/main.c

@@ -26,8 +26,7 @@
 DynArray_DynString wgl_extensions = { 0 };
 
 typedef struct {
-    const char* msg;
-    int num;
+    HGLRC gl_ctx;
 } State;
 
 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) {
-    // State* pState = NULL;
-    // if (uMsg == WM_CREATE) {
-    //     CREATESTRUCT* pCreate = (CREATESTRUCT*) lParam;
-    //     pState = (State*) pCreate->lpCreateParams;
-    //     SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pState);
-    // } else {
-    //     LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA);
-    //     pState = (State*) ptr;
-    // }
+    State* pState = NULL;
+    if (uMsg == WM_CREATE) {
+        CREATESTRUCT* pCreate = (CREATESTRUCT*) lParam;
+        pState = (State*) pCreate->lpCreateParams;
+        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pState);
+    } else {
+        LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA);
+        pState = (State*) ptr;
+    }
 
     switch (uMsg) {
         // case WM_LBUTTONDOWN: {
@@ -146,13 +145,13 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
             return 1;
         }
 
-        HGLRC gl_ctx = wglCreateContext(hdc);
-        if (gl_ctx == NULL) {
+        pState->gl_ctx = wglCreateContext(hdc);
+        if (pState->gl_ctx == NULL) {
             LOG_ERRORF("Failed to create OpenGL context. Error: %d", GetLastError());
             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());
             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");
         }
 
-        if (wglDeleteContext(gl_ctx) == FALSE) {
+        if (wglDeleteContext(pState->gl_ctx) == FALSE) {
             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_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB
             };
-            gl_ctx = wglCreateContextAttribsARB(hdc, NULL, attribIList);
-            if (gl_ctx == NULL) {
+            pState->gl_ctx = wglCreateContextAttribsARB(hdc, NULL, attribIList);
+            if (pState->gl_ctx == NULL) {
                 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");
         }
 
@@ -261,11 +260,12 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 
     case WM_KEYDOWN:
         if (wParam == VK_ESCAPE) {
-            PostQuitMessage(0);
+            PostMessage(hwnd, WM_DESTROY, 0, 0);
         }
         return 0;
 
     case WM_DESTROY:
+        wglDeleteContext(pState->gl_ctx);
         PostQuitMessage(0);
         return 0;
 
@@ -302,25 +302,27 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
     return DefWindowProc(hwnd, uMsg, wParam, lParam);
 }
 
-int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
-    // opengl_module = LoadLibraryA("opengl32.dll");
-
+int main() {
     pltf_init();
 
+    HMODULE module = GetModuleHandle(NULL);
+    if (module == NULL) {
+        LOG_FATAL("Failed to get module");
+    }
+
     // Register the window class
     const wchar_t* CLASS_NAME = L"Sample Window Class";
 
     WNDCLASS wc = { };
     wc.lpfnWndProc = WindowProc;
-    wc.hInstance = hInstance;
+    wc.hInstance = module;
     wc.lpszClassName = CLASS_NAME;
     wc.style = CS_OWNDC;
 
     RegisterClass(&wc);
 
     State s = {
-        .msg = "This is state",
-        .num = 69420
+        .gl_ctx = NULL
     };
 
     // Create the window
@@ -335,7 +337,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
 
         NULL,      // Parent window
         NULL,      // Menu
-        hInstance, // Instance handle
+        module, // Instance handle
         &s         // Additional application data
     );
 
@@ -344,7 +346,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
         return 1;
     }
 
-    ShowWindow(hwnd, nCmdShow);
+    ShowWindow(hwnd, SW_SHOWDEFAULT);
 
     // Message loop
 
@@ -403,12 +405,14 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
     );
 
     MSG msg = { 0 };
+    int exit_code = 0;
     while (true) {
         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
             TranslateMessage(&msg);
             DispatchMessage(&msg);
             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;
-}
 
-// #else
-//
-// int main() {
-//     _main();
-//     return 0;
-// }
-//
-// #endif
+cleanup:
+    UnregisterClass(CLASS_NAME, module);
+    return exit_code;
+
+
+    return 0;
+}