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

Author Topic: problem with loading images from file  (Read 3731 times)

0 Members and 1 Guest are viewing this topic.

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
problem with loading images from file
« on: June 02, 2011, 03:05:03 pm »
hi, when I load these images, it fails for some reason. All are valid locations on the computer.

Code: [Select]

#include <SFML/Graphics.hpp>
#include <vector>
#define ONE
#define TWO  
#define THREE
#define POSX 300
#define POSY 200


std::vector<sf::Sprite> bSprite(3);

int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Test for the button etc");

sf::Image One;
sf::Image Two;
sf::Image Three;
if(!One.LoadFromFile(ONE))
return EXIT_FAILURE;
if(!Two.LoadFromFile(TWO))
return EXIT_FAILURE;
if(!Three.LoadFromFile(THREE));
return EXIT_FAILURE;




while(App.IsOpened())
{
sf::Event ev1;
while(App.GetEvent(ev1))
if(ev1.Type == sf::Event::Closed)
App.Close();



int x;

App.Clear();

App.Display();
}

return EXIT_SUCCESS;
}


i took out definitions for one,two, three because of security reasons etc.

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
problem with loading images from file
« Reply #1 on: June 02, 2011, 03:26:22 pm »
also here is the code that derived the earlier code

Code: [Select]

#include <SFML/Graphics.hpp>
#include <vector>
#define ONE
#define TWO
#define THREE
#define POSX 300
#define POSY 200


std::vector<sf::Sprite> bSprite(3);

void add(sf::Image, int);
int check(sf::RenderWindow*);


int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Test for the button etc");

sf::Image One;
sf::Image Two;
sf::Image Three;
if(!One.LoadFromFile(ONE))
return EXIT_FAILURE;
if(!Two.LoadFromFile(TWO))
return EXIT_FAILURE;
if(!Three.LoadFromFile(THREE));
return EXIT_FAILURE;

add(One, 0);
add(Two, 1);
add(Three, 2);


bSprite[0].SetPosition(POSX, POSY);
bSprite[1].SetPosition(POSX, POSY);
bSprite[2].SetPosition(POSX, POSY);



while(App.IsOpened())
{
sf::Event ev1;
while(App.GetEvent(ev1))
if(ev1.Type == sf::Event::Closed)
App.Close();



int x;

App.Clear();
x = check(&App);
if( !x == 1)
App.Draw(bSprite[0]);
App.Display();
}

return EXIT_SUCCESS;
}



void add(sf::Image b, int i)
{
bSprite[i].SetImage(b);


}

int check(sf::RenderWindow * g)
{

while(g->GetInput().GetMouseX >
bSprite[1].GetPosition().x &&
g->GetInput().GetMouseX <
bSprite[1].GetPosition.x + 100 &&
g->GetInput().GetMouseY >
bSprite[1].GetPosition().y  &&
g->GetInput().GetMouseY <
bSprite[1].GetPosition().y + 30 &&
g->GetInput().IsMouseButtonDown() == true )
{
g->Draw(bSprite[2]);
return 1;
}

while(g->GetInput().GetMouseX > bSprite[1].GetPosition().x && g->GetInput().GetMouseX < bSprite[1].GetPosition.x + 100 && g->GetInput().GetMouseY > bSprite[1].GetPosition().y  && g->GetInput().GetMouseY < bSprite[1].GetPosition().y + 30 )
{
g->Draw(bSprite[1]);
return 1;
}

return 0;
}



only problem here is that the check function doesn't work because the sprites...
I'm guessing it's because they fail to load(this doesn't compile at all)

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
problem with loading images from file
« Reply #2 on: June 02, 2011, 04:15:15 pm »
Err what are ONE, TWO and THREE defined as ?!?!

Maybe you need to do:

#define ONE "myfileone.png"
#define TWO "myfiletwo.png"
#define THREE  "myfilethree.png"

?
SFML 2.1

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
problem with loading images from file
« Reply #3 on: June 02, 2011, 04:16:48 pm »
Sorry, I then saw the comments "took out definitions for one,two, three because of security reasons etc."

If you going to do that, change the filenames to something else.
SFML 2.1

Fouf

  • Newbie
  • *
  • Posts: 23
    • View Profile
problem with loading images from file
« Reply #4 on: June 02, 2011, 06:03:02 pm »
Most likely invalid locations... I can not see any other reason.. at the moment.
- Fouf

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
problem with loading images from file
« Reply #5 on: June 02, 2011, 08:36:19 pm »
They are all valid locations... I copied them from the shell exactly from file paste from nautilus and copy pasted to actual file...


They are all jpegs... is that the reason... possibly it's too much for the Image etc??

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
problem with loading images from file
« Reply #6 on: June 02, 2011, 11:43:57 pm »
If it doesn't compile it's not because it can't load the images, that would happen at runtime.
What does the output of the compiler say?

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
problem with loading images from file
« Reply #7 on: June 03, 2011, 12:02:19 am »
I've broken down the program though... When I try to load these files they mess up, and that's where the problem is. When I try to manipulate them through the sprite that the images are assigned though via x.SetImage(), it fails and these errors come up:


Quote
btest1.cpp: In function ‘int check(sf::RenderWindow*)’:
btest1.cpp:76: error: invalid use of member (did you forget the ‘&’ ?)
btest1.cpp:78: error: ‘bSprite.std::vector<_Tp, _Alloc>::operator[] [with _Tp = sf::Sprite, _Alloc = std::allocator<sf::Sprite>](1ul)->sf::Drawable::GetPosition’ does not have class type
btest1.cpp:80: error: invalid use of member (did you forget the ‘&’ ?)
btest1.cpp:82: error: invalid use of member (did you forget the ‘&’ ?)
btest1.cpp:83: error: no matching function for call to ‘sf::Input::IsMouseButtonDown() const’
/usr/local/include/SFML/Window/Input.hpp:72: note: candidates are: bool sf::Input::IsMouseButtonDown(sf::Mouse::Button) const
btest1.cpp:89: error: invalid use of member (did you forget the ‘&’ ?)
btest1.cpp:89: error: ‘bSprite.std::vector<_Tp, _Alloc>::operator[] [with _Tp = sf::Sprite, _Alloc = std::allocator<sf::Sprite>](1ul)->sf::Drawable::GetPosition’ does not have class type
btest1.cpp:89: error: invalid use of member (did you forget the ‘&’ ?)
btest1.cpp:89: error: invalid use of member (did you forget the ‘&’ ?)



EDIT: Also after one of the loadfromfile functions... the semi colon has been removed and there still is a problem in case you caught that or just noticed etc.  So it has nothing to do with loading the images... but something with using the sprites. IDK, i recently (like yesterday) learned about vectors. I'm not sure what is going on.

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
problem with loading images from file
« Reply #8 on: June 03, 2011, 02:55:20 am »
I've figured out my problem :(

Fouf

  • Newbie
  • *
  • Posts: 23
    • View Profile
problem with loading images from file
« Reply #9 on: June 03, 2011, 02:59:19 am »
what was it?
- Fouf

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
problem with loading images from file
« Reply #10 on: June 03, 2011, 03:56:45 am »
haha, i didn't write out the function calls in check right.... a lot of functions are missing their () parentheses.... but now it's segmentation faults

 

anything