int main()
{
clase_obj.load("mono.jpg");
//clase_obj.spray.SetImage(clase_obj.foto); // This one works fine
clase_obj.sprite_load(clase_obj.foto); // This one doesn't work
clase_obj.spray.SetPosition(300,300);
ventana.SetFramerateLimit(60);
while (ventana.IsOpened())
{
salir();
ventana.Clear();
ventana.Draw(clase_obj.spray);
ventana.Display();
}
return EXIT_SUCCESS;
}
The problem is that you're making a "copy" that doesn't actually point to the proper place in memory. Basically, when you first load the image file it's stored in memory slot A and when you try to pass it to your sprite_load you're copying the image information but not the location of the file in memory (pointing to slot B)
So what you can do is replace your sprite_load with this:
1 2 3 4 5
//This passes the address of the loaded image
void sprite_load(sf::Image& pic)
{
spray.SetImage(pic);
}
Or you can bypass this function altogether and just make something like this:
Thanks a lot! I'm still understanding these concepts, but it works now. About your second suggestion, that's the way I was doing it so far, but since now I want to make an array of images as instances of the same class, I just want to load the image once. Correct me if I'm mistaken, but if I did this:
Glad it helped :) And yes, you are right that it would load the file multiple times. You might consider looking in to creating some sort of storage place for your images. As in, a class that has a container and you can just load the image in to this manager-type object then reference it later.
Might I ask what you're working on? :D
I'm just getting familiarized with C++, and since my goal is to develop video games, I'm learning SFML along the way. The code is for a 2D platformer, serving no other purpose than presenting me with challenges to program. I made an Arkanoid clone before, but I decided to move to something a little more exciting.
(I know I should've completely finished Arkanoid first, but frankly this platformer is a far more enjoyable project.)