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

Author Topic: Creating Sprite array from a single Sprite  (Read 2070 times)

0 Members and 1 Guest are viewing this topic.

JOJO

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Creating Sprite array from a single Sprite
« on: January 18, 2017, 03:34:54 pm »
Hello everyone thanks for taking the time to read my post,
So I just started out with SFML, I finished coding a chess engine and I wanted to add some GUI to it.
I'm currently having quiet a lot of issues loading all my sprites from just one file which would contain all the figures dispaying features.
(So I'm planning on having a sprite array). I also have no idea, how to display them on a chess_board? How to get the positions etc...
I hope you can bring some light on these questions else thanks you for reading and trying to help I hope my questions were clear enough
Thank you !

Chaia*

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Creating Sprite array from a single Sprite
« Reply #1 on: January 18, 2017, 10:32:36 pm »
By having a Sprite Array do you mean for the "chess field" (the black/white squares)?
If yes, why are you not using one Sprite for the whole field?

Regarding the issue with loading the sprites: You just need to have 1 sf::Texture (which will contain your whole Texture/Image/Picture...). Then you create a Sprite for every game entity you have (e.g. for each chess figure) and store them in an array/vector or somehow in another way (e.g. in your game-entity-classes/structs) and call setTexture(....). After that you finally just need to specify the section of your texture which shall be mapped on your Sprite by calling sf::Sprite::setTextureRect(...).

Regarding the isse with the positions: You probably have an Array representing your chess field (and holding information like: On field XY stands figure Z) and you know the size of one of your small sub-fields (black/white square). Then, the position for each sub-field is simply calculated by multiplying the array-index of that sub-field with the size of one of your sub-fields.
For the sprites you can then call sf::Sprite::setPosition(...).

Hope that helps. If not, try to specify where or what exactly your problems are (e.g. what exact issue do you have with loading all your sprites from one file?).
« Last Edit: January 18, 2017, 10:34:48 pm by Chaia* »

JOJO

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Creating Sprite array from a single Sprite
« Reply #2 on: January 19, 2017, 11:19:16 am »
Hello Chaia,
Thank you for your answer I didn' t think about using the sf::Sprite::setTextureRect(...) to select the parts of the sprite I want...
The answer helped a lot I'll get to coding it now :)
Have a nice day !