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

Author Topic: Loading Images with Strings  (Read 2482 times)

0 Members and 1 Guest are viewing this topic.

Makuto

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
    • Au 79 Games
    • Email
Loading Images with Strings
« on: July 06, 2011, 07:15:42 pm »
Hi! I'm making a library that makes SFML really simple, and I need a way to load images with a string.  The user of the lib creates a new "object," then an object function loads an image and assigns it to an SFML sprite.  The image's filename is passed to the function via a string.  I get a compiler error that the string has to be a constant.  Is there a way around this, or do I need to code it completely differently?
Macoy Madson-Au 79 Games
Check out my work at http://augames.f11.us/
Most of my SFML-related code is here: https://github.com/makuto/personal/tree/master/gameDev/resources/base2.0
I try to KIS(S), do you?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Loading Images with Strings
« Reply #1 on: July 06, 2011, 07:32:02 pm »
Show some code.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Makuto

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
    • Au 79 Games
    • Email
Loading Images with Strings
« Reply #2 on: July 06, 2011, 07:43:48 pm »
Here's the class declaration in the .cpp file:
Code: [Select]
void object::loadImage(string image)
{
    obImg->LoadFromFile(image.c_str());////////////////
    obSpr->SetImage(obImg);
}


Here is the declaration in a .h file for object class:
Code: [Select]

class object
{
    public:
     void move (std::string, float);
     void setPosition(float, float);
     void loadImage(std::string);
     void center (int, int);
     //void obArray();
     void newInstance();
     void instance(int, float, float, float, int, std::string, float);
     void deleteInstance(int);
     void deleteAllInstances();
     object();
     ~object();
     //////VARIABLES
     float x;
     float y;
     float rotation;
     bool collision;
     float boundBox[2];
     bool display;
     float origin[2];
     float moveSpeed;
    private:
      std::string moveType;
      sf::Image *obImg;
      sf::Sprite *obSpr;
      instance *instances;
};
Macoy Madson-Au 79 Games
Check out my work at http://augames.f11.us/
Most of my SFML-related code is here: https://github.com/makuto/personal/tree/master/gameDev/resources/base2.0
I try to KIS(S), do you?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Loading Images with Strings
« Reply #3 on: July 06, 2011, 07:48:29 pm »
Okay, and what exact error do you now get (copy the message) and where does the error occur?

Why do you use c_str()? sf::Image::LoadFromFile() takes an std::string. Passing a const char* is not forbidden, but unnecessary.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Makuto

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
    • Au 79 Games
    • Email
Loading Images with Strings
« Reply #4 on: July 06, 2011, 07:51:32 pm »
C:\Documents and Settings\Macoy\My Documents\Development\Programming\C++\My Projects\gamegears\gameGears.cpp|20|error: no matching function for call to `sf::Sprite::SetImage(sf::Image*&)'|

That's the first error, here's the next line:

..\..\..\..\..\..\..\..\SFML-1.6\include\SFML\Graphics\Sprite.hpp|72|note: candidates are: void sf::Sprite::SetImage(const sf::Image&)|
Macoy Madson-Au 79 Games
Check out my work at http://augames.f11.us/
Most of my SFML-related code is here: https://github.com/makuto/personal/tree/master/gameDev/resources/base2.0
I try to KIS(S), do you?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Loading Images with Strings
« Reply #5 on: July 06, 2011, 08:01:21 pm »
Ah, I see it. You pass a pointer obImg to SetImage(), but you need to dereference it first. sf::Sprite::SetImage() takes a reference, no pointer.

By the way, you should allocate memory for your pointers, or they are uninitialized. The better option would be not to use pointers at all (why sf::Image* instead of sf::Image?)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Makuto

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
    • Au 79 Games
    • Email
Loading Images with Strings
« Reply #6 on: July 06, 2011, 08:07:43 pm »
Ok, I understand. Thanks a lot for the help!
Macoy Madson-Au 79 Games
Check out my work at http://augames.f11.us/
Most of my SFML-related code is here: https://github.com/makuto/personal/tree/master/gameDev/resources/base2.0
I try to KIS(S), do you?

 

anything