SFML community forums

Help => General => Topic started by: DecoratorFawn82 on January 06, 2014, 09:35:26 pm

Title: SFML: texture.loadFromFile("image.png"); Fails!
Post by: DecoratorFawn82 on January 06, 2014, 09:35:26 pm
Hi all! I have problem with loading this image onto the screen so I can see it. Here is my code if it is anything wrong with it:


Yes, I have the image in the directory and I know it is in a .png format. The CMD Window doesn't give me any error. But the window won't show the image. I'm using CodeBlocks and when I read the tutorial it said:

but when you launch your program from your IDE (Visual Studio, Code::Blocks, ...) the working directory is sometimes set to the project directory instead. This can generally be changed easily in the project settings.

And if that is the problem where do I find "project settings" in CodeBlocks? I didn't found it when I clicked on the project tab

Code: [Select]
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "Window");
    sf::Event event;
    while (window.isOpen())
    {
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }
        window.clear(sf::Color::Black);

        sf::Texture texture;
        if (!texture.loadFromFile("image.png"))
        {
            cout << "Could not load image.png" << endl;
        }

        window.display();
    }

    return 0;
}
Title: Re: SFML: texture.loadFromFile("image.png"); Fails!
Post by: DecoratorFawn82 on January 06, 2014, 10:16:19 pm
[SOLVED] I were missing that I'm only loaded the image. But I didn't DRAW it to the screen using a sprite.

Code: [Select]
        sf::Sprite sprite;
        sprite.setTexture(texture);
        window.draw(sprite);
Title: Re: SFML: texture.loadFromFile("image.png"); Fails!
Post by: eXpl0it3r on January 06, 2014, 10:28:51 pm
PM people after 40min without reply is not good... And then you miss the most obvious thing. ???

Learn to debug your code and learn to have patience!

Also don't load a texture ever frame. Load it once before the game loop.