SFML, Unable to open file

Started by
1 comment, last by agm_ultimatex 14 years, 2 months ago
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?
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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

This topic is closed to new replies.

Advertisement