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

Author Topic: Optimization while using SFML  (Read 4615 times)

0 Members and 1 Guest are viewing this topic.

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Optimization while using SFML
« 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.

upseen

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Optimization while using SFML
« Reply #1 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Optimization while using SFML
« Reply #2 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 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Strikerklm96

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Optimization while using SFML
« Reply #3 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?
« Last Edit: March 25, 2013, 04:25:02 am by Strikerklm96 »

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Optimization while using SFML
« Reply #4 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 class in the wiki.
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!