SFML community forums

Help => General => Topic started by: Szustarol on June 02, 2016, 10:57:51 pm

Title: Problem with a class with texture element
Post by: Szustarol on June 02, 2016, 10:57:51 pm
Hi!
I have a class like:
someclass name{
public:
 sf::Texture sometexture;
 sf::Sprite somesprite;
void settexture(){
somesprite.settexture(sometexture);
}
void loadtxt(std::string path){
sometexture.loadfromfile(path);
}
};

and then somewhere in code  i have;
std::vector <name> arrayofclasses;
...
arrayofclasses[i].loadtxt("Files/file.bmp");
arrayofclasses[i].settexture();
 
and i get compiler error:
'void sf::Shape::setTexture(const sf::Texture *,bool)': cannot convert argument 1 from 'sf::Texture' to 'const sf::Texture *'


ofc the code up there is just sample i wanted to explain what i have in a simple way, but it looks like this: array of classes, in one of them i want to set texture and then load them to sprite.

im using visual studio

why does it happen?
what is wrong?
would appreciate any help :)
Title: Re: Problem with a class with texture element
Post by: victorlevasseur on June 02, 2016, 11:22:59 pm
Hi,

That's because sf::Sprite::setTexture wants a pointer to a sf::Texture and not directly a sf::Texture.
Title: Re: Problem with a class with texture element
Post by: Mortal on June 03, 2016, 11:39:30 am
in your example, doesn't generate the compiler error, except that you have mistakes about  member functions's names for both sf::Sprite and sf::Texture.
Title: Re: Problem with a class with texture element
Post by: Szustarol on June 03, 2016, 02:18:21 pm
okay, so now i added & before name of texture and i have the white square problem :/
i know it happens if texture is destroyed after calling a function, but it shouldnt be destroyed here, because its a class and it still exists. Why is it?
Title: Re: Problem with a class with texture element
Post by: Arcade on June 03, 2016, 04:49:20 pm
It's hard to guess what the problem could be with the provided snipped of code. You may need to provide a more complete (but still minimal) example of how you are setting up and drawing your sprites.

Your original error is a bit suspicious, though. While it's true that the setTexture function for sf::Shape (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Shape.php#af8fb22bab1956325be5d62282711e3b6) takes in a texture pointer, this is not true for sf::Sprite (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Sprite.php#a3729c88d88ac38c19317c18e87242560). Are you sure you're not accidentally setting the texture on the wrong thing?
Title: Re: Problem with a class with texture element
Post by: Szustarol on June 03, 2016, 05:20:11 pm
I want to print all images from a folder.
I read it all with a function that is not really important in this problem imo
I have something like
image.gif
image2.gif
etc etc
so then i create as many objects of a class that looks similar to the one i posted before, as many images there are

then in a loop i set the parameters of the somesprite like size etc.
and then i use the loadtxt function, to load the sprite with texture


then there is a piece of unrelated code

at the end of program i display all of loaded images in positions set before in a loop

all loops mentioned above display the images depending on the count of the images gathered at the loading process, so the display looks something like
window display someclass[i].somesprite
where i is the counter that counts towards the number of all images

i hope the explanation is clear, if it is not ill just post code here
Title: Re: Problem with a class with texture element
Post by: Arcade on June 03, 2016, 08:18:25 pm
As far as I can tell your high level explanation seems fine, but I'm thinking you made a mistake in the implementation somewhere. What I was getting at was that it may help if you could reproduce the problem with as little code as possible and provide that complete but minimal code here for us to look at. Going back to your first post:
Quote
and i get compiler error:
'void sf::Shape::setTexture(const sf::Texture *,bool)': cannot convert argument 1 from 'sf::Texture' to 'const sf::Texture *'
From the snippet of code you have shown us so far I do not see how you could get this error. You are not using sf::Shape (or anything that derives from it). You are using sf::Sprite.
Title: Re: Problem with a class with texture element
Post by: Szustarol on June 03, 2016, 08:44:41 pm
Oh I'm really sorry!
Its sf::Rectangleshape not Sprite!
I have noticed it right after your post, im so stupid i have mistake those two. Im really sorry!
Is there a difference if Im using sf::Rectangleshape?