SFML community forums

Help => Graphics => Topic started by: Strikerklm96 on March 21, 2013, 12:15:58 am

Title: Optimization while using SFML
Post by: Strikerklm96 on March 21, 2013, 12:15:58 am
Hello.

I have been using SFML while developing a game I have been designing for a long time. I'm still "relatively" new to programming (C++ for about 2 years) so I have several questions with my plans. I should give a bit of background before going straight to the graphics part, because maybe the graphics idea is good but the design concept isn't. More likely, they're both bad.

Here is the design concept:
Have a vector of GameEntities.
Each GameEntity has a draw function.
Every frame, go through the vector and draw them on screen at some location.

A player has "control" over one of those entities in the form of a pointer that points at one of the objects, so when he presses a key, it iterates the GameEntities position by a certain amount (movement). Similar things for the rest of things this GameEntity will be able to do.

HERE IS THE GRAPHICS PART:
The class GameEntity has a myTexture pointer variable that dictates what the GameEntity looks like in game. I was originally going to have a different texture for each animation sequence loaded into an array inside the GameEntity when the game loads. So when an event caused it to change its animation, a function would change  what the myTexture pointer is pointing to in the array. But I don't want to go coding anything that is going to be garbage and inefficient. How should I go about this? This game isn't going to be super CPU intensive so I don't need the ABSOLUTE best method, but will this work well enough?

Thanks.
Title: Re: Optimization while using SFML
Post by: upseen on March 21, 2013, 11:16:11 am
Make all frames of the animation in one single big texture, and use SubRects, pointing to a different part of the texture each frame of the animation.

Performance-wise, this won't be much different from the method you mentioned, but it is way much easy to handle and is a big plus to resource management.

A faster way, is to let the vertex shader handle the animation (using SubRects too), but it might be just as slow on old graphic cards than it is fast on newer ones, but it's not really worth it on small projects.
Title: Re: Optimization while using SFML
Post by: Nexus on March 21, 2013, 02:31:00 pm
Like upseen, I recommend to use switch texture rects instead of textures.

I have implemented animation functionality (http://www.bromeon.ch/libraries/thor/v2.0/doc/group___animation.html) in Thor. Since it's open source, you can look at its implementation to let you inspire, or you can directly use the Thor library in your project.
Title: Re: Optimization while using SFML
Post by: Strikerklm96 on March 25, 2013, 04:21:00 am
Thanks for the replies! :)

Ok, so i'm going to have all the potential states of the thing on a single texture, and when I change what the object looks like, i'm actually just changing what part of the texture is being displayed?
^^ Is that correct? ^^

Also, did the rest of my game design seem good?
Title: Re: Optimization while using SFML
Post by: mateandmetal on March 26, 2013, 01:07:12 pm
Ok, so i'm going to have all the potential states of the thing on a single texture, and when I change what the object looks like, i'm actually just changing what part of the texture is being displayed?
^^ Is that correct? ^^

Correct. You can take a look at the AnimatedSprite (https://github.com/SFML/SFML/wiki/Source%3A-AnimatedSprite) class in the wiki.