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

Author Topic: Problem with a class with texture element  (Read 2502 times)

0 Members and 1 Guest are viewing this topic.

Szustarol

  • Newbie
  • *
  • Posts: 19
    • View Profile
Problem with a class with texture element
« 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 :)

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Problem with a class with texture element
« Reply #1 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.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Problem with a class with texture element
« Reply #2 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.

Szustarol

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Problem with a class with texture element
« Reply #3 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?

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Problem with a class with texture element
« Reply #4 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 takes in a texture pointer, this is not true for sf::Sprite. Are you sure you're not accidentally setting the texture on the wrong thing?

Szustarol

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Problem with a class with texture element
« Reply #5 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
« Last Edit: June 03, 2016, 05:21:48 pm by Szustarol »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Problem with a class with texture element
« Reply #6 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.

Szustarol

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Problem with a class with texture element
« Reply #7 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?

 

anything