在SFML中加载纹理 [英] Loading a texture in sfml

查看:83
本文介绍了在SFML中加载纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习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);

当我启动程序时,我只得到一个白色屏幕,并且在其prgps.exe中的0x50CEDEDA(msvcr110.dll)处未处理的异常:0xC0000005:访问冲突读取位置0x00524000.",控制台也随机填充符号.我试图寻找一些信息,但我只是发现如果纹理被破坏或移动到内存中的其他位置,子画面将以无效的纹理指针结尾",这对于某些人来说可能是显而易见的,但是我是新手.他们没有给出任何可行的例子.

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.

我使用的是SFML 2.1和Visual Studio 2013

I'm usinf SFML 2.1 and Visual Studio 2013

这是我的代码示例,其中没有尝试加载纹理之前绘制的所有形状:

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;
}

我也意识到了其他事情……我也无法加载字体,它发生的完全相同,我想我知道为什么.当我开始项目时,我添加的是发布库,而不是调试库("sfml-system.lib; sfml-main.lib; sfml-graphics.lib; sfml-window.lib;"而不是"sfml-system"-d.lib; sfml-main-d.lib; sfml-graphics-d.lib; sfml-window-d.lib;),所以我认为这可能实际上是问题所在,因此我尝试解决此问题,但是我面临另一种问题.

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.

长话短说:我尝试了正确的调试和发布配置,但遇到了其他错误,首先,由于缺少MSVCR110D.dll,因此出于好奇,我将其下载并放在了debug文件夹中,现在我得到0xc000007b.我尝试了不同的配置,似乎唯一可行的配置是使用发行版lib进行调试(到目前为止,除非尝试加载纹理或字体).

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

推荐答案

我建议您将负责加载纹理的代码移出循环,并检查其返回值.如果无法解决问题,您可以遇到或排除与多个加载图像有关的任何问题.

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.

代码:

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

这篇关于在SFML中加载纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆