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

Author Topic: Query about draw function  (Read 4432 times)

0 Members and 1 Guest are viewing this topic.

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Query about draw function
« on: February 18, 2014, 09:14:14 pm »
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:

Quote
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.


Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Query about draw function
« Reply #1 on: February 18, 2014, 09:18:16 pm »
Well, first of all, sprite is const, and you are trying to alter it.

"object type is: const Sprite"

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Query about draw function
« Reply #2 on: February 18, 2014, 09:23:55 pm »
void draw(sf::RenderTarget &target, sf::RenderStates states) const;
 

Thats your problem ;)

Edit: Azaral was first
Failing to succeed does not mean failing to progress!

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Query about draw function
« Reply #3 on: February 18, 2014, 09:40:01 pm »
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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Query about draw function
« Reply #4 on: February 18, 2014, 10:46:59 pm »
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.
Laurent Gomila - SFML developer

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Query about draw function
« Reply #5 on: February 20, 2014, 11:29:03 pm »
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.
« Last Edit: February 20, 2014, 11:31:44 pm by gop_t3r »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Query about draw function
« Reply #6 on: February 21, 2014, 07:47:17 am »
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.
Laurent Gomila - SFML developer

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Query about draw function
« Reply #7 on: February 21, 2014, 02:45:51 pm »
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.

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Query about draw function
« Reply #8 on: February 21, 2014, 10:14:06 pm »
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.

 

anything