+ if (!RegisterClass (&wc))
+ Sys_Error("Couldn't register window class\n");
+
+ IN_Init();
+}
+
+int VID_InitMode (int fullscreen, int width, int height, int bpp)
+{
+ int i;
+ HDC hdc;
+ RECT rect;
+ MSG msg;
+ PIXELFORMATDESCRIPTOR pfd =
+ {
+ sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
+ 1, // version number
+ PFD_DRAW_TO_WINDOW // support window
+ | PFD_SUPPORT_OPENGL // support OpenGL
+ | PFD_DOUBLEBUFFER , // double buffered
+ PFD_TYPE_RGBA, // RGBA type
+ 24, // 24-bit color depth
+ 0, 0, 0, 0, 0, 0, // color bits ignored
+ 0, // no alpha buffer
+ 0, // shift bit ignored
+ 0, // no accumulation buffer
+ 0, 0, 0, 0, // accum bits ignored
+ 32, // 32-bit z-buffer
+ 0, // no stencil buffer
+ 0, // no auxiliary buffer
+ PFD_MAIN_PLANE, // main layer
+ 0, // reserved
+ 0, 0, 0 // layer masks ignored
+ };
+ int pixelformat;
+ DWORD WindowStyle, ExWindowStyle;
+ HGLRC baseRC;
+ int CenterX, CenterY;
+ const char *gldrivername;
+ int depth;
+
+ if (vid_initialized)
+ Sys_Error("VID_InitMode called when video is already initialised\n");
+
+ // if stencil is enabled, ask for alpha too
+ if (bpp >= 32)
+ {
+ pfd.cStencilBits = 8;
+ pfd.cAlphaBits = 8;
+ }
+ else
+ {
+ pfd.cStencilBits = 0;
+ pfd.cAlphaBits = 0;
+ }
+
+ gldrivername = "opengl32.dll";
+ i = COM_CheckParm("-gl_driver");
+ if (i && i < com_argc - 1)
+ gldrivername = com_argv[i + 1];
+ if (!GL_OpenLibrary(gldrivername))
+ {
+ Con_Printf("Unable to load GL driver %s\n", gldrivername);
+ return false;
+ }
+
+ memset(&gdevmode, 0, sizeof(gdevmode));
+
+ vid_isfullscreen = false;
+ if (fullscreen)
+ {
+ gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
+ gdevmode.dmBitsPerPel = bpp;
+ gdevmode.dmPelsWidth = width;
+ gdevmode.dmPelsHeight = height;
+ gdevmode.dmSize = sizeof (gdevmode);
+ if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
+ {
+ VID_Shutdown();
+ Con_Printf("Unable to change to requested mode %dx%dx%dbpp\n", width, height, bpp);
+ return false;
+ }
+
+ vid_isfullscreen = true;
+ WindowStyle = WS_POPUP;
+ ExWindowStyle = WS_EX_TOPMOST;
+ }
+ else
+ {
+ hdc = GetDC (NULL);
+ i = GetDeviceCaps(hdc, RASTERCAPS);
+ depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
+ ReleaseDC (NULL, hdc);
+ if (i & RC_PALETTE)
+ {
+ VID_Shutdown();
+ Con_Print("Can't run in non-RGB mode\n");
+ return false;
+ }
+ if (bpp > depth)
+ {
+ VID_Shutdown();
+ Con_Print("A higher desktop depth is required to run this video mode\n");
+ return false;
+ }
+
+ WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
+ ExWindowStyle = 0;
+ }
+
+ rect.top = 0;
+ rect.left = 0;
+ rect.right = width;
+ rect.bottom = height;
+ AdjustWindowRectEx(&rect, WindowStyle, false, 0);
+
+ if (fullscreen)
+ {
+ CenterX = 0;
+ CenterY = 0;
+ }
+ else
+ {
+ 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);
+
+ 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_Print("wgl functions not found\n");
+ return false;
+ }
+
+ baseRC = qwglCreateContext(hdc);
+ if (!baseRC)
+ {
+ VID_Shutdown();
+ Con_Print("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_Print("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;
+
+ if(vid_initialized == false)
+ return;
+
+ VID_RestoreSystemGamma();
+
+ 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);
+ AppActivate(false, false);
+ if (mainwindow)
+ DestroyWindow(mainwindow);
+ mainwindow = 0;
+ if (vid_isfullscreen)
+ ChangeDisplaySettings (NULL, 0);
+ vid_isfullscreen = false;