You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 1, 2020. It is now read-only.
/** * Creates a new sf::Image and * loads it with image data from a resource (.rc) file*/
sf::Image LoadImageFromResource(const std::string& name)
{
HRSRC rsrcData = FindResource(NULL, name.c_str(), RT_RCDATA);
if (!rsrcData)
throwstd::runtime_error("Failed to find resource.");
DWORD rsrcDataSize = SizeofResource(NULL, rsrcData);
if (rsrcDataSize <= 0)
throwstd::runtime_error("Size of resource is 0.");
HGLOBAL grsrcData = LoadResource(NULL, rsrcData);
if (!grsrcData)
throwstd::runtime_error("Failed to load resource.");
LPVOID firstByte = LockResource(grsrcData);
if (!firstByte)
throwstd::runtime_error("Failed to lock resource.");
sf::Image image;
if (!image.loadFromMemory(firstByte, rsrcDataSize))
throwstd::runtime_error("Failed to load image from memory.");
return image;
}
Note: The shown code may lead to an unnecessary copy if the compiler doesn't support NRVO. In such a case, it would be possible to use the image as an output parameter, or return std::unique_ptr<sf::Image>.