C++ SFML 2.0 从文件加载图像

C++ SFML 2.0 Load Image From File

本文关键字:文件 加载 图像 SFML C++      更新时间:2023-10-16

我是SFML的新手,我正在使用SFML 2.0,我只是写了一些代码,它的工作正常,但问题是我加载图像以显示在屏幕上,它给了我错误

Failed to load image "Splashscreen.png". Reason : Unable to open file
Failed to load image "Mainmenu.png". Reason : Unable to open file

我从 URL 加载图像的代码是

对于菜单图像

sf::Texture menuTexture;

menuTexture.loadFromFile("Mainmenu.png");
    {
        sf::Sprite sprite(menuTexture);
        MenuItem playButton;
        playButton.rect.top = 145;
        playButton.rect.height = playButton.rect.top + 380;
        playButton.rect.left = 0;
        playButton.rect.width = playButton.rect.left + 1023; 
        playButton.menuAction = Play;
        MenuItem exitButton;
        exitButton.rect.top = 0;
        exitButton.rect.height = exitButton.rect.top + 1023;
        exitButton.rect.left = 383;
        exitButton.rect.width = exitButton.rect.left + 560; 
        exitButton.menuAction = Exit;
        menuItem.push_back(playButton);
        menuItem.push_back(exitButton);
        window.draw(sprite);
        window.display();
        return GetMenuResponse(window);
    }

对于初始屏幕图像

sf::Texture texture;
    if(texture.loadFromFile("Splashscreen.png") != true)
    {
        return;
    }
    sf::Sprite sprite(texture);
    renderWindow.clear();
    renderWindow.draw(sprite,sf::RenderStates::Default);
    renderWindow.display();
    sf::Event event;
        while(true)
        {
            while(renderWindow.pollEvent(event))
            {
                if(event.type == sf::Event::EventType::KeyPressed 
                    || event.type == sf::Event::EventType::MouseButtonPressed
                    || event.type == sf::Event::EventType::Closed)
                {
                    return;
                }
            }
        }

图像未加载,任何人都可以帮助解决此问题。

但是当我

构建我的项目并从调试文件夹运行相同的程序时,我的意思是当我运行exe文件图像时,图像显示出来,但是当我从Visual Studio 2010运行程序时,图像没有加载当我在发布模式下运行程序时,程序崩溃了。

您是否检查过该文件是否在您的工作目录中?它可以与输出目录不同!