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

Author Topic: 4 newbie questions (tilemap, textures & animating a sprite)  (Read 2052 times)

0 Members and 1 Guest are viewing this topic.

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
4 newbie questions (tilemap, textures & animating a sprite)
« on: February 08, 2018, 06:02:01 pm »
Hi! I made a small sample game based on tilesets without really looking how its done besides the "Example: tile map" tutorial to see if I could figure it how and learn on the way. I will now be looking into how others have done it to learn more and figure out which way might be better, but until then:

Its a 2d platformer and there are 4 things I'm not sure of and would like to ask more experienced users.

1 - When I create my vertex array, if my tile value is 0, its supposed to not have a tile. So during the creation, when this happens I skip to the next cycle. As far as I understand this means the QUADs will get the default values of (0,0) in all 4 positions and all  4 texture coordinates. That is also what I use when I want to remove a block from the game, i set its QUAD position and texture coords to (0,0).

Is this the way you should do things or there's a better way? Since the example has no such cases i figure it out on my own and might be doing something wrong.

2 - on my map.load (where I create my vertex array) I should be getting the "map" (vector<vector<int>>) NOT by value but as either a pointer or as a reference, right? I honestly need to go read more about pointers & references.

3 - I have N amount of enemy's that walk left and right like SMB Gumba's, to animate them independently of each other, I have 2 choices:
A - Use a vector of clocks, one for each enemy.
B - use 1 clock and vector of SF::time one for each enemy so I can calculate time passed.
Which of the 2 would be optimal? There's a 3rd choice of 1 clock and animate all the enemy's at the same time, but that might not work in all cases, I'm not sure.

4 - To animate a sprite would it be better to use several textures and use the ".setTexture" to change the animation frame or to use 1 texture and use the ".setTextureRact"?
I'm asking performance wise. Probably doesn't make a difference, but I'm curious.

In case you want to look at the tilemap code:
(click to show/hide)


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: 4 newbie questions (tilemap, textures & animating a sprite)
« Reply #1 on: February 09, 2018, 12:04:34 am »
Hi 😊

1. For 'empty' tiles, you can also use a transparent part if the texture and leave the co-ordinates where they would have been if visible.

2. Yes, using a reference here is a good choice unless you actually want to move it or copy it.

3. I'd probably use a global clock and use offset times for everything. Many clocks seem like a waste but they aren't much more than time offsets anyway. As long as they aren't being constantly created and destroyed, multiple clocks seems acceptable.

4. Definitely not setTexture. Use setTextureRect instead. Setting a texture can affect performance (possibly severely if done a lot) whereas setting the texture rectangle is 'free'.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: 4 newbie questions (tilemap, textures & animating a sprite)
« Reply #2 on: February 09, 2018, 06:39:35 am »
Quote
4. Definitely not setTexture. Use setTextureRect instead. Setting a texture can affect performance (possibly severely if done a lot) whereas setting the texture rectangle is 'free'.
Note that in practice, this is rarely true. To make a difference, the animated character would have to be the only thing to be drawn; or you would have to put other stuff into the texture (ie. make an atlas).

If it's not clear, here is the sequence of texture switch when using one texture per frame:
draw(something_using_texture_A);
draw(animation_texture_frame_x);
draw(something_using_texture_B);
...
 

And now using a single animation texture:
draw(something_using_texture_A);
draw(animation_texture);
draw(something_using_texture_B);
...
 
... no difference.
Laurent Gomila - SFML developer

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: 4 newbie questions (tilemap, textures & animating a sprite)
« Reply #3 on: February 09, 2018, 02:46:54 pm »
Ok, thank you both! Short and easy to understand.


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: 4 newbie questions (tilemap, textures & animating a sprite)
« Reply #4 on: February 17, 2018, 03:11:33 pm »
No problem; you are welcome. :)

To be clear, I did use the word "can" to show that there are times when it would not affect performance.
Although, to be fair, I don't think it's "rare" to draw multiple things from the same texture and this is when it texture rectangle becomes an improvement. Texture rectangle is the same in the other case so using a texture rectangle as default is probably the safest bet.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*