我正在使用SFML 2.4和Visual Studio Community 2015。 我无法从资源加载图像,我找不到任何东西。 这是我的代码:
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <Windows.h>
sf::Sprite m_Test;
sf::Image LoadImageFromResource(LPCWSTR name)
{
HRSRC rsrcData = FindResource(NULL, name, RT_RCDATA);
if (!rsrcData)
throw std::runtime_error("Failed to find resource.");
DWORD rsrcDataSize = SizeofResource(NULL, rsrcData);
if (rsrcDataSize <= 0) {
MessageBox(NULL, L"Size of resource is 0.", L"ERROR", MB_OK);
throw std::runtime_error("Size of resource is 0.");
}
HGLOBAL grsrcData = LoadResource(NULL, rsrcData);
if (!grsrcData)
throw std::runtime_error("Failed to load resource.");
LPVOID firstByte = LockResource(grsrcData);
if (!firstByte)
throw std::runtime_error("Failed to lock resource.");
sf::Image image;
if (!image.loadFromMemory(firstByte, rsrcDataSize))
throw std::runtime_error("Failed to load image from memory.");
return image;
}
void init() {
sf::Image testImage = LoadImageFromResource(L"test");
sf::Texture testTexture;
if (!testTexture.loadFromImage(testImage)) {
throw std::runtime_error("Failed to load image.");
}
sf::Sprite test;
test.setTexture(testTexture);
m_Test = test;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
sf::RenderWindow window(sf::VideoMode(1280, 720), "The Principle of Battle");
init();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
window.draw(m_Test);
window.display();
}
}
当我运行此代码时,输出显示:
Battle.exe原理中的0x00007FFF47BE7788抛出异常:Microsoft C ++异常:内存位置为0x0000009C0C96EE18的std :: runtime_error。
我试图调试它,但我不能。我无法找到这个问题的答案。
答案 0 :(得分:0)
如果要使用异常,则应查看异常处理。该错误表示抛出了异常,因此您需要找出异常是什么,以便您知道要修复的内容。
http://en.cppreference.com/w/cpp/language/try_catch http://www.cplusplus.com/doc/tutorial/exceptions/
答案 1 :(得分:0)
我不得不改变这一行:
HRSRC rsrcData = FindResource(NULL, name, RT_RCDATA);
为:
HRSRC rsrcData = FindResource(NULL, MAKEINTRESOURCE(name), RT_RCDATA);