Loading a texture in sfml

社会主义新天地 提交于 2021-02-05 06:26:07

问题


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


回答1:


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.




回答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();
    }
}



回答3:


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.



来源:https://stackoverflow.com/questions/24358968/loading-a-texture-in-sfml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!