SFML无法从文件加载图像

时间:2014-02-25 18:38:19

标签: c++ sfml

所以我正在通过游戏开发学习C ++,最近我在尝试加载名为image1.png的图像时遇到了错误。

我尝试使用目录并且不起作用然后我将图像移动到与.cpp文件相同的目录,但它仍然没有加载纹理。

// defining libraries
#include <SFML/Graphics.hpp>
#include<iostream>

//global variables, functions, and classes 

int main () {

    //SETUPS 
    sf::RenderWindow window(sf::VideoMode(800, 600),"SFML Game");
    window.setFramerateLimit(60);
    window.setKeyRepeatEnabled(false);

    bool play = true;

    sf::Event event;

    bool aPressed = false;
    bool aReleased = false;
    bool leftClick = false;
    bool space = false;
    bool returnReleased = false;
    bool rightClick = false;

    int numberOfClicks = 0;
    int mouseX, mouseY;
    int rectX = 0;
    int rectY = 0;

    sf::Texture image1;
    if (image1.loadFromFile("image1.png") == -1) {
        return 1;
    }

    sf::RectangleShape rect;
    rect.setSize(sf::Vector2f(100, 50));
    rect.setPosition(0, 0);
    rect.setTexture(&image1);

    sf::CircleShape circle;
    circle.setRadius(50);
    circle.setPosition(50,50);
    circle.setFillColor(sf::Color::Magenta);



    //Game Loop 

    while (play == true) {

        //EVENTS 
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::A) {
                aPressed = true;
            }

            if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::A) {
                aReleased = true;
            }

            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) {
                space = true;
            }

            if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space) {
                space = false;
            }


            if (event.type == sf::Event::Closed) {
                play = false;
            }
            if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Escape) {
                play = false;
            }
            if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
                leftClick = true;
            }

            if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Return) {
                returnReleased = true;
            }

            if (event.type == sf::Event::MouseButtonPressed && event.key.code == sf::Mouse::Right){
                rightClick = true;
            }
        }

        //LOGIC 
        if (aPressed == true) {
            std::cout << "Key has been pressed\n";
            aPressed = false;
        }

        if (aReleased == true) {
            std::cout << "Key has been released\n";
            aReleased = false;
        }

        if (leftClick == true) {
            numberOfClicks++;
            std::cout << "Number of clicks: " << numberOfClicks << "\n";
            leftClick = false;
        }

        if (event.type == sf::Event::MouseMoved) {
            mouseX = event.mouseMove.x;
            mouseY = event.mouseMove.y;
            //std::cout << "X:" << mouseX << "\n";
            //std::cout << "Y:" << mouseY << "\n";
        }

        if (returnReleased == true) {
            std::cout << "Return Released \n";
            returnReleased = false;
        }

        if (rightClick == true) {
            numberOfClicks--;
            std::cout << "Number of clicks: " << numberOfClicks << "\n";
            rightClick = false;
        }

        rectX += 2;
        rectY += 1;
        rect.setPosition(rectX, rectY);
        rect.setTexture(&image1);


        //RENDERING
        window.clear();

        window.draw(rect);
        window.draw(circle);

        window.display();
    }


    //Clean Up 
    window.close();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

从SFML关于图片加载的页面:http://www.sfml-dev.org/tutorials/2.0/graphics-sprite.php

  

loadFromFile函数有时会失败,没有明显的原因。首先,检查标准输出中SFML打印的错误消息(检查控制台)。如果消息是“无法打开文件”,请确保工作目录(任何文件路径的目录将被相对解释)是您认为的:当您从资源管理器运行应用程序时,工作目录是可执行文件夹,但是当您从IDE(Visual Studio,Code :: Blocks,...)启动程序时,工作目录有时会设置为项目目录。这通常可以在项目设置中轻松更改。

请检查错误消息并发布错误消息。