Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML: texture.loadFromFile("image.png"); Fails!  (Read 2803 times)

0 Members and 1 Guest are viewing this topic.

DecoratorFawn82

  • Newbie
  • *
  • Posts: 10
    • View Profile
SFML: texture.loadFromFile("image.png"); Fails!
« 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;
}

DecoratorFawn82

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: SFML: texture.loadFromFile("image.png"); Fails!
« Reply #1 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);

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML: texture.loadFromFile("image.png"); Fails!
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/