SFML community forums
Help => Graphics => Topic started by: anpd on November 03, 2014, 01:15:43 pm
-
Hi,
Just started testing SFML, I need to draw a grid that changes constantly. Is there a way to do something like
draw(sprite, posX, posY);
Or do I need a sprite for each part of the grid and use setPosition to move them?
-
What kind of "grid" are you talking about?
If you want a draw(sprite, x, y) function, I'd say your code design isn't the best. ;)
And no, SFML doesn't have such a function, but if you really want that, it's easy to write yourself a helper function.
Also don't post in the General Discussion forum when requesting help, I've moved your thread.
-
Thanks for moving the post (sorry). Well in this case I have a game board 19x19 that can be empty, white stone or black stone. All this is just an array with 0,1,2 in it. The status of the board can change from having a stone to being empty or have a stone added.
The nicest way in my opionon would be to just iterate over the array and check if( grid ==1) draw whiteStone at pos. Having potentially 722 sprites for all the different combinations seems like a waste.
-
You don't need 722 sprites. There are many approaches to the problem at hand. One way might be to create some kind of an entity class, for which you then only have to set what type (white/black stone) it has and at which grid position it is (x,y). Then you could derive the entity class from sf::Drawable and implement the virtual draw function, which draws the internal sprite and when you change the grid position, the entity class would set the sprite position newly.
That's just one way, there are also others. ;)
-
Cheers :) will take a look at that then.
-
If by "grid" you mean a map, you should take a look at the tilemap-example:
http://sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#example-tile-map (http://sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#example-tile-map)
(tough it will ne adjustments to handle changes)