-
Suppose I have a class like this:
Class GraphicalEntity : public sf::Drawable, sf::Transformable
{
public:
GraphicalEntity();
private:
sf::Sprite aSprite
void draw(sf::RenderTarget &target, sf::RenderStates states) const;
};
And then there's the implementation thereof with draw():
draw(sf::RenderTarget &target, sf::RenderStates states) const
{
aSprite.setPosition(100, 100); // Error
target.draw(aSprite);
}
I get an error when I want to set the position of the sprite, visual studio complains:
Error: no instance of overloaded function "Sprite::setPosition" matches the argument list and object (the object has type qualifies that prevent a match)
argument types are: (int,int)
object type is: const Sprite
However, if I declare a sprite within draw's implementation then I can easily apply various transforms. What I want to know is, how can I use aSprite within the draw function? If I declare a local sprite within the draw() function, then I can't use it outside the function.
-
Well, first of all, sprite is const, and you are trying to alter it.
"object type is: const Sprite"
-
void draw(sf::RenderTarget &target, sf::RenderStates states) const;
Thats your problem ;)
Edit: Azaral was first
-
Yes, I understand that; but I don't want to be limited by a Sprite only being declared within the draw() function. Is there a way for me to draw a sprite with the transforms applied externally? Say I want to be able to do something with the sprite externally, I can't do that through the draw() function; hence I want to be able to have more flexibility in how I use a sprite without it being constant. If the problem is
void draw(sf::RenderTarget &target, sf::RenderStates states) const;
Then what can I do to resolve this issue?
-
The draw function is const because it is meant to draw the object, not to alter it. It makes no sense to move a sprite when you want to draw it. You'll have to find a better design for your class.
-
Thanks for your response. The reason I asked this question is because my tile map can't be drawn if I set positions and the textureRect according to a use case within the draw() function; for example:
case 0:
tile_sprite.setPosition(j * TileSize.x, i * TileSize.y);
tile_sprite.setTextureRect(sf::IntRect(i * 0, j * 0, TileSize.x, TileSize.y));
break;
case 1:
tile_sprite.setPosition(j * TileSize.x, i * TileSize.y);
tile_sprite.setTextureRect(sf::IntRect(i * 40, j * 0, TileSize.x, TileSize.y));
break;
Declaring a local sprite within the draw() function and using the above transformations will draw the tilemap, but how can I apply these transformation outside the draw() function while still being able to draw the sprite? I've tried placing the transformations and the use cases in a separate function, but that causes the tilemap not to be drawn.
-
Use one sprite per tile, not one for everything. You shouldn't have to modify anything when you draw, everything should be ready (and ideally prepared once, in an init/load function).
Have a look at the vertex array tutorial, and especially at the tilemap example at the end.
-
Use one sprite per tile, not one for everything. You shouldn't have to modify anything when you draw, everything should be ready (and ideally prepared once, in an init/load function).
Have a look at the vertex array tutorial, and especially at the tilemap example at the end.
I have looked at it, but the problem is I use a 2D vector array, whereas the tutorial uses a const int; and VectorArray is 1D. I wouldn't know how to convert. Infact, I used this tutorial before I commenced work on another tile loader, namely because I couldn't get collisions up and running.
-
Update: I scouted through some old code of mine and reused it in conjunction with the vertex arrays example you referred me to. I got a tile loader up and running thanks, now I just need to apply collisions somehow.