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

Author Topic: Question regarding textures  (Read 923 times)

0 Members and 1 Guest are viewing this topic.

Wouter

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Question regarding textures
« on: October 22, 2016, 07:21:05 pm »
Hello there,
I've been using SFML to create a little game, but I've been wondering how to use textures properly.

I use a sprite for the player in my game and to change its texture (for example when walking in different directions) i do the following:
-create a texture for each direction (sf::texture walkTexture[4]  and load each image from file to texture)
-set player sprite to appropriate texture (playerSpite.setTexture(walkTexture[1-4]))

I've seen people put all the images for the animations in 1 big texture and then loading different parts of that texture to the sprites, is there any advantage in doing this or is it practically the same as my way in terms of efficiency?

Thanks in advance!

ps: Sorry for my imperfect English  :D





« Last Edit: October 22, 2016, 08:10:26 pm by Wouter »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Question regarding textures
« Reply #1 on: October 23, 2016, 01:22:40 pm »
There are advantages to using a single texture for all (or as many as possible) animation frames for an entity and some extra advantages to using a single texture for animations frames for multiple entities. There are also disadvantages.

Some advantages (that spring to mind at the moment) for using the same texture for lots of multiple animations for multiple entities are: if no other texture is used, the avoidance of texture switching can speed up rendering; the frames can be edited at once (for example, global color adjustment without having to apply to multiple files, copying between similar frames or using identical frames for different animations); fewer files (and possibly less disk space usage); possibly less memory usage (depending on arrangement frames in the texture and also if the files are stored in memory as each file's header would increase usage).

Some advantages to using separate files can include: frames/animations may be easier to track/follow/remember/use; possibility of a more modular design including only loading the animations required, which may reduce the amount of memory used and modifying one animation does not affect any other animation; less chance of using a texture that is larger than the graphics card's maximum size.


I would say that my default would be to attempt to fit the animations onto a single texture and use some sort of list of which frame is where and recall the texture rectangle from that list. Selba Ward's Gallery Sprite can do this for you. Plinth's Frame Sequence can help with controlling the frame animations too.

However, instead of having all the frames 'packed' and possibly out-of-order, you could store them in a grid and then, instead of using different files, use a different row (or column) for a different animation.
For example, walk left might be row 0, walk right row 1, stopped row 2 and each row might have 4 frames/columns. Although, each row can have a different number of columns, you would have to track how many depending on the row (again, Plinth's Frame Sequence can help here).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Wouter

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Question regarding textures
« Reply #2 on: October 23, 2016, 03:31:20 pm »
I think I understand, for future projects I think I am going to use the single file method because i've been noticing (as you said) that the animation images for my current project are a little bit messy/hard to manage.

Thanks for the explanation :D