+ {
+ CenterX = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
+ CenterY = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
+ }
+ CenterX = max(0, CenterX);
+ CenterY = max(0, CenterY);
+
+ // x and y may be changed by WM_MOVE messages
+ window_x = CenterX;
+ window_y = CenterY;
+ window_width = width;
+ window_height = height;
+ rect.left += CenterX;
+ rect.right += CenterX;
+ rect.top += CenterY;
+ rect.bottom += CenterY;
+
+ mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlacesWindowClass", gamename, WindowStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, global_hInstance, NULL);
+ if (!mainwindow)
+ {
+ Con_Printf("CreateWindowEx(%d, %s, %s, %d, %d, %d, %d, %d, %p, %p, %d, %p) failed\n", ExWindowStyle, "DarkPlacesWindowClass", gamename, WindowStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, global_hInstance, NULL);
+ VID_Shutdown();
+ return false;
+ }
+
+ /*
+ if (!fullscreen)
+ SetWindowPos (mainwindow, NULL, CenterX, CenterY, 0, 0,SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
+ */
+
+ ShowWindow (mainwindow, SW_SHOWDEFAULT);
+ UpdateWindow (mainwindow);
+
+ SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
+ SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
+
+ VID_UpdateWindowStatus ();
+
+ // now we try to make sure we get the focus on the mode switch, because
+ // sometimes in some systems we don't. We grab the foreground, then
+ // finish setting up, pump all our messages, and sleep for a little while
+ // to let messages finish bouncing around the system, then we put
+ // ourselves at the top of the z order, then grab the foreground again,
+ // Who knows if it helps, but it probably doesn't hurt
+ SetForegroundWindow (mainwindow);
+
+ while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
+ {
+ TranslateMessage (&msg);
+ DispatchMessage (&msg);
+ }
+
+ Sleep (100);
+
+ SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
+
+ SetForegroundWindow (mainwindow);
+
+ // fix the leftover Alt from any Alt-Tab or the like that switched us away
+ ClearAllStates ();
+
+ hdc = GetDC(mainwindow);
+
+ if ((pixelformat = ChoosePixelFormat(hdc, &pfd)) == 0)
+ {
+ VID_Shutdown();
+ Con_Printf("ChoosePixelFormat(%d, %p) failed\n", hdc, &pfd);
+ return false;
+ }
+
+ if (SetPixelFormat(hdc, pixelformat, &pfd) == false)
+ {
+ VID_Shutdown();
+ Con_Printf("SetPixelFormat(%d, %d, %p) failed\n", hdc, pixelformat, &pfd);
+ return false;
+ }
+
+ if (!GL_CheckExtension("wgl", wglfuncs, NULL, false))
+ {
+ VID_Shutdown();
+ Con_Printf("wgl functions not found\n");
+ return false;
+ }
+
+ baseRC = qwglCreateContext(hdc);
+ if (!baseRC)
+ {
+ VID_Shutdown();
+ Con_Printf("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.\n");
+ return false;
+ }
+ if (!qwglMakeCurrent(hdc, baseRC))
+ {
+ VID_Shutdown();
+ Con_Printf("wglMakeCurrent(%d, %d) failed\n", hdc, baseRC);
+ return false;
+ }
+
+ qglGetString = GL_GetProcAddress("glGetString");
+ qwglGetExtensionsStringARB = GL_GetProcAddress("wglGetExtensionsStringARB");
+ if (qglGetString == NULL)
+ {
+ VID_Shutdown();
+ Con_Printf("glGetString not found\n");
+ return false;
+ }
+ gl_renderer = qglGetString(GL_RENDERER);
+ gl_vendor = qglGetString(GL_VENDOR);
+ gl_version = qglGetString(GL_VERSION);
+ gl_extensions = qglGetString(GL_EXTENSIONS);
+ gl_platform = "WGL";
+ gl_platformextensions = "";
+
+ if (qwglGetExtensionsStringARB)
+ gl_platformextensions = qwglGetExtensionsStringARB(hdc);
+
+ gl_videosyncavailable = GL_CheckExtension("WGL_EXT_swap_control", wglswapintervalfuncs, NULL, false);
+ ReleaseDC(mainwindow, hdc);
+
+ GL_Init ();
+
+ // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
+ if (strncasecmp(gl_vendor,"ATI",3)==0)
+ {
+ if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
+ isRagePro = true;
+ }
+ if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
+ isG200 = true;
+
+ //vid_menudrawfn = VID_MenuDraw;
+ //vid_menukeyfn = VID_MenuKey;
+ vid_hidden = false;
+ vid_initialized = true;
+
+ IN_StartupMouse ();
+ IN_StartupJoystick ();
+
+ return true;
+}
+
+static void IN_Shutdown(void);
+void VID_Shutdown (void)
+{
+ HGLRC hRC = 0;
+ HDC hDC = 0;
+
+ vid_initialized = false;
+ IN_Shutdown();
+ if (qwglGetCurrentContext)
+ hRC = qwglGetCurrentContext();
+ if (qwglGetCurrentDC)
+ hDC = qwglGetCurrentDC();
+ if (qwglMakeCurrent)
+ qwglMakeCurrent(NULL, NULL);
+ if (hRC && qwglDeleteContext)
+ qwglDeleteContext(hRC);
+ // close the library before we get rid of the window
+ GL_CloseLibrary();
+ if (hDC && mainwindow)
+ ReleaseDC(mainwindow, hDC);
+ if (vid_isfullscreen)
+ ChangeDisplaySettings (NULL, 0);
+ vid_isfullscreen = false;
+ AppActivate(false, false);
+ VID_RestoreSystemGamma();
+ if (mainwindow)
+ DestroyWindow(mainwindow);
+ mainwindow = 0;