Skip to content

Commit

Permalink
Win32: Consolidate fullscreen toggling down to one function.
Browse files Browse the repository at this point in the history
Attempt to fix hrydgard#6295. It seems to be related to pixel formats, at least on this laptop with Arrandale Intel HD graphics...
  • Loading branch information
thedax committed Jun 25, 2014
1 parent 5215d12 commit 22698e9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 89 deletions.
62 changes: 38 additions & 24 deletions Windows/OpenGLBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,51 +87,65 @@ void DebugCallbackARB(GLenum source, GLenum type, GLuint id, GLenum severity,
FormatDebugOutputARB(finalMessage, 256, source, type, id, severity, message);
ERROR_LOG(G3D, "GL: %s", finalMessage);
}

bool GL_Init(HWND window, std::string *error_message) {
*error_message = "ok";
hWnd = window;
bool GL_SetPixelFormat(std::string *error_message)
{
wglMakeCurrent(hDC, hRC);
GLuint PixelFormat;

// TODO: Change to use WGL_ARB_pixel_format instead
static const PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
24, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
8, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // At least a 16Bit Z-Buffer (Depth Buffer)
8, // 8-bit Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
24, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
8, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // At least a 16Bit Z-Buffer (Depth Buffer)
8, // 8-bit Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};

hDC = GetDC(hWnd);

if (!hDC) {
*error_message = "Failed to get a device context.";
if (error_message != nullptr)
*error_message = "Failed to get a device context.";
ERROR_LOG(G3D, "Failed to get a device context.");
return false; // Return FALSE
}

if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) {
*error_message = "Can't find a suitable PixelFormat.";
if (error_message != nullptr)
*error_message = "Can't find a suitable PixelFormat.";
ERROR_LOG(G3D, "Can't find a suitable PixelFormat.");
return false;
}

if (!SetPixelFormat(hDC, PixelFormat, &pfd)) {
*error_message = "Can't set the PixelFormat.";
if (error_message != nullptr)
*error_message = "Can't set the PixelFormat.";
ERROR_LOG(G3D, "Can't set the PixelFormat.");
return false;
}

return true;
}
bool GL_Init(HWND window, std::string *error_message) {
*error_message = "ok";
hWnd = window;

if (!GL_SetPixelFormat(error_message))
return false;

if (!(hRC = wglCreateContext(hDC))) {
*error_message = "Can't create a GL rendering context.";
return false;
Expand Down
2 changes: 1 addition & 1 deletion Windows/OpenGLBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Common/CommonWindows.h"

bool GL_Init(HWND window, std::string *error_message);

bool GL_SetPixelFormat(std::string *error_message = nullptr);
void GL_Shutdown();
void GL_Resized();
void GL_SwapBuffers();
5 changes: 1 addition & 4 deletions Windows/WindowsHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,7 @@ bool WindowsHost::CreateDesktopShortcut(std::string argumentPath, std::string ga
}

void WindowsHost::GoFullscreen(bool viewFullscreen) {
if (viewFullscreen)
MainWindow::_ViewFullScreen(MainWindow::GetHWND());
else
MainWindow::_ViewNormal(MainWindow::GetHWND());
MainWindow::ToggleFullscreen(MainWindow::GetHWND());
}

void WindowsHost::ToggleDebugConsoleVisibility() {
Expand Down
99 changes: 41 additions & 58 deletions Windows/WndMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ struct VerySleepy_AddrInfo {
};

extern std::map<int, int> windowsTransTable;
static RECT g_normalRC = {0};
static RECT g_normalRC = { 0 };
static std::wstring windowTitle;
extern bool g_TakeScreenshot;
extern InputState input_state;
Expand Down Expand Up @@ -283,67 +283,56 @@ namespace MainWindow {
}
}

void _ViewNormal(HWND hWnd) {
// Put caption and border styles back.
DWORD dwOldStyle = ::GetWindowLong(hWnd, GWL_STYLE);
DWORD dwNewStyle = dwOldStyle | WS_CAPTION | WS_THICKFRAME | WS_SYSMENU;
::SetWindowLong(hWnd, GWL_STYLE, dwNewStyle);
void ToggleFullscreen(HWND hWnd) {
int x = 0, y = 0;
int cx = 0, cy = 0;

// Put back the menu bar.
::SetMenu(hWnd, menu);
DWORD dwStyle = GetWindowLong(hWnd, GWL_STYLE);
if ((dwStyle & WS_OVERLAPPEDWINDOW)) {
g_Config.bFullScreen = true;

// Resize to normal view.
// NOTE: Use SWP_FRAMECHANGED to force redraw non-client.
const int x = g_normalRC.left;
const int y = g_normalRC.top;
const int cx = g_normalRC.right - g_normalRC.left;
const int cy = g_normalRC.bottom - g_normalRC.top;
::SetWindowPos(hWnd, HWND_NOTOPMOST, x, y, cx, cy, SWP_FRAMECHANGED);

// Reset full screen indicator.
g_Config.bFullScreen = false;
CorrectCursor();
// Keep in mind normal window rectangle.
::GetWindowRect(hWnd, &g_normalRC);

bool showOSM = (g_Config.iInternalResolution == RESOLUTION_AUTO);
ResizeDisplay(true);
if (showOSM) {
ShowScreenResolution();
}
ShowOwnedPopups(hwndMain, TRUE);
W32Util::MakeTopMost(hwndMain, g_Config.bTopMost);
}
dwStyle &= ~WS_OVERLAPPEDWINDOW;

// Resize to fullscreen view. No need to adjust x & y.
cx = ::GetSystemMetrics(SM_CXSCREEN);
cy = ::GetSystemMetrics(SM_CYSCREEN);
} else {
g_Config.bFullScreen = false;

dwStyle |= WS_OVERLAPPEDWINDOW;

void _ViewFullScreen(HWND hWnd) {
// Keep in mind normal window rectangle.
::GetWindowRect(hWnd, &g_normalRC);
// Resize to normal view.
x = g_normalRC.left;
y = g_normalRC.top;
cx = g_normalRC.right - g_normalRC.left;
cy = g_normalRC.bottom - g_normalRC.top;
}

// Remove caption and border styles.
DWORD dwOldStyle = ::GetWindowLong(hWnd, GWL_STYLE);
DWORD dwNewStyle = dwOldStyle & ~(WS_CAPTION | WS_THICKFRAME | WS_SYSMENU);
::SetWindowLong(hWnd, GWL_STYLE, dwNewStyle);
::SetWindowLong(hWnd, GWL_STYLE, dwStyle);

// Remove the menu bar.
::SetMenu(hWnd, NULL);
// Remove or show the menu as appropriate.
::SetMenu(hWnd, g_Config.bFullScreen ? NULL : menu);

// Resize to full screen view.
// NOTE: Use SWP_FRAMECHANGED to force redraw non-client.
const int x = 0;
const int y = 0;
const int cx = ::GetSystemMetrics(SM_CXSCREEN);
const int cy = ::GetSystemMetrics(SM_CYSCREEN);
::SetWindowPos(hWnd, HWND_TOPMOST, x, y, cx, cy, SWP_FRAMECHANGED);

// Set full screen indicator.
g_Config.bFullScreen = true;
::SetWindowPos(hWnd, g_Config.bFullScreen ? HWND_TOP : HWND_NOTOPMOST, x, y, cx, cy, SWP_FRAMECHANGED);

CorrectCursor();

bool showOSM = (g_Config.iInternalResolution == RESOLUTION_AUTO);
ResizeDisplay(true);
if (showOSM) {
if (showOSM)
ShowScreenResolution();
}

ShowOwnedPopups(hwndMain, FALSE);
W32Util::MakeTopMost(hWnd, g_Config.bTopMost);

ShowOwnedPopups(hWnd, g_Config.bFullScreen ? FALSE : TRUE);

// Brittle code alert: please keep this as the LAST thing done in this function.
// Moving it elsewhere seems to break fullscreen exiting on Intel GPUs.
GL_SetPixelFormat();
}

RECT DetermineWindowRectangle() {
Expand Down Expand Up @@ -785,6 +774,9 @@ namespace MainWindow {

hideCursor = true;
SetTimer(hwndMain, TIMER_CURSORUPDATE, CURSORUPDATE_INTERVAL_MS, 0);

if (g_Config.bFullScreen)
ToggleFullscreen(hwndMain);

ShowWindow(hwndMain, nCmdShow);

Expand All @@ -796,9 +788,6 @@ namespace MainWindow {

SetFocus(hwndMain);

if (g_Config.bFullScreen)
_ViewFullScreen(hwndMain);

return TRUE;
}

Expand Down Expand Up @@ -1416,13 +1405,7 @@ namespace MainWindow {
break;

case ID_OPTIONS_FULLSCREEN:
g_Config.bFullScreen = !g_Config.bFullScreen;

if (g_Config.bFullScreen)
_ViewFullScreen(hWnd);
else
_ViewNormal(hWnd);

ToggleFullscreen(hWnd);
break;

case ID_OPTIONS_VERTEXCACHE:
Expand Down
3 changes: 1 addition & 2 deletions Windows/WndMainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ namespace MainWindow
HINSTANCE GetHInstance();
void BrowseAndBoot(std::string defaultPath, bool browseDirectory = false);
void SaveStateActionFinished(bool result, void *userdata);
void _ViewFullScreen(HWND hWnd);
void _ViewNormal(HWND hWnd);
void ToggleFullscreen(HWND hWnd);
void ToggleDebugConsoleVisibility();
void TranslateMenus();
void setTexScalingMultiplier(int level);
Expand Down

0 comments on commit 22698e9

Please sign in to comment.