Fixed wglChoosePixelFormatARB crashing when not supported

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1304 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2009-12-04 06:35:03 +00:00
parent f3f630142a
commit 053042bf5a

View File

@ -514,67 +514,75 @@ void WindowImplWin32::CreateContext(const VideoMode& Mode, WindowSettings& Param
{ {
// Get the wglChoosePixelFormatARB function (it is an extension) // Get the wglChoosePixelFormatARB function (it is an extension)
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = reinterpret_cast<PFNWGLCHOOSEPIXELFORMATARBPROC>(wglGetProcAddress("wglChoosePixelFormatARB")); PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = reinterpret_cast<PFNWGLCHOOSEPIXELFORMATARBPROC>(wglGetProcAddress("wglChoosePixelFormatARB"));
if (wglChoosePixelFormatARB)
// Define the basic attributes we want for our window
int IntAttributes[] =
{ {
WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, // Define the basic attributes we want for our window
WGL_SUPPORT_OPENGL_ARB, GL_TRUE, int IntAttributes[] =
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
WGL_SAMPLE_BUFFERS_ARB, (Params.AntialiasingLevel ? GL_TRUE : GL_FALSE),
WGL_SAMPLES_ARB, Params.AntialiasingLevel,
0, 0
};
// Let's check how many formats are supporting our requirements
int Formats[128];
UINT NbFormats;
float FloatAttributes[] = {0, 0};
bool IsValid = wglChoosePixelFormatARB(myDeviceContext, IntAttributes, FloatAttributes, sizeof(Formats) / sizeof(*Formats), Formats, &NbFormats) != 0;
if (!IsValid || (NbFormats == 0))
{
if (Params.AntialiasingLevel > 2)
{ {
// No format matching our needs : reduce the multisampling level WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
std::cerr << "Failed to find a pixel format supporting " WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
<< Params.AntialiasingLevel << " antialiasing levels ; trying with 2 levels" << std::endl; WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
Params.AntialiasingLevel = IntAttributes[11] = 2; WGL_SAMPLE_BUFFERS_ARB, (Params.AntialiasingLevel ? GL_TRUE : GL_FALSE),
IsValid = wglChoosePixelFormatARB(myDeviceContext, IntAttributes, FloatAttributes, sizeof(Formats) / sizeof(*Formats), Formats, &NbFormats) != 0; WGL_SAMPLES_ARB, Params.AntialiasingLevel,
} 0, 0
};
// Let's check how many formats are supporting our requirements
int Formats[128];
UINT NbFormats;
float FloatAttributes[] = {0, 0};
bool IsValid = wglChoosePixelFormatARB(myDeviceContext, IntAttributes, FloatAttributes, sizeof(Formats) / sizeof(*Formats), Formats, &NbFormats) != 0;
if (!IsValid || (NbFormats == 0)) if (!IsValid || (NbFormats == 0))
{ {
// Cannot find any pixel format supporting multisampling ; disabling antialiasing if (Params.AntialiasingLevel > 2)
std::cerr << "Failed to find a pixel format supporting antialiasing ; antialiasing will be disabled" << std::endl;
Params.AntialiasingLevel = 0;
}
}
// Get the best format among the returned ones
if (IsValid && (NbFormats > 0))
{
int BestScore = 0xFFFF;
for (UINT i = 0; i < NbFormats; ++i)
{
// Get the current format's attributes
PIXELFORMATDESCRIPTOR Attribs;
Attribs.nSize = sizeof(PIXELFORMATDESCRIPTOR);
Attribs.nVersion = 1;
DescribePixelFormat(myDeviceContext, Formats[i], sizeof(PIXELFORMATDESCRIPTOR), &Attribs);
// Evaluate the current configuration
int Color = Attribs.cRedBits + Attribs.cGreenBits + Attribs.cBlueBits + Attribs.cAlphaBits;
int Score = EvaluateConfig(Mode, Params, Color, Attribs.cDepthBits, Attribs.cStencilBits, Params.AntialiasingLevel);
// Keep it if it's better than the current best
if (Score < BestScore)
{ {
BestScore = Score; // No format matching our needs : reduce the multisampling level
BestFormat = Formats[i]; std::cerr << "Failed to find a pixel format supporting "
<< Params.AntialiasingLevel << " antialiasing levels ; trying with 2 levels" << std::endl;
Params.AntialiasingLevel = IntAttributes[11] = 2;
IsValid = wglChoosePixelFormatARB(myDeviceContext, IntAttributes, FloatAttributes, sizeof(Formats) / sizeof(*Formats), Formats, &NbFormats) != 0;
}
if (!IsValid || (NbFormats == 0))
{
// Cannot find any pixel format supporting multisampling ; disabling antialiasing
std::cerr << "Failed to find a pixel format supporting antialiasing ; antialiasing will be disabled" << std::endl;
Params.AntialiasingLevel = 0;
} }
} }
// Get the best format among the returned ones
if (IsValid && (NbFormats > 0))
{
int BestScore = 0xFFFF;
for (UINT i = 0; i < NbFormats; ++i)
{
// Get the current format's attributes
PIXELFORMATDESCRIPTOR Attribs;
Attribs.nSize = sizeof(PIXELFORMATDESCRIPTOR);
Attribs.nVersion = 1;
DescribePixelFormat(myDeviceContext, Formats[i], sizeof(PIXELFORMATDESCRIPTOR), &Attribs);
// Evaluate the current configuration
int Color = Attribs.cRedBits + Attribs.cGreenBits + Attribs.cBlueBits + Attribs.cAlphaBits;
int Score = EvaluateConfig(Mode, Params, Color, Attribs.cDepthBits, Attribs.cStencilBits, Params.AntialiasingLevel);
// Keep it if it's better than the current best
if (Score < BestScore)
{
BestScore = Score;
BestFormat = Formats[i];
}
}
}
}
else
{
// wglChoosePixelFormatARB not supported ; disabling antialiasing
std::cerr << "Antialiasing is not supported ; it will be disabled" << std::endl;
Params.AntialiasingLevel = 0;
} }
} }