Loading a texture in sfml

3

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:

include "stdafx.h"

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).

c++
sfml
asked on Stack Overflow Jun 23, 2014 by delca7 • edited Jun 20, 2020 by Community

4 Answers

2

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.

answered on Stack Overflow Jun 23, 2014 by PomfCaster
2

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();
    }
}
answered on Stack Overflow Jul 14, 2014 by twinra
0

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.

answered on Stack Overflow Jul 8, 2014 by JaytleBee
0

When in the "linker input" configuration in debug mode, make sure you add "-d" to the additional files!

example: sfml-system-d.lib

answered on Stack Overflow Apr 22, 2021 by Agus Brollo • edited Apr 22, 2021 by RockPaperLz- Mask it or Casket

User contributions licensed under CC BY-SA 3.0