Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Won't load image  (Read 4426 times)

0 Members and 1 Guest are viewing this topic.

M squared

  • Newbie
  • *
  • Posts: 22
    • View Profile
Won't load image
« on: March 09, 2011, 01:25:28 am »
So i'm using
   sf::Image Image;
   if(Image.LoadFromFile("sprite.png"))
to try to load an image, but I keep getting an error that it can't load the image. I included it in the project file. Heres the code if you need it/
Code: [Select]

// sfml specific headers required
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int main()
{
//create a sfml window
sf::RenderWindow Game(sf::VideoMode(800, 600, 32), "SFML Blank Window");

//creat the sfml event handler
sf::Event Event;

//Creat a SFML shape
sf::Shape box = sf::Shape::Rectangle(0, 0, 50, 50,sf::Color(127, 0, 100, 255));

//set the box position
box.SetPosition(100, 100);

//creat the image buffer
sf::Image Image;
if(Image.LoadFromFile("sprite.png"))
return EXIT_FAILURE;

// create SFML sprite
sf::Sprite sprite;

//set the sprite image
sprite.SetImage(Image);

// set the sprites posisiton
sprite.SetPosition(400, 400);

// game loop
while(Game.IsOpened())
{
// event handler loop
while(Game.GetEvent(Event))
{
//if x'd out close the game window
if(Event.Type == sf::Event::Closed)
Game.Close();
}
//clear the game window
Game.Clear(sf::Color(100, 149, 237));

// closes game when esc is pressed
if(Game.GetInput().IsKeyDown(sf::Key::Escape))
Game.Close();
// if left mouse and A or D is down move box
if(Game.GetInput().IsMouseButtonDown(sf::Mouse::Left))
{
if(Game.GetInput().IsKeyDown(sf::Key::A))
{
box.Move(-1, 0);
}
else if(Game.GetInput().IsKeyDown(sf::Key::D))
{
box.Move(1, 0);
}
}
//if the right mouse button is pressed and a or d is down move box
else if(Game.GetInput().IsMouseButtonDown(sf::Mouse::Right))
{
if(Game.GetInput().IsKeyDown(sf::Key::A))
{
sprite.Move(-1,0);
}
else if(Game.GetInput().IsKeyDown(sf::Key::D))
{
sprite.Move(1, 0);
}
}

//draw the shape
Game.Draw(box);

// draw the sprite
Game.Draw(sprite);

//display sfml graphics to the game window
Game.Display();
}

return EXIT_SUCCESS;
}

Relic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Won't load image
« Reply #1 on: March 09, 2011, 05:15:49 am »
Code: [Select]
if(Image.LoadFromFile("sprite.png"))
      return EXIT_FAILURE;


It should be:

Code: [Select]
if(!Image.LoadFromFile("sprite.png"))
      return EXIT_FAILURE;

M squared

  • Newbie
  • *
  • Posts: 22
    • View Profile
Won't load image
« Reply #2 on: March 09, 2011, 12:51:02 pm »
Quote from: "Relic"
Code: [Select]
if(Image.LoadFromFile("sprite.png"))
      return EXIT_FAILURE;


It should be:

Code: [Select]
if(!Image.LoadFromFile("sprite.png"))
      return EXIT_FAILURE;

Right, I did that, but now it immediantly closes before it even has a chance to draw the sprite. This doesn't happen when I leave out the not operator.

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Won't load image
« Reply #3 on: March 09, 2011, 01:24:29 pm »
Quote from: "M squared"

right, i did that, but now it immediantlh closes before it even has a chance to draw the sprite. This doensn't happen when I leave out the not operator.

Let's stop and think for a bit :)

LoadFromFile returns true if it successfully loads the image.

If it exits if you add the not-operator, it means that it couldn't load the image (i.e. it can't find the file, or the file is in a wrong format or corrupted).

Simply continuing on by not adding the not-operator (i.e. exit if you CAN load the image) won't do you any good.

Figure out why it can't find the file - I'm pretty sure it's because it's searching for it in the wrong directory (i.e. your current working directory is not the same as where the image lies).

Edit: I just saw your post about adding the file to the project - that's not how things work. It'll try to find the file from the working directory on your hard drive - adding it as a resource won't help you to LoadFromFile.

M squared

  • Newbie
  • *
  • Posts: 22
    • View Profile
Won't load image
« Reply #4 on: March 09, 2011, 07:38:25 pm »
Dang, my typing has gotten pretty bad...
Anywho, your right, my bad. I was thinking about somehting else....
In which case, what directory and or file do you think I should add it to?

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Won't load image
« Reply #5 on: March 10, 2011, 08:35:38 am »
Well - if you're using Visual Studio (or VC++ Express) - you could set the debug working directory to wherever you have the media files (i.e. your picture). (Project settings / Debug... / Working Directory)

You could also add the images to your Debug / Release directory as that would be your working directory by default.

M squared

  • Newbie
  • *
  • Posts: 22
    • View Profile
Won't load image
« Reply #6 on: March 10, 2011, 04:43:05 pm »
Quote from: "devlin"
Well - if you're using Visual Studio (or VC++ Express) - you could set the debug working directory to wherever you have the media files (i.e. your picture). (Project settings / Debug... / Working Directory)

You could also add the images to your Debug / Release directory as that would be your working directory by default.

Thanks, it's working.