SFML 2.1, I want do display a sprite,but I get an unhandled exception.

Hi,I'm sorry I come to you again,cpluplus.com users,but I really can't figure out what's wrong. My short code is:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <SFML/Graphics.hpp>

int main(){
	sf::RenderWindow window;
	window.create(sf::VideoMode(800,600),"myWindow");

	sf::Texture pTexture;
	sf::Sprite playerImage;

	pTexture.loadFromFile("image.png");

	playerImage.setTexture(pTexture);
	playerImage.setPosition(100,100);

	while(window.isOpen()){
	
		sf::Event event;
		while(window.pollEvent(event))
		{
			switch(event.type){
			case sf::Event::Closed:
				window.close();
				break;
			}
		}
		
		

		
		window.draw(playerImage);
		window.display();
	}
	return 0;
}


So,in my head,this code should simply draw my image. When I run it though,my console window starts spitting random characteres,and visual studio points me to this line

 
        rep     movsd           ;N - move all of our dwords


from the memcpy.asm file.

Can you please tell me what's wrong?
Your exception is being thrown from line 10 (my guess is your file path is invalid). Your not testing to see if the file was loaded sucessfully either. Try doing something like:

1
2
3
4
5
6

if(! (pTexture.loadFromFile("image.png")) )
{
 //handle error 
}



http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php
Last edited on
I've tried writing

1
2
3
4
5
6

if(! (pTexture.loadFromFile("image.png")) )
{
std::cout<< " Image cant be loaded" ; 
}


just to see what happens,and I got the same exact error. All hell breaks loose in the console,and nothing shows up in the window. The path,though,is correct.
Did you compile SFML yourself?

Are you linking debug libraries with a release build or vice versa?
It's SFML 2.1 for Visual Studio 2012,I followed the exact instructions from the website.
You didn't answer either question.

SFML doesn't come in different flavors for different compilers, so "SFML 2.1 for Visual Studio 2012" doesn't make much sense to me.

Did you compile SFML yourself or are you using a pre-built library?

There is nothing in this code that would cause the problem you indicate (whether or not the file was successfully read,) so one assumes the problem is somewhere in the toolchain (and history has shown that to often be the case.)
It does actually,check this link:

http://www.sfml-dev.org/download/sfml/2.1/

I've downloaded the Visual Studio 2012 version and used these instructions to install it :

http://www.sfml-dev.org/tutorials/2.1/start-vc.php

I guess this is a pre-built library,right? I'm not exactly sure what these words mean, I'm a total beginner,with only high-school programming background.

If I try drawing stuff like

1
2
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
,for example,it works.
Last edited on
Yes, those are pre-built libraries. I had forgotten Laurent was officially supplying them, now. (For a long time they were not supplied while v2 was in development.) They aren't different flavors of SFML, though, they are all compiled from the same source.

I usually just pull the source from the github repository and compile for myself.

Assuming you've downloaded the correct version of the pre-built library as you say you have, you should again check to see if you're compiling with the debug libraries in release mode or vice versa.

From the instructions you linked to:

It is important to link to the libraries that match the configuration: "sfml-xxx-d.lib" for Debug, and "sfml-xxx.lib" for Release. A bad mix may result in crashes.


Yes,i've used "sfml-xxx.lib" instead of "sfml-xxx-d.lib".

Now it works.

Thanks.

Have a nice day.
Topic archived. No new replies allowed.