I started to learn SFML, I want to create sprite to load an image from a file, so I just followed the tutorial and made the obvious thing.
sf::Texture texture;
texture.loadFromFile("C:\image.png");
sf::Sprite sprite;
sprite.setTexture(texture);
window.draw(sprite);
And when I start the program I just get a white screen and "Unhandled exception at 0x50CEDEDA (msvcr110.dll) in itsprgps.exe: 0xC0000005: Access violation reading location 0x00524000.", also the console gets filled with random symbols. I tried to look for some info but I just found "If the texture is destroyed or moves elsewhere in memory, the sprite ends up with an invalid texture pointer", this might be obvious for some people but I'm new in this and they don't give any working example.
I'm usinf SFML 2.1 and Visual Studio 2013
EDIT:
This is a sample of my code without all the shapes I drew before trying to load the texture:
int main()
{
sf::RenderWindow window(sf::VideoMode(557, 500), "My window");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(255, 255, 255));
sf::Texture texture;
texture.loadFromFile("C:\roads.png");
sf::Sprite sprite;
sprite.setTexture(texture);
window.draw(sprite);
window.display();
}
return 0;
}
I also realized something else... I cannot load fonts either, it happens the exact same thing, and I think I know why. When I started the project I added the libraries for release instead of debug ("sfml-system.lib;sfml-main.lib;sfml-graphics.lib;sfml-window.lib;" instead of "sfml-system-d.lib;sfml-main-d.lib;sfml-graphics-d.lib;sfml-window-d.lib;") so I think that might actually be the problem, so I tried to solve it but I faced another kind of problems.
Long story short: I tried the proper configuration for debug and release and I got different errors, first, that I'm missing a MSVCR110D.dll so out of curiosity just downloaded it and put it in the debug folder, and now I get 0xc000007b. I tried different configurations and the only one that seems to work is debugging with the release libs (Except when trying to load textures or fonts so far).
Change the ("C:\image.png");
to ("C:\\image.png");
.
It's likely the single backslash is causing the issue as it's an escape character.
In addition you should check the return value from loadFromFile
to make sure it was successful.
I'd advice you to move code responsible for load texture out of the loop and check its return value. Event if this will not resolve issue, you could meet or exclude any problems related to multiple loading images.
Code:
int main()
{
sf::RenderWindow window(sf::VideoMode(557, 500), "My window");
sf::Texture texture;
if(!texture.loadFromFile("C:\roads.png"))
{
std::cerr << "failed to load image" << std::endl;
exit(1);
}
sf::Sprite sprite;
sprite.setTexture(texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(255, 255, 255));
window.draw(sprite);
window.display();
}
}
It seems like you need to install the Visual C++ Redistributable for your version of visual studio, which you can download at the website of microsoft.
When in the "linker input" configuration in debug mode, make sure you add "-d" to the additional files!
example: sfml-system-d.lib
User contributions licensed under CC BY-SA 3.0