Search code examples
c++visual-studiotexturessfml

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.


Solution

  • 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.