Redson
Redson

Reputation: 2140

Cannot load an image file as texture using C++ and SFML 2.1

I am trying to output an image onto the screen using SFML 2.1, C++, and MS Visual Studio Professional 2013. I am getting an unexpected error when trying to load a file into a texture. It outputs a whole bunch of random characters. I'm sure if its how I configured the SFML library with Visual Studio or a problem with the code. Can anyone solve this problem? Thanks.

Here is a screenshot of what it looks like when I run the program (https://i.sstatic.net/uMdLT.png):

This is my code:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;

int main() {

    sf::RenderWindow window;
    window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing

    sf::Texture jetTexture;
    sf::Sprite jetImage;

    // Getting Error here!
    if (!jetTexture.loadFromFile("fighter jet.png"))
        throw std::runtime_error("Could not load fighter jet.png");

    jetImage.setTexture(jetTexture);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.draw(jetImage);
        window.display();
    }

    return 0;
}

For all configuration properties, they look like this:

Linker -> General (https://i.sstatic.net/NZg7P.png):

Linker -> Input (https://i.sstatic.net/1tPaB.png):

**Please note that if I did not configured the SFML library as I did, then I would be getting an error from my system saying msvcr110d.dll is missing.

Upvotes: 1

Views: 6183

Answers (2)

Ivan Neeson
Ivan Neeson

Reputation: 3301

Ok I managed to fix this, here is what you need to do:

  1. Set your SUBSYSTEM to WINDOWS:
  2. Add "sfml-main.lib" to your libraries:
  3. Change your Runtime library to /MD. This is because you're not using the debug versions of the SFML 2.1 libraries (and probably can't in VS2013).
  4. Make sure your "fighter jet.png" image is in the right place. By default Visual Studio uses the Project Directory as the working directory. So put the png in the same folder as your vcxproj file.

Upvotes: 6

Cirrus Minor
Cirrus Minor

Reputation: 394

Are you sure that the picture is in the execution directory of your program ? You should avoid spaces in file's name (fighter_jet.png).

If you're not sure about execution directory, try with absolute path (to be sure it's a path problem, and not a picture's problem).

I hope it helps.

I've tried this code on my system (Xcode - OSX), with one of my picture, and it works. Have you tried with another picture ?

#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;

int main() {

    sf::RenderWindow window;
    window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing

    sf::Texture jetTexture;
    sf::Sprite jetImage;

    // Getting Error here!
    if (!jetTexture.loadFromFile("fighter jet.png"))
        throw std::runtime_error("Could not load fighter jet.png");

    jetImage.setTexture(jetTexture);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.draw(jetImage);
        window.display();
    }

    return 0;
}

Upvotes: 1

Related Questions