• Advertisement
Sign in to follow this  

SFML, Unable to open file

This topic is 2900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im just running through some of the SFML tutorials on their website, and ran into an issue when running the sprite tutorial. It seems to be having issues loading the image. I know I have it in the right place, because if I move the image, the error says it cannot find the file. I am using visual studio 2008, on windows 7 x64. I have this example as a release build and I am using the release version dll files (sfml-graphics.dll, sfml-system.dll and sfml-window.dll). Here is my code:
// SFMLGraphics.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
	sf::RenderWindow app(sf::VideoMode(800, 600, 32), "SFML Graphics");
	sf::Image image;
	if(!image.LoadFromFile("sprite.jpg"))
	{
		std::cout << "Could not load sprite image\n";
		system("PAUSE");
		return EXIT_FAILURE;
	}

	sf::Sprite sprite(image);
	sprite.SetColor(sf::Color(0, 255, 255, 128));
	sprite.SetPosition(200.f, 100.f);
	sprite.SetScale(2.0f, 2.0f);

	while(app.IsOpened())
	{
		sf::Event e;
		while(app.GetEvent(e))
		{
			if(e.Type == sf::Event::Closed)
			{
				app.Close();
			}
		}

		app.Clear(sf::Color(0, 0, 0));
		app.Draw(sprite);
		app.Display();
	}
	return EXIT_SUCCESS;
}

I'm calling the system pause so I could see the error messages SFML outputs. Any ideas on what I might be doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
I don't know if it's windows7 related. However, it's probably a matter of the relative path to the texture. Because you don't give a path to the texture, it's looking in several default places and, apparently, not finding it.

To see if that's the problem, use the full path to the texture:
"c:\\projects\\sfmlgraphics\\resource\\sprite.jpg" or whereever your texture is located.

If that's the problem, there are quite a few discussions on this site about setting up paths to resources. The favorite method involves calling GetModuleFileName() and deriving a path from that.

Share this post


Link to post
Share on other sites
Yeah it seems that was it. Could of sworn i saw a different message, when I moved the image file out of the release folder (location of the exe).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement