Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
857 views
in Technique[技术] by (71.8m points)

c++ - Loading a texture in sfml

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...