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

Author Topic: Is there a way to Draw sprite at position without setPosition  (Read 3084 times)

0 Members and 1 Guest are viewing this topic.

anpd

  • Newbie
  • *
  • Posts: 3
    • View Profile
Is there a way to Draw sprite at position without setPosition
« 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10918
    • View Profile
    • development blog
    • Email
Re: Is there a way to Draw sprite at position without setPosition
« Reply #1 on: November 03, 2014, 01:22:35 pm »
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

anpd

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Is there a way to Draw sprite at position without setPosition
« Reply #2 on: November 03, 2014, 01:33:03 pm »
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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10918
    • View Profile
    • development blog
    • Email
Re: Is there a way to Draw sprite at position without setPosition
« Reply #3 on: November 03, 2014, 01:39:20 pm »
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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

anpd

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Is there a way to Draw sprite at position without setPosition
« Reply #4 on: November 03, 2014, 01:41:30 pm »
Cheers  :) will take a look at that then.

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Is there a way to Draw sprite at position without setPosition
« Reply #5 on: November 03, 2014, 02:29:14 pm »
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

(tough it will ne adjustments to handle changes)
« Last Edit: November 03, 2014, 02:32:12 pm by Ghosa »

 

anything