Luna
Luna

Reputation: 109

SFML loadFromFile not showing image

I've been trying to output an image using SFML in C++ VS 2013, but when I use loadFromFile, it doesn't display anything, I've checked the working directories, it's the project directory, and the image is in a file in it. Here is the code:-

#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>
int main(){
sf::VideoMode videomode(800, 800);
sf::RenderWindow window(videomode, "HEllO");
sf::Sprite sprite;
sf::Texture texture;
texture.loadFromFile("Images\aline.png");
sprite.setTexture(texture);
sprite.setPosition(200, 200);
while (window.isOpen()){
    window.clear();
    window.draw(sprite);
    window.display();
    sf::Event event;
    while (window.pollEvent(event)){
        if ((event.type == sf::Event::Closed))
            window.close();
        }

    }
}

The place where my image is: Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Images

Working directory:- $(ProjectDir)

If you have any idea why this is happening, please help me. I'm sorry if the question is a stupid/obvious one.

Upvotes: 0

Views: 2016

Answers (3)

Andreas DM
Andreas DM

Reputation: 11018

Change

texture.loadFromFile("Images\aline.png");

to

texture.loadFromFile("Images/aline.png");
                    //      ^

Or if on Windows, \\ should work as well.

texture.loadFromFile("Images\\aline.png");

Upvotes: 0

MORTAL
MORTAL

Reputation: 383

your code is ok. but make sure you don't mix Debug/Release libs. if you mixed them your pic won't be shown on screen. from comments the weird characters in console output means you indeed linked release libs with debug mode or vise-versa.

Upvotes: 2

Jesper Juhl
Jesper Juhl

Reputation: 31468

Two obvious things to change would be:

  1. Check the return value from loadFromFile() - it actually tells you if it thinks it succeeded.
  2. Your code texture.loadFromFile("Images\aline.png"); should be texture.loadFromFile("Images/aline.png");.

Upvotes: 1

Related Questions