Consolidate getErrorString functions

This commit is contained in:
Chris Thrasher 2024-07-02 11:31:25 -06:00
parent 1bf092fe6d
commit 88646246fc
No known key found for this signature in database
GPG Key ID: 56FB686C9DFC8E2C
5 changed files with 87 additions and 99 deletions

View File

@ -65,6 +65,7 @@ if(SFML_OS_WINDOWS)
${SRCROOT}/Win32/JoystickImpl.hpp
${SRCROOT}/Win32/SensorImpl.hpp
${SRCROOT}/Win32/SensorImpl.cpp
${SRCROOT}/Win32/Utils.hpp
${SRCROOT}/Win32/VideoModeImpl.cpp
${SRCROOT}/Win32/VulkanImplWin32.cpp
${SRCROOT}/Win32/WindowImplWin32.cpp

View File

@ -26,6 +26,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Win32/ClipboardImpl.hpp>
#include <SFML/Window/Win32/Utils.hpp>
#include <SFML/System/Err.hpp>
#include <SFML/System/String.hpp>
@ -35,26 +36,6 @@
#include <cstring>
namespace
{
std::string getErrorString(DWORD error)
{
PTCHAR buffer = nullptr;
if (FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
error,
0,
reinterpret_cast<PTCHAR>(&buffer),
0,
nullptr) == 0)
return "Unknown error.";
sf::String message = buffer;
LocalFree(buffer);
return message.toAnsiString();
}
}
namespace sf::priv
{

View File

@ -26,6 +26,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/JoystickImpl.hpp>
#include <SFML/Window/Win32/Utils.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/System/Err.hpp>
@ -116,25 +117,6 @@ ConnectionCache connectionCache[sf::Joystick::Count];
// If true, will only update when WM_DEVICECHANGE message is received
bool lazyUpdates = false;
// Get a system error string from an error code
std::string getErrorString(DWORD error)
{
PTCHAR buffer = nullptr;
if (FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
error,
0,
reinterpret_cast<PTCHAR>(&buffer),
0,
nullptr) == 0)
return "Unknown error.";
const sf::String message = buffer;
LocalFree(buffer);
return message.toAnsiString();
}
// Get the joystick's name
sf::String getDeviceName(unsigned int index, JOYCAPS caps)
{
@ -163,7 +145,7 @@ sf::String getDeviceName(unsigned int index, JOYCAPS caps)
if (result != ERROR_SUCCESS)
{
sf::err() << "Unable to open registry for joystick at index " << index << ": "
<< getErrorString(static_cast<DWORD>(result)) << std::endl;
<< sf::priv::getErrorString(static_cast<DWORD>(result)) << std::endl;
return joystickDescription;
}
}
@ -184,7 +166,7 @@ sf::String getDeviceName(unsigned int index, JOYCAPS caps)
if (result != ERROR_SUCCESS)
{
sf::err() << "Unable to query registry key for joystick at index " << index << ": "
<< getErrorString(static_cast<DWORD>(result)) << std::endl;
<< sf::priv::getErrorString(static_cast<DWORD>(result)) << std::endl;
return joystickDescription;
}
@ -197,7 +179,7 @@ sf::String getDeviceName(unsigned int index, JOYCAPS caps)
if (result != ERROR_SUCCESS)
{
sf::err() << "Unable to open registry key for joystick at index " << index << ": "
<< getErrorString(static_cast<DWORD>(result)) << std::endl;
<< sf::priv::getErrorString(static_cast<DWORD>(result)) << std::endl;
return joystickDescription;
}
@ -209,7 +191,7 @@ sf::String getDeviceName(unsigned int index, JOYCAPS caps)
if (result != ERROR_SUCCESS)
{
sf::err() << "Unable to query name for joystick at index " << index << ": "
<< getErrorString(static_cast<DWORD>(result)) << std::endl;
<< sf::priv::getErrorString(static_cast<DWORD>(result)) << std::endl;
return joystickDescription;
}

View File

@ -0,0 +1,53 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/String.hpp>
#include <SFML/System/Win32/WindowsHeader.hpp>
namespace sf::priv
{
inline std::string getErrorString(DWORD error)
{
PTCHAR buffer = nullptr;
if (FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
error,
0,
reinterpret_cast<PTCHAR>(&buffer),
0,
nullptr) == 0)
{
return "Unknown error.";
}
const sf::String message = buffer;
LocalFree(buffer);
return message.toAnsiString();
}
} // namespace sf::priv

View File

@ -27,6 +27,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Window/ContextSettings.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <SFML/Window/Win32/Utils.hpp>
#include <SFML/Window/Win32/WglContext.hpp>
#include <SFML/Window/WindowImpl.hpp>
@ -100,30 +101,6 @@ void ensureExtensionsInit(HDC deviceContext)
namespace sf::priv
{
////////////////////////////////////////////////////////////
String getErrorString(DWORD errorCode)
{
PTCHAR buffer = nullptr;
if (FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
errorCode,
0,
reinterpret_cast<LPTSTR>(&buffer),
256,
nullptr) != 0)
{
String errMsg(buffer);
LocalFree(buffer);
return errMsg;
}
std::ostringstream ss;
ss << "Error " << errorCode;
return {ss.str()};
}
////////////////////////////////////////////////////////////
WglContext::WglContext(WglContext* shared) : WglContext(shared, ContextSettings{}, {1u, 1u})
{
@ -234,7 +211,7 @@ bool WglContext::makeCurrent(bool current)
if (wglMakeCurrent(m_deviceContext, current ? m_context : nullptr) == FALSE)
{
err() << "Failed to " << (current ? "activate" : "deactivate")
<< " OpenGL context: " << getErrorString(GetLastError()).toAnsiString() << std::endl;
<< " OpenGL context: " << getErrorString(GetLastError()) << std::endl;
return false;
}
@ -261,7 +238,7 @@ void WglContext::setVerticalSyncEnabled(bool enabled)
if (SF_GLAD_WGL_EXT_swap_control)
{
if (wglSwapIntervalEXT(enabled ? 1 : 0) == FALSE)
err() << "Setting vertical sync failed: " << getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Setting vertical sync failed: " << getErrorString(GetLastError()) << std::endl;
}
else
{
@ -335,7 +312,7 @@ int WglContext::selectBestPixelFormat(HDC deviceContext, unsigned int bitsPerPix
const bool isValid = wglChoosePixelFormatARB(deviceContext, intAttributes, nullptr, 512, formats, &nbFormats) != FALSE;
if (!isValid)
err() << "Failed to enumerate pixel formats: " << getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to enumerate pixel formats: " << getErrorString(GetLastError()) << std::endl;
// Get the best format among the returned ones
if (isValid && (nbFormats > 0))
@ -355,8 +332,7 @@ int WglContext::selectBestPixelFormat(HDC deviceContext, unsigned int bitsPerPix
if (wglGetPixelFormatAttribivARB(deviceContext, formats[i], PFD_MAIN_PLANE, 7, attributes, values) == FALSE)
{
err() << "Failed to retrieve pixel format information: "
<< getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to retrieve pixel format information: " << getErrorString(GetLastError()) << std::endl;
break;
}
@ -369,7 +345,7 @@ int WglContext::selectBestPixelFormat(HDC deviceContext, unsigned int bitsPerPix
FALSE)
{
err() << "Failed to retrieve pixel format multisampling information: "
<< getErrorString(GetLastError()).toAnsiString() << std::endl;
<< getErrorString(GetLastError()) << std::endl;
break;
}
}
@ -383,7 +359,7 @@ int WglContext::selectBestPixelFormat(HDC deviceContext, unsigned int bitsPerPix
FALSE)
{
err() << "Failed to retrieve pixel format sRGB capability information: "
<< getErrorString(GetLastError()).toAnsiString() << std::endl;
<< getErrorString(GetLastError()) << std::endl;
break;
}
}
@ -397,8 +373,8 @@ int WglContext::selectBestPixelFormat(HDC deviceContext, unsigned int bitsPerPix
if (wglGetPixelFormatAttribivARB(deviceContext, formats[i], PFD_MAIN_PLANE, 1, pbufferAttributes, &pbufferValue) ==
FALSE)
{
err() << "Failed to retrieve pixel format pbuffer information: "
<< getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to retrieve pixel format pbuffer information: " << getErrorString(GetLastError())
<< std::endl;
break;
}
@ -467,8 +443,7 @@ void WglContext::setDevicePixelFormat(unsigned int bitsPerPixel)
if (bestFormat == 0)
{
err() << "Failed to find a suitable pixel format for device context: "
<< getErrorString(GetLastError()).toAnsiString() << '\n'
err() << "Failed to find a suitable pixel format for device context: " << getErrorString(GetLastError()) << '\n'
<< "Cannot create OpenGL context" << std::endl;
return;
}
@ -482,7 +457,7 @@ void WglContext::setDevicePixelFormat(unsigned int bitsPerPixel)
// Set the chosen pixel format
if (SetPixelFormat(m_deviceContext, bestFormat, &actualFormat) == FALSE)
{
err() << "Failed to set pixel format for device context: " << getErrorString(GetLastError()).toAnsiString() << '\n'
err() << "Failed to set pixel format for device context: " << getErrorString(GetLastError()) << '\n'
<< "Cannot create OpenGL context" << std::endl;
return;
}
@ -496,7 +471,7 @@ void WglContext::updateSettingsFromPixelFormat()
if (format == 0)
{
err() << "Failed to get selected pixel format: " << getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to get selected pixel format: " << getErrorString(GetLastError()) << std::endl;
return;
}
@ -506,8 +481,7 @@ void WglContext::updateSettingsFromPixelFormat()
if (DescribePixelFormat(m_deviceContext, format, sizeof(actualFormat), &actualFormat) == 0)
{
err() << "Failed to retrieve pixel format information: " << getErrorString(GetLastError()).toAnsiString()
<< std::endl;
err() << "Failed to retrieve pixel format information: " << getErrorString(GetLastError()) << std::endl;
return;
}
@ -537,8 +511,7 @@ void WglContext::updateSettingsFromPixelFormat()
}
else
{
err() << "Failed to retrieve pixel format information: " << getErrorString(GetLastError()).toAnsiString()
<< std::endl;
err() << "Failed to retrieve pixel format information: " << getErrorString(GetLastError()) << std::endl;
m_settings.depthBits = actualFormat.cDepthBits;
m_settings.stencilBits = actualFormat.cStencilBits;
}
@ -555,8 +528,8 @@ void WglContext::updateSettingsFromPixelFormat()
}
else
{
err() << "Failed to retrieve pixel format multisampling information: "
<< getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to retrieve pixel format multisampling information: " << getErrorString(GetLastError())
<< std::endl;
m_settings.antialiasingLevel = 0;
}
}
@ -577,8 +550,8 @@ void WglContext::updateSettingsFromPixelFormat()
}
else
{
err() << "Failed to retrieve pixel format sRGB capability information: "
<< getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to retrieve pixel format sRGB capability information: " << getErrorString(GetLastError())
<< std::endl;
m_settings.sRgbCapable = false;
}
}
@ -621,8 +594,8 @@ void WglContext::createSurface(WglContext* shared, const Vector2u& size, unsigne
if (!m_deviceContext)
{
err() << "Failed to retrieve pixel buffer device context: "
<< getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to retrieve pixel buffer device context: " << getErrorString(GetLastError())
<< std::endl;
wglDestroyPbufferARB(m_pbuffer);
m_pbuffer = nullptr;
@ -630,7 +603,7 @@ void WglContext::createSurface(WglContext* shared, const Vector2u& size, unsigne
}
else
{
err() << "Failed to create pixel buffer: " << getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to create pixel buffer: " << getErrorString(GetLastError()) << std::endl;
}
}
}
@ -746,8 +719,8 @@ void WglContext::createContext(WglContext* shared)
{
if (wglMakeCurrent(shared->m_deviceContext, nullptr) == FALSE)
{
err() << "Failed to deactivate shared context before sharing: "
<< getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to deactivate shared context before sharing: " << getErrorString(GetLastError())
<< std::endl;
return;
}
@ -802,8 +775,7 @@ void WglContext::createContext(WglContext* shared)
m_context = wglCreateContext(m_deviceContext);
if (!m_context)
{
err() << "Failed to create an OpenGL context for this window: "
<< getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to create an OpenGL context for this window: " << getErrorString(GetLastError()) << std::endl;
return;
}
@ -818,8 +790,8 @@ void WglContext::createContext(WglContext* shared)
{
if (wglMakeCurrent(shared->m_deviceContext, nullptr) == FALSE)
{
err() << "Failed to deactivate shared context before sharing: "
<< getErrorString(GetLastError()).toAnsiString() << std::endl;
err() << "Failed to deactivate shared context before sharing: " << getErrorString(GetLastError())
<< std::endl;
return;
}
@ -827,8 +799,7 @@ void WglContext::createContext(WglContext* shared)
}
if (wglShareLists(sharedContext, m_context) == FALSE)
err() << "Failed to share the OpenGL context: " << getErrorString(GetLastError()).toAnsiString()
<< std::endl;
err() << "Failed to share the OpenGL context: " << getErrorString(GetLastError()) << std::endl;
}
}